Notice that the { $snippet "1 [ * ] reduce" } part of the definition sort of makes sense on its own, being the product of a sequence. The nice thing about a concatenative language is that we can just factor this part out and write
{ $code ": prod ( {x1,...,xn} -- x1*...*xn ) 1 [ * ] reduce ;
+
: fact ( n -- n! ) [1..b] prod ;" }
$nl
Our definitions have become simpler and there was no need to pass parameters, rename local variables, or do anything
when you asked the listener to import the word { $link [1..b] } for you. You can also use more than one vocabulary at a time
with the word { $link POSTPONE: USING: } , which is followed by a list of vocabularies and terminated by { $link POSTPONE: ; } , like
-{ $code "USING: ranges sequences.deep ;" }
+{ $code "USING: ranges sequences ;" }
$nl
Finally, you define the vocabulary where your definitions are stored with the word { $link POSTPONE: IN: } . If you search the online
help for a word you have defined so far, like { $link prime? } , you will see that your definitions have been grouped under the
USING: kernel math.functions ranges sequences ;
IN: github.tutorial
" }
-Now that you have some words in your vocabulary, you can edit, say, the { $snippet "multiple?" } word with { $snippet "\ multiple? edit" } . You
+Now that you have some words in your vocabulary, you can edit, say, the { $snippet "multiple?" } word with { $snippet "\\ multiple? edit" } . You
will find your editor open on the relevant line of the right file. This also works for words in the Factor distribution,
although it may be a bad idea to modify them.
-This { $snippet "\\" } word requires a little explanation. It works like a sort of escape, allowing us to put a reference to the
+This { $link POSTPONE: \ } word requires a little explanation. It works like a sort of escape, allowing us to put a reference to the
next word on the stack, without executing it. This is exactly what we need, because { $link edit } is a word that takes words
themselves as arguments. This mechanism is similar to quotations, but while a quotation creates a new anonymous function,
here we are directly refering to the word { $snippet "multiple?" } .
{ $snippet "github.tutorial-tests" } ":"
{ $code "
-[ t ] [ 2 prime? ] unit-test
-[ t ] [ 13 prime? ] unit-test
-[ t ] [ 29 prime? ] unit-test
-[ f ] [ 15 prime? ] unit-test
-[ f ] [ 377 prime? ] unit-test
-[ f ] [ 1 prime? ] unit-test
-[ t ] [ 20750750228539 prime? ] unit-test
+{ t } [ 2 prime? ] unit-test
+{ t } [ 13 prime? ] unit-test
+{ t } [ 29 prime? ] unit-test
+{ f } [ 15 prime? ] unit-test
+{ f } [ 377 prime? ] unit-test
+{ f } [ 1 prime? ] unit-test
+{ t } [ 20750750228539 prime? ] unit-test
" }
$nl
You can now run the tests with { $snippet "\"github.tutorial\" test" } . You will see that we have actually made a mistake, and
pressing { $snippet "F3" } will show more details. It seems that our assertions fails for { $snippet "2" } .
-In fact, if you manually try to run our functions for { $snippet "2" } , you will see that our defition of { $snippet "[2..b]" } returns { $snippet "{ 2 }" }
+In fact, if you manually try to run our functions for { $snippet "2" } , you will see that our definition of { $snippet "[2..b]" } returns { $snippet "{ 2 }" }
for { $snippet "2 sqrt" } , due to the fact that the square root of two is less than two, so we get a descending interval. Try making a
fix so that the tests now pass.
-There are a few more words to test errors and inference of stack effects. { $link POSTPONE: unit-test } suffices for now, but later on
+There are a few more words to test errors and inference of stack effects. Using { $link POSTPONE: unit-test } suffices for now, but later on
you may want to check the main documentation on { $link "tools.test" } .
We can also add some documentation to our vocabulary. Autogenerated documentation is always available for user-defined
Although it is not apparent from what we have said so far, Factor has object-oriented features, and many core words
are actually method invocations. To better understand how objects behave in Factor, a quote is in order:
$nl
-{ $emphasis "I invented the term Object-Oriented and I can tell you I did not have C++ in mind.
+{ $emphasis "\"I invented the term Object-Oriented and I can tell you I did not have C++ in mind.\"
-Alan Kay" }
$nl
The term object-oriented has as many different meanings as people using it. One point of view - which was actually
For instance, we can create a new movie with
{ $code "
-movie new \"The prestige\" >>title
- \"Christopher Nolan\" >>director
- { \"Hugh Jackman\" \"Christian Bale\" \"Scarlett Johansson\" } >>actors
+movie new
+ \"The prestige\" >>title
+ \"Christopher Nolan\" >>director
+ { \"Hugh Jackman\" \"Christian Bale\" \"Scarlett Johansson\" } >>actors
" }
We can also shorten this to
{ $code "
TUPLE: band
- { keyboards string read-only }
- { guitar string read-only }
- { bass string read-only }
- { drums string read-only } ;
+ { keyboards string read-only }
+ { guitar string read-only }
+ { bass string read-only }
+ { drums string read-only } ;
+
: <band> ( keyboards guitar bass drums -- band ) band boa ;
" }
together with one instance
{ $code "
M: movie star actors>> first ;
+
M: band star bass>> ;
" }
If you write { $snippet "star ." } two times, you can see the different effect of calling a generic word on instances of different
{ $code "
TUPLE: trilogy first second third ;
+
: <trilogy> ( first second third -- trilogy ) trilogy boa ;
+
\"A new hope\" \"The Empire strikes back\" \"Return of the Jedi\" <trilogy>
\"George Lucas\" 2array
" }
As an example, we will define a literal for DNA sequences. A DNA sequence is a sequence of one of the bases cytosine,
guanine, adenine and thymine, which we will denote by the letters c, g, a, t. Since there are four possible bases, we
-can encode each with two bits. Let use define a word
+can encode each with two bits. Let use define a word that operates on characters:
{ $code "
: dna>bits ( token -- bits ) {
- { \"a\" [ { f f } ] }
- { \"c\" [ { t t } ] }
- { \"g\" [ { f t } ] }
- { \"t\" [ { t f } ] }
+ { CHAR: a [ { f f } ] }
+ { CHAR: c [ { t t } ] }
+ { CHAR: g [ { f t } ] }
+ { CHAR: t [ { t f } ] }
} case ;
" }
where the first bit represents whether the basis is a purine or a pyrimidine, and the second one identifies bases that
{ $code "
SYNTAX: DNA{ \"}\" parse-tokens concat
- [ 1string dna>bits ] { } map-as suffix! ;
+ [ dna>bits ] { } map-as suffix! ;
" }
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 "{ }" } .
{ $code "
SYNTAX: DNA{ \"}\" parse-tokens concat
- [ 1string dna>bits ] { } map-as
- concat >bit-array suffix! ;
+ [ dna>bits ] { } map-as
+ concat >bit-array suffix! ;
" }
If you try it with { $snippet "DNA{ a ccg t a g }" } you should get
{ $snippet \"?{ f f t t t t f t t f f f f t }\" }
" }
-Let us try an example from { $url "http://re-factor.blogspot.it/2014/06/swift-ranges.html" "Re-Factor" } ,
+Let us try an example from the { $url
+"http://re-factor.blogspot.com/2014/06/swift-ranges.html" "Re: Factor" } blog,
which adds infix syntax for ranges. Until now, we have used { $link [a..b] } to create a range. We can make a
syntax that is friendlier to people coming from other languages using { $snippet "..." } as an infix word.
In a sense, Factor syntax is completely flat, and parsing words allow you to introduce syntaxes more complex than a
stream of tokens to be used locally. This lets any programmer expand the language by adding these syntactic features in libraries
-. In principle, it would even be possible to have an external language compile to Factor - say JavaScript - and embed it
+. In principle, it would even be possible to have an external language compile to Factor -- say JavaScript -- and embed it
as a domain-specific language in the boundaries of a { $snippet "<JS ... JS>" } parsing word. Some taste is needed not to abuse too much of
this to introduce styles that are much too alien in the concatenative world.
;
instance, let us define a word to solve quadratic equations. I will spare you the purely stack-based version, and
present you a version with locals (this will require the { $vocab-link "locals" } vocabulary):
-{ $code "
- :: solveq ( a b c -- x )
- b neg
- b b * 4 a c * * - sqrt
- +
- 2 a * / ;
-" }
+{ $code ":: solveq ( a b c -- x )
+ b neg
+ b b * 4 a c * * - sqrt
+ +
+ 2 a * / ;" }
In this case we have chosen the + sign, but we can do better and output both solutions:
-{ $code "
- :: solveq ( a b c -- x1 x2 )
- b neg
- b b * 4 a c * * - sqrt
- [ + ] [ - ] 2bi
- [ 2 a * / ] bi@ ;
-" }
+{ $code ":: solveq ( a b c -- x1 x2 )
+ b neg
+ b b * 4 a c * * - sqrt
+ [ + ] [ - ] 2bi
+ [ 2 a * / ] bi@ ;" }
You can check that this definition works with something like { $snippet "2 -16 30 solveq" } , which should output both { $snippet "3.0" } and { $snippet "5.0" } .
Apart from being written in RPN style, our first version of { $snippet "solveq" } looks exactly the same it would in a language
with local variables. For the second definition, we apply both the { $link + } and { $link - } operations to -b and delta, using the
sort of partial application with a hole. It also curries a quotation, but uses the third element on the stack instead
of the second. Also, the resulting curried quotation will be applied to an element inserting it in the second position.
-The example from the documentation probably tells more than the above sentence: try writing { $snippet "1 { 1 2 3 } [ / ] with map" } .
+The example from the documentation probably tells more than the
+above sentence -- try writing:
+
+{ $code "1 { 1 2 3 } [ / ] with map" }
Let me take again { $snippet "prime?" } , but this time write it without using helper words:
For this purpose, Factor allows you to create { $strong "dynamic variables" } and bind them in scopes. The first thing is to create a { $strong "symbol" }
for a variable, say
-{ $code "
- SYMBOL: favorite-language
-" }
+{ $code "SYMBOL: favorite-language" }
Then one can use the word { $link set } to bind the variable and { $link get } to retrieve its values, like
-{ $code "
- \"Factor\" favorite-language set
- favorite-language get
-" }
+{ $code "\"Factor\" favorite-language set
+favorite-language get" }
Scopes are nested, and new scopes can be created with the word { $link with-scope } . Try for instance
-{ $code "
- : on-the-jvm ( -- ) [
- \"Scala\" favorite-language set
- favorite-language get .
- ] with-scope ;
-" }
+{ $code ": on-the-jvm ( -- )
+ [
+ \"Scala\" favorite-language set
+ favorite-language get .
+ ] with-scope ;" }
If you run { $snippet "on-the-jvm" } , { $snippet "\"Scala\"" } will be printed, but after execution, { $snippet "favorite-language get" } will hold { $snippet "\"Factor\"" } as its value.
All the tools that we have seen in this section should only be used when absolutely necessary, as they break concatenativity and make
" }
With this definition, we can make a word to read the first character of the first line:
-{ $code "
- : read-first-letters ( path -- )
- utf8 <file-reader> [
+{ $code ": read-first-letters ( path -- )
+ utf8 <file-reader> [
readln 1 safe-head write nl
- ] with-input-stream ;
-" }
+ ] with-input-stream ;" }
Using the helper word { $link with-file-reader } , we can also shorten this to
-{ $code "
- : read-first-letters ( path -- )
- utf8 [
+{ $code ": read-first-letters ( path -- )
+ utf8 [
readln 1 safe-head write nl
- ] with-file-reader ;
-" }
+ ] 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
-{ $code "
- : read-first-letters ( path -- )
- utf8 file-lines [ 1 safe-head write nl ] each ;
-" }
+{ $code ": read-first-letters ( path -- )
+ utf8 file-lines [ 1 safe-head write nl ] each ;" }
When the file is small, one can also use { $link file-contents } to read the whole contents of a file in a single string.
Factor defines many more words for input/output, which cover many more cases, such as binary files or sockets.
directory entries, you will see that the type is either { $link +directory+ } or { $link +regular-file+ } (well, there are symlinks as well,
but we will ignore them for simplicity). Hence we can define a word that lists files and directories with
-{ $code "
- : list-files-and-dirs ( path -- files dirs )
- directory-entries [ type>> +regular-file+ = ] partition ;
-" }
+{ $code ": list-files-and-dirs ( path -- files dirs )
+ directory-entries [ type>> +regular-file+ = ] partition ;" }
With this, we can define a word { $snippet "ls" } that will print directory contents as follows:
-{ $code "
- : ls ( path -- )
- list-files-and-dirs
- \"DIRECTORIES:\" write nl
- \"------------\" write nl
- [ name>> write nl ] each
- \"FILES:\" write nl
- \"------\" write nl
- [ name>> write nl ] each ;
-" }
+{ $code ": ls ( path -- )
+ list-files-and-dirs
+ \"DIRECTORIES:\" write nl
+ \"------------\" write nl
+ [ name>> write nl ] each
+ \"FILES:\" write nl
+ \"------\" write nl
+ [ name>> write nl ] each ;" }
Try the word on your home directory to see the effects. In the next section, we shall look at how to create an
executable for our simple program.
;
Start by creating our { $snippet "ls" } vocabulary with { $snippet "\"ls\" scaffold-work" } and make it look like this:
-{ $code "
- ! Copyright (C) 2014 Andrea Ferretti.
- ! See http://factorcode.org/license.txt for BSD license.
- USING: accessors command-line io io.directories io.files.types
- kernel namespaces sequences ;
- IN: ls
-
- <PRIVATE
-
- : list-files-and-dirs ( path -- files dirs )
- directory-entries [ type>> +regular-file+ = ] partition ;
-
- PRIVATE>
-
- : ls ( path -- )
- list-files-and-dirs
- \"DIRECTORIES:\" write nl
- \"------------\" write nl
- [ name>> write nl ] each
- \"FILES:\" write nl
- \"------\" write nl
- [ name>> write nl ] each ;
-" }
+{ $code "\
+! Copyright (C) 2014 Andrea Ferretti.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors command-line io io.directories io.files.types
+ kernel namespaces sequences ;
+IN: ls
+
+<PRIVATE
+
+: list-files-and-dirs ( path -- files dirs )
+ directory-entries [ type>> +regular-file+ = ] partition ;
+
+PRIVATE>
+
+: ls ( path -- )
+ list-files-and-dirs
+ \"DIRECTORIES:\" write nl
+ \"------------\" write nl
+ [ name>> write nl ] each
+ \"FILES:\" write nl
+ \"------\" write nl
+ [ name>> write nl ] each ;" }
When we run our vocabulary, we will need to read arguments from the command line. Command-line arguments are stored
under the { $link command-line } dynamic variable, which holds an array of strings. Hence - forgetting any error checking - we can
define a word which runs { $snippet "ls" } on the first command-line argument with
-{ $code "
- : ls-run ( -- ) command-line get first ls ;
-" }
+{ $code ": ls-run ( -- ) command-line get first ls ;" }
Finally, we use the word { $link POSTPONE: MAIN: } to declare the main word of our vocabulary:
-{ $code "
- MAIN: ls-run
-" }
+{ $code "MAIN: ls-run" }
Having added those two lines to your vocabulary, you are now ready to run it. The simplest way is to run the
vocabulary as a script with the { $snippet "-run" } flag passed to Factor. For instance to list the contents of my home I can do
-{ $code "
- ./factor -run=ls /home/andrea
-" }
+{ $code "$ ./factor -run=ls /home/andrea" }
In order to produce an executable, we must set some options and call the { $snippet "deploy" } word. The simplest way to do this
graphically is to invoke the { $link deploy-tool } word. If you write { $snippet "\"ls\" deploy-tool" } , you will be presented with a window to
choose deployment options. For our simple case, we will leave the default options and choose Deploy.
After a little while, you should be presented with an executable that you can run like
-{ $code "
- cd ls
- ./ls /home/andrea
-" }
+{ $code "$ cd ls
+
+$ ./ls /home/andrea" }
Try making the { $snippet "ls" } program more robust by handling missing command-line arguments and non-existent or non-directory
arguments.
;
ARTICLE: "tour-multithreading" "Multithreading"
-As we have said, the Factor runtime is single-threaded, like Node. Still, one can emulate concurrency in a single-
-threaded setting by making use of { $strong "coroutines" } . These are essentially cooperative threads, which periodically release
+As we have said, the Factor runtime is single-threaded, like Node. Still, one can emulate concurrency in a single-threaded
+setting by making use of { $strong "coroutines" } . These are essentially cooperative threads, which periodically release
control with the { $link yield } word, so that the scheduler can decide which coroutine to run next.
Although cooperative threads do not allow to make use of multiple cores, they still have some benefits:
We will spawn 18 threads, each one printing a line. The operation that a thread must run amounts to
-{ $code "
-star-wars get ?nth print
-" }
+{ $code "star-wars get ?nth print" }
Note that dynamic variables are shared between threads, so each one has access to star-wars. This is fine, since it is
read-only, but the usual caveats about shared memory in a multithreaded settings apply.
Let us define a word for the thread workload
-{ $code "
-: print-a-line ( i -- ) star-wars get ?nth print ;
-" }
+{ $code ": print-a-line ( i -- ) star-wars get ?nth print ;" }
If we give the i-th thread the name "\"i\"" , our example amounts to
-{ $code "
-18 [0..b) [
- [ [ print-a-line ] curry ]
- [ number>string ]
- bi spawn
-] each
-" }
+{ $code "18 [0..b) [
+ [ [ print-a-line ] curry ]
+ [ number>string ]
+ bi spawn
+] each" }
Note the use of { $link curry } to send i to the quotation that prints the i-th line. This is almost what we want, but it runs
too fast. We need to put the thread to sleep for a while. So we { $link clear } the stack that now contains a lot of thread
It turns out that { $link sleep } does exactly what we need, but it takes a { $strong "duration" } object as input. We can create a
duration of i seconds with... well { $snippet "i seconds" } . So we define
-{ $code "
-: wait-and-print ( i -- ) dup seconds sleep print-a-line ;
-" }
+{ $code ": wait-and-print ( i -- ) dup seconds sleep print-a-line ;" }
Let us try
-{ $code "
-18 [0..b) [
- [ [ wait-and-print ] curry ]
- [ number>string ]
- bi spawn
-] each
-" }
+{ $code "18 [0..b) [
+ [ [ wait-and-print ] curry ]
+ [ number>string ]
+ bi spawn
+] each" }
Instead of { $link spawn } , we can also use { $link in-thread } which uses a dummy thread name and discards the returned thread,
simplifying the above to
-{ $code "
-18 [0..b) [
- [ wait-and-print ] curry in-thread
-] each
-" }
+{ $code "18 [0..b) [
+ [ wait-and-print ] curry in-thread
+] each" }
$nl
-In serious applications theads will be long-running. In order to make them
+In serious applications threads will be long-running. In order to make them
cooperate, one can use the { $link yield } word to signal that the thread has done a unit of work, and other threads can gain
control. You also may want to have a look at other words to { $link stop } , { $link suspend } or { $link resume } threads.
;
request to an HTTP response, but more concretely it is anything that implements the method { $snippet "call-responder*" } . Responses are
instances of the tuple { $link response } , so are usually generated calling { $link <response> } and customizing a few slots. Let us
write a simple echo responder:
-{ $code "
+{ $code "TUPLE: echo-responder ;
- TUPLE: echo-responder ;
+: <echo-responder> ( -- responder ) echo-responder new ;
- : <echo-responder> ( -- responder ) echo-responder new ;
-
- M: echo-responder call-responder*
- drop
- <response>
+M: echo-responder call-responder*
+ drop
+ <response>
200 >>code
\"Document follows\" >>message
\"text/plain\" >>content-type
- swap concat >>body ;
-
-" }
+ swap concat >>body ;" }
Responders are usually combined to form more complex responders in order to implement routing and other features. In
our simplistic example, we will use just this one responder, and set it globally with
-{ $code "
-
- <echo-responder> main-responder set-global
-
-" }
+{ $code "<echo-responder> main-responder set-global" }
Once you have done this, you can start the server with { $snippet "8080 httpd" } . You can then visit { $url "http://localhost:8080/hello/%20/from/%20/factor" }
in your browser to see your first responder in action. You can then stop the server with { $link stop-server } .
Let use first investigate a simple example of routing. To do this, we create a special type of responder called a { $strong "dispatcher" } , that dispatches requests based on path parameters. Let us create a simple dispatcher that will choose
between our echo responder and a default responder used to serve static files.
-{ $code "
-
- dispatcher new-dispatcher
- <echo-responder> \"echo\" add-responder
- \"/home/andrea\" <static> \"home\" add-responder
- main-responder set-global
-
-" }
+{ $code "dispatcher new-dispatcher
+ <echo-responder> \"echo\" add-responder
+ \"/home/andrea\" <static> \"home\" add-responder
+ main-responder set-global" }
Of course, substitute the path { $snippet "/home/andrea" } with any folder you like. If you start again the server with { $snippet "8080 httpd" }
, you should be able to see both our simple echo responder (under { $snippet "/echo" } ) and the contents of your files (under { $snippet "/home" } ).
Now that you know how to do routing, we can write page actions in Chloe. Things are starting to become complicated, so
we scaffold a vocabulary with { $snippet "\"hello-furnace\" scaffold-work" } . Make it look like this:
-{ $code "
-
- ! Copyright (C) 2014 Andrea Ferretti.
- ! See http://factorcode.org/license.txt for BSD license.
- USING: accessors furnace.actions http http.server
- http.server.dispatchers http.server.static kernel sequences ;
- IN: hello-furnace
+{ $code "\
+! Copyright (C) 2014 Andrea Ferretti.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors furnace.actions http http.server
+ http.server.dispatchers http.server.static kernel sequences ;
+IN: hello-furnace
- TUPLE: echo-responder ;
+TUPLE: echo-responder ;
- : <echo-responder> ( -- responder ) echo-responder new ;
+: <echo-responder> ( -- responder ) echo-responder new ;
- M: echo-responder call-responder*
- drop
- <response>
+M: echo-responder call-responder*
+ drop
+ <response>
200 >>code
\"Document follows\" >>message
\"text/plain\" >>content-type
swap concat >>body ;
- TUPLE: hello-dispatcher < dispatcher ;
+TUPLE: hello-dispatcher < dispatcher ;
- : <example-responder> ( -- responder )
- hello-dispatcher new-dispatcher
+: <example-responder> ( -- responder )
+ hello-dispatcher new-dispatcher
<echo-responder> \"echo\" add-responder
\"/home/andrea\" <static> \"home\" add-responder
<page-action>
- { hello-dispatcher \"greetings\" } >>template
- \"chloe\" add-responder ;
-
-" }
+ { hello-dispatcher \"greetings\" } >>template
+ \"chloe\" add-responder ;" }
Most things are the same as we have done in the listener. The only difference is that we have added a third responder
in our dispatcher, under { $snippet "chloe" } . This responder is created with a page action. The page action has many slots - say, to
dispatcher class and the relative path of the template file.
In order for all this to work, create a file { $snippet "work/hello-furnace/greetings.xml" } with a content like
-{ $code "
-
- <?xml version='1.0' ?>
-
- <t:chloe xmlns:t=\"http://factorcode.org/chloe/1.0\">
- <p>Hello from Chloe</p>
- </t:chloe>
+{ $code "<?xml version='1.0' ?>
-" }
+<t:chloe xmlns:t=\"http://factorcode.org/chloe/1.0\">
+ <p>Hello from Chloe</p>
+</t:chloe>" }
Reload the { $snippet "hello-furnace" } vocabulary and { $snippet "<example-responder> main-responder set-global" } . You should be able to see
the results of your efforts under { $url "http://localhost:8080/chloe" } . Notice that there was no need to restart the server, we
CSP model, based on the use of { $strong "channels" } .
As a warm-up, we will make a simple example of communication between threads in the same process.
-{ $code "
-
- FROM: concurrency.messaging => send receive ;
-
-" }
+{ $code "FROM: concurrency.messaging => send receive ;" }
We can start a thread that will receive a message and print it repeatedly:
-{ $code "
+{ $code ": print-repeatedly ( -- ) receive . print-repeatedly ;
- : print-repeatedly ( -- ) receive . print-repeatedly ;
- [ print-repeatedly ] \"printer\" spawn
-
-" }
+[ print-repeatedly ] \"printer\" spawn" }
A thread whose quotation starts with { $link receive } and calls itself recursively behaves like an actor in Erlang or Akka.
We can then use { $link send } to send messages to it. Try { $snippet "\"hello\" over send" } and then { $snippet "\"threading\" over send" } .
blocking: { $link to } will block until the value is read in a different thread, and { $link from } will block until a value is available.
We create a channel and give it a name with
-{ $code "
-
- SYMBOL: ch
- <channel> ch set
+{ $code "SYMBOL: ch
-" }
+<channel> ch set" }
Then we write to it in a separate thread, in order not to block the UI
-{ $code "
-
- [ \"hello\" ch get to ] in-thread
-
-" }
+{ $code "[ \"hello\" ch get to ] in-thread" }
We can then read the value in the UI with
-{ $code "
-
- ch get from
-
-" }
+{ $code "ch get from" }
We can also invert the order:
-{ $code "
+{ $code "[ ch get from . ] in-thread
- [ ch get from . ] in-thread
- \"hello\" ch get to
-
-" }
+\"hello\" ch get to" }
This works fine, since we had set the reader first.
Now, for the interesting part: we will start a second Factor instance and communicate via message sending. Factor
Start another instance of Factor, and run a node server on it. We will use the word { $link <inet4> } , that creates an IPv4
address from a host and a port, and the { $link <node-server> } constructor
-{ $code "
+{ $code "USE: concurrency.distributed
- USE: concurrency.distributed
- f 9000 <inet4> <node-server> start-server
-
-" }
+f 9000 <inet4> <node-server> start-server" }
Here we have used { $link f } as host, which just stands for localhost. We will also start a thread that keeps a running count
of the numbers it has received.
-{ $code "
+{ $code "FROM: concurrency.messaging => send receive ;
- FROM: concurrency.messaging => send receive ;
- : add ( x -- y ) receive + dup . add ;
- [ 0 add ] \"adder\" spawn
+: add ( x -- y ) receive + dup . add ;
-" }
+[ 0 add ] \"adder\" spawn" }
Once we have started the server, we can make a thread available with { $link register-remote-thread } ":"
-{ $code "
-
- dup name>> register-remote-thread
-
-" }
+{ $code "dup name>> register-remote-thread" }
Now we switch to the other instance of Factor. Here we will receive a reference to the remote thread and start sending
numbers to it. The address of a thread is just the address of its server and the name we have registered the thread with
, so we obtain a reference to our adder thread with
-{ $code "
-
- f 9000 <inet4> \"adder\" <remote-thread>
-
-" }
+{ $code "f 9000 <inet4> \"adder\" <remote-thread>" }
Now, we reimport { $link send } just to be sure (there is an overlap with a word having the same name in { $vocab-link "io.sockets" } , that we
have imported)
-{ $code "
-
- FROM: concurrency.messaging => send receive ;
-
-" }
+{ $code "FROM: concurrency.messaging => send receive ;" }
and we can start sending numbers to it. Try { $snippet "3 over send" } , and then { $snippet "8 over send" } - you should see the running total
printed in the other Factor instance.
What about channels? We go back to our server, and start a channel there, just as above. This time, though, we { $link publish } it to make it available remotely:
-{ $code "
-
- USING: channels channels.remote ;
- <channel> dup publish
+{ $code "USING: channels channels.remote ;
-" }
+<channel> dup publish" }
What you get in return is an id you can use remotely to communicate. For instance, I just got
{ $snippet "72581372615274718877979307388951222312843084896785643440879198703359628058956" } (yes, they really want to be sure it is unique!).
We will wait on this channel, thereby blocking the UI:
-{ $code "
-
- swap from .
-
-" }
+{ $code "swap from ." }
In the other Factor instance we use the id to get a reference to the remote channel and write to it
-{ $code "
-
- f 9000 <inet4> 72581372615274718877979307388951222312843084896785643440879198703359628058956 <remote-channel>
- \"Hello, channels\" over to
-
-" }
+{ $code "\
+f 9000 <inet4> 72581372615274718877979307388951222312843084896785643440879198703359628058956 <remote-channel>
+\"Hello, channels\" over to" }
In the server instance, the message should be printed.
Remote channels and threads are both useful to implement distributed applications and make good use of multicore
"The community is small. It is difficult to find information about Factor on the internet.
However, you can help with this by posting questions on Stack Overflow under the [factor] tag."
"The concatenative model is very powerful, but also hard to get good at."
-"Factor lacks threads: although the distributed processes make up for it, they incur some cost in serialization."
+"Factor lacks native threads: although the distributed processes make up for it, they incur some cost in serialization."
"Factor does not currently have a package manager. Most prominent packages are part of the main Factor distribution."
}
The Factor source tree is massive, so here's a few vocabularies to get you started off:
These vocabularies are a testament to the power and expressivity of Factor, and we hope that they
help you make something you like. Happy hacking!
$nl
-{ $code "
+{ $code "\
USE: images.http
-\"http://factorcode.org/logo.png\" http-image.
-" }
+
+\"http://factorcode.org/logo.png\" http-image." }
;
-ARTICLE: "tour" "A guided tour of Factor"
+ARTICLE: "tour" "Guided tour of Factor"
Factor is a mature, dynamically typed language based on the concatenative paradigm. Getting started with Factor can be daunting
since the concatenative paradigm is different from most mainstream languages.
This tutorial will: