Also notice that we have been able to split our computation onto many lines or combine it onto fewer lines rather arbitrarily, and that each line made sense in itself.
;
-ARTICLE: "tour-first-word" "Defining our first word"
+ARTICLE: "tour-first-word" "Defining our first word"
We will now define our first function. Factor has slightly odd naming of functions: since functions are read from left
to right, they are simply called { $strong "words" } , and this is what we'll call them from now on. Modules in Factor define
else that would have been necessary to refactor our function in most languages.
Of course, Factor already has a word for calculating factorial (there is a whole { $vocab-link "math.factorials" }
-vocabulary, including many variants of the usual factorial) and a word for calculating product "(" { $link product } in the
+vocabulary, including many variants of the usual factorial) and a word for calculating product " (" { $link product } in the
{ $vocab-link "sequences" } vocabulary), but as it often happens, introductory examples overlap with the standard library.
;
\"github.tutorial\" scaffold-work" }
You will find a file { $snippet "work/github/tutorial/tutorial.factor" } containing an empty vocabulary. Factor integrates with
-many editors, so you can try { $snippet "\"github.tutorial\" edit" } ":" this will prompt you to choose your favourite editor, and use that
+many editors, so you can try { $snippet "\"github.tutorial\" edit" } ": " this will prompt you to choose your favourite editor, and use that
editor to open the newly created vocabulary.
You can add the definitions of the previous paragraph, so that it looks like
In other cases, you may want to use some defaults, or compute some fields.
-The functional minded will be worried about the mutability of tuples. Actually, slots can be declared to be "read-only"
+The functional minded will be worried about the mutability of tuples. Actually, slots can be declared to be " read-only "
with { $snippet "{ slot-name read-only } " } . In this case, the field setter will not be generated, and the value must be set a the
beginning with a boa constructor. Other valid slot modifiers are { $link POSTPONE: initial: } - to declare a default value - and a class word
, such as { $snippet "integer" } , to restrict the values that can be inserted.
: fib-rec ( n -- f(n) ) [ 1 - fib ] [ 2 - fib ] bi + ;
" }
-(notice the use of { $link POSTPONE: DEFER: } to define two mutually "recursive" words). You can benchmark the running time writing { $snippet "40 fib" }
+(notice the use of { $link POSTPONE: DEFER: } to define two mutually " recursive " words). You can benchmark the running time writing { $snippet "40 fib" }
and then pressing Ctrl+t instead of Enter. You will get timing information, as well as other statistics. Programmatically
, you can use the { $link time } word on a quotation to do the same.
where the first bit represents whether the basis is a purine or a pyrimidine, and the second one identifies bases that
pair together.
-Our aim is to read a sequence of letters a, c, g, "t" - possibly with spaces - and convert them to a bit array. Factor
+Our aim is to read a sequence of letters a, c, g, " t " - possibly with spaces - and convert them to a bit array. Factor
supports bit arrays, and literal bit arrays look like { $snippet "?{ f f t }" } .
Our syntax for DNA will start with { $snippet "DNA{" } and get all tokens until the closing token { $snippet "}" } is found. The intermediate
Notice the use of { $link map-as } instead of { $link map } . Since the target collection is not a string, we did not use { $link map } , which
preserves the type, but { $link map-as } , which take as an additional argument an examplar of the target collection - here { $snippet "{ }" } .
-Our "final" version flattens the array of pairs with { $link concat } and finally makes into a bit array:
+Our " final " version flattens the array of pairs with { $link concat } and finally makes into a bit array:
{ $code "
SYNTAX: DNA{ \"}\" parse-tokens concat
: prime? ( n -- ? )
2 over sqrt [a,b] [ divisor? ] with any? not ;" }
-If you are not able to visualize what is happening, you may want to consider the { $vocab-link "fry" } vocabulary. It defines { $strong "fried quotations" } ";"
+If you are not able to visualize what is happening, you may want to consider the { $vocab-link "fry" } vocabulary. It defines { $strong "fried quotations" } "; "
these are quotations that have holes in them - marked by { $snippet "_" } - that are filled with values from the stack.
The first quotation is rewritten more simply as
] with-file-reader ;" }
Unfortunately, we are limited to one line. To read more lines, we should chain calls to { $link readln } until one returns { $link f } .
-Factor helps us with { $link file-lines } , which lazily iterates "over" lines. Our "final" definition becomes
+Factor helps us with { $link file-lines } , which lazily iterates over lines. Our " final " definition becomes
{ $code "
: read-first-letters ( path -- )
: print-a-line ( i -- )
star-wars get ?nth print ;" }
-If we give the i-th thread the name "\"i\"" , our example amounts to
+If we give the i-th thread the name { $snippet "i" } , our example amounts to
{ $code "
18 [0..b) [
{ $description "Delegate computation to the specified generator until it is exhausted, before resuming computation in the current generator." } ;
ARTICLE: "generators" "Generators"
-"Generators in Factor are lazily executed blocks of code, which emit values on request. They are designed to work very similarly to generators found in languages like Python or JavaScript."
+"Generators in Factor are lazily executed blocks of code, which emit values on request. They are designed to work very similarly to generators found in languages like Python or JavaScript. "
"Their implementation in Factor is a simple wrapper around " { $vocab-link "coroutines" } "."
$nl
"Generator words are created using " { $link \ GEN: } " or " { $link \ GEN:: } ". When a generator word is called, its required inputs are consumed from the stack, and a " { $link generator } " object is left as output. Accordingly, the right hand side of generator word's stack effect must always be a single value. For example:"
{ $subsections next* ?next* skip* }
"Check if a generator is already exhausted:"
{ $subsections exhausted? }
-"All generator words are found in the " { $vocab-link "generators" } "vocabulary."
+"All generator words are found in the " { $vocab-link "generators" } " vocabulary."
;
ABOUT: "generators"
ARTICLE: "mediawiki.api" "MediaWiki API"
{ $url "https://www.mediawiki.org/wiki/API:Main_page" }
{ $heading "Configuration" }
-"Set " { $snippet "endpoint" } " to the API entry point. An"
+"Set " { $snippet "endpoint" } " to the API entry point. An "
"example for Wikimedia wikis:"
{ $code
"USING: formatting mediawiki.api namespaces ;"
"\"en\" \"wikipedia\" wikimedia-url endpoint set-global" }
$nl
"For Wikimedia wikis, also provide contact information in " {
-$snippet "contact" } " so that wiki operators can contact you in"
-"case of malfunction, including username or email, and possibly"
+$snippet "contact" } " so that wiki operators can contact you in "
+"case of malfunction, including username or email, and possibly "
"the task name:"
{ $code
"USING: mediawiki.api namespaces ;"
"\"password\""
"<password-login> password-login set-global" }
$nl
-"If both login methods are given, OAuth is preferred. If none"
+"If both login methods are given, OAuth is preferred. If none "
"are given, you're not logged in."
$nl
-"If you use several wikis simultaneously, you might want to save"
-"your " { $snippet "cookies" } " (if you use the password login"
-"method) and your " { $snippet "csrf-token" } ". You also should"
-"invalidate your csrf-token before using an action that requires"
+"If you use several wikis simultaneously, you might want to save "
+"your " { $snippet "cookies" } " (if you use the password login "
+"method) and your " { $snippet "csrf-token" } ". You also should "
+"invalidate your csrf-token before using an action that requires "
"a csrf token in a wiki for the first time:"
{ $code
"USING: mediawiki.api namespaces ;"