2005-07-19 17:56:22 -04:00
IN: help
2005-09-01 01:20:43 -04:00
USING: gadgets gadgets-books gadgets-borders gadgets-buttons
2005-12-22 21:44:15 -05:00
gadgets-labels gadgets-panes io kernel namespaces sequences ;
2005-07-20 01:16:33 -04:00
2005-10-25 01:31:54 -04:00
: page-theme
2005-10-29 23:25:38 -04:00
T{ gradient f { { 0.8 0.8 1.0 1.0 } { 1.0 0.8 1.0 1.0 } } }
2005-10-27 17:21:06 -04:00
swap set-gadget-interior ;
2005-10-25 01:31:54 -04:00
2005-12-22 21:44:15 -05:00
: <page> ( markup -- gadget )
[ default-style [ print-element ] with-style ] make-pane
2005-12-17 20:03:41 -05:00
dup page-theme <default-border> ;
2005-07-18 18:14:13 -04:00
: tutorial-pages
2005-10-29 23:25:38 -04:00
{
{
2005-12-22 21:44:15 -05:00
{ $heading "Factor: a dynamic language" }
2005-07-18 18:14:13 -04:00
"This series of slides presents a quick overview of Factor."
2005-12-22 21:44:15 -05:00
"Factor is interactive, which means you can test out the code in this tutorial immediately."
"Code examples will insert themselves in the listener's input area when clicked:"
{ $code "\"hello world\" print" }
2005-07-20 01:16:33 -04:00
"You can then press ENTER to execute the code, or edit it first."
2005-12-22 21:44:15 -05:00
{ $url "http://factorcode.org" }
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "The view from 10,000 feet" }
2005-07-18 18:14:13 -04:00
"- Everything is an object"
"- A word is a basic unit of code"
"- Words are identified by names, and organized in vocabularies"
"- Words pass parameters on the stack"
"- Code blocks can be passed as parameters to words"
"- Word definitions are very short with very high code reuse"
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Basic syntax" }
"Factor code is made up of whitespace-speparated tokens. Recall the example from the first slide:"
{ $code "\"hello world\" print" }
2005-07-18 18:14:13 -04:00
"The first token (\"hello world\") is a string."
"The second token (print) is a word."
"The string is pushed on the stack, and the print word prints it."
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "The stack" }
2005-07-18 18:14:13 -04:00
"- The stack is like a pile of papers."
"- You can ``push'' papers on the top of the pile,"
" and ``pop'' papers from the top of the pile."
"Here is another code example:"
2005-12-22 21:44:15 -05:00
{ $code "2 3 + ." }
2005-07-18 18:14:13 -04:00
"Try running it in the listener now."
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Postfix arithmetic" }
"What happened when you ran it? The two numbers (2 3) are pushed on the stack. Then, the + word pops them and pushes the result (5). Then, the . word prints this result."
2005-07-18 18:14:13 -04:00
"This is called postfix arithmetic."
"Traditional arithmetic is called infix: 3 + (6 * 2)"
"Lets translate this into postfix: 3 6 2 * + ."
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Colon definitions" }
2005-07-18 18:14:13 -04:00
"We can define new words in terms of existing words."
2005-12-22 21:44:15 -05:00
{ $code ": twice 2 * ;" }
"This defines a new word named ``twice'' that calls ``2 *''. Try the following in the listener:"
{ $code "3 twice twice ." }
2005-07-18 18:14:13 -04:00
"The result is the same as if you wrote:"
2005-12-22 21:44:15 -05:00
{ $code "3 2 * 2 * ." }
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Stack effects" }
"When we look at the definition of the ``twice'' word, it is intuitively obvious that it takes one value from the stack, and leaves one value behind. However, with more complex definitions, it is better to document this so-called ``stack effect''."
"A stack effect comment is written between ( and ). Factor ignores stack effect comments. Don't you!"
2005-07-18 18:14:13 -04:00
"The stack effect of twice is ( x -- 2*x )."
"The stack effect of + is ( x y -- x+y )."
"The stack effect of . is ( object -- )."
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Reading user input" }
"User input is read using the readln ( -- string ) word. Note its stack effect; it puts a string on the stack."
2005-07-21 23:36:40 -04:00
"This program will ask your name, then greet you:"
2005-12-22 21:44:15 -05:00
{ $code "\"What is your name?\" print\nreadln \"Hello, \" write print" }
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Shuffle words" }
"The word ``twice'' we defined is useless. Let's try something more useful: squaring a number."
"We want a word with stack effect ( n -- n*n ). We cannot use * by itself, since its stack effect is ( x y -- x*y ); it expects two inputs."
"However, we can use the word ``dup''. It has stack effect ( object -- object object ), and it does exactly what we need. The ``dup'' word is known as a shuffle word."
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "The squared word" }
2005-07-18 18:14:13 -04:00
"Try entering the following word definition:"
2005-12-22 21:44:15 -05:00
{ $code ": square ( n -- n*n ) dup * ;" }
"Shuffle words solve the problem where we need to compose two words, but their stack effects do not ``fit''."
2005-07-18 18:14:13 -04:00
"Some of the most commonly-used shuffle words:"
2005-12-22 21:44:15 -05:00
{ $code "drop ( object -- )\nswap ( obj1 obj2 -- obj2 obj1 )\nover ( obj1 obj2 -- obj1 obj2 obj1 )" }
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Another shuffle example" }
2005-07-18 18:14:13 -04:00
"Now let us write a word that negates a number."
"Start by entering the following in the listener"
2005-12-22 21:44:15 -05:00
{ $code "0 10 - ." }
2005-07-18 18:14:13 -04:00
"It will print -10, as expected. Now notice that this the same as:"
2005-12-22 21:44:15 -05:00
{ $code "10 0 swap - ." }
2005-07-18 18:14:13 -04:00
"So indeed, we can factor out the definition ``0 swap -'':"
2005-12-22 21:44:15 -05:00
{ $code ": negate ( n -- -n ) 0 swap - ;" }
} {
{ $heading "Seeing words" }
"If you have entered every definition in this tutorial, you will now have several new colon definitions:"
{ $code "twice\nsquare\nnegate" }
"You can look at previously-entered word definitions using 'see'. Try the following:"
{ $code "\\ negate see" }
"Prefixing a word with \\ pushes it on the stack, instead of executing it. So the see word has stack effect ( word -- )."
} {
{ $heading "Branches" }
"Now suppose we want to write a word that computes the absolute value of a number; that is, if it is less than 0, the number will be negated to yield a positive result."
{ $code ": absolute ( x -- |x| ) dup 0 < [ negate ] when ;" }
"If the top of the stack is negative, the word negates it again, making it positive. The < ( x y -- x<y ) word outputs a boolean. In Factor, any object can be used as a truth value."
2005-08-26 01:44:12 -04:00
"- The f object is false."
"- Anything else is true."
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "More branches" }
2005-07-18 18:14:13 -04:00
"On the previous slide, you saw the 'when' conditional:"
2005-12-22 21:44:15 -05:00
{ $code " ... condition ... [ ... true case ... ] when" }
2005-07-18 18:14:13 -04:00
"Another commonly-used form is 'unless':"
2005-12-22 21:44:15 -05:00
{ $code " ... condition ... [ ... false case ... ] unless" }
2005-09-24 15:21:17 -04:00
"The 'if' conditional takes action on both branches:"
2005-12-22 21:44:15 -05:00
{ $code " ... condition ... [ ... ] [ ... ] if" }
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Combinators" }
2005-09-24 15:21:17 -04:00
"if, when, unless are words that take lists of code as input."
2005-12-22 21:44:15 -05:00
"Lists of code are called ``quotations''. Words that take quotations are called ``combinators''."
"Another combinator is times ( n quot -- ). It calls a quotation n times."
2005-07-18 18:14:13 -04:00
"Try this:"
2005-12-22 21:44:15 -05:00
{ $code "10 [ \"Hello combinators\" print ] times" }
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Sequences" }
2005-07-18 18:14:13 -04:00
"You have already seen strings, very briefly:"
" \"Hello world\""
2005-12-22 21:44:15 -05:00
"Strings are part of a class of objects called sequences. Two other types of sequences you will use a lot are:"
"Lists: [ 1 3 \"hi\" 10 2 ]"
"Arrays: { \"the\" { \"quick\" \"brown\" } \"fox\" }"
"As you can see in the second example, lists and arrays can contain any type of object, including other lists and arrays."
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Sequences and combinators" }
"A very useful combinator is each ( seq quot -- ). It calls a quotation with each element of the sequence in turn."
2005-07-18 18:14:13 -04:00
"Try this:"
2005-12-22 21:44:15 -05:00
{ $code "{ 10 20 30 } [ . ] each" }
"A closely-related combinator is map ( seq quot -- seq ). It also calls a quotation with each element."
"However, it then collects the outputs of the quotation into a new sequence."
2005-07-18 18:14:13 -04:00
"Try this:"
2005-12-22 21:44:15 -05:00
{ $code "{ 10 20 30 } [ 3 + ] map ." }
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Numbers - integers and ratios" }
"Factor supports arbitrary-precision integers and ratios."
2005-07-18 18:14:13 -04:00
"Try the following:"
2005-12-22 21:44:15 -05:00
{ $code ": factorial ( n -- n! ) 1 swap <range> product ;\n100 factorial .\n1 3 / 1 2 / + ." }
"Rational numbers are added, multiplied and reduced to lowest terms in the same way you learned in grade school."
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Object oriented programming" }
"Each object belongs to a class. Generic words act differently based on an object's class."
{ $code "GENERIC: describe ( object -- )\nM: integer describe \"The integer \" write . ;\nM: string describe \"The string \" write . ;\nM: object describe drop \"Unknown object\" print ;" }
"Each M: line defines a ``method.'' Method definitions may appear in independent source files. Examples of built-in classes are integer, string, and object."
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Defining new classes" }
2005-07-18 18:14:13 -04:00
"New classes can be defined:"
2005-12-22 21:44:15 -05:00
{ $code "TUPLE: point x y ;\nM: point describe\n \"x =\" write dup point-x .\n \"y =\" write point-y . ;\n100 200 <point> describe" }
"A tuple is a collection of named slots. Tuples support custom constructors, delegation... see the developer's handbook for details."
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "The library" }
2005-07-18 18:14:13 -04:00
"Offers a good selection of highly-reusable words:"
"- Operations on sequences"
"- Variety of mathematical functions"
"- Web server and web application framework"
"- Graphical user interface framework"
"Browsing the library:"
"- To list all vocabularies:"
2005-12-22 21:44:15 -05:00
{ $code "vocabs." }
2005-07-18 18:14:13 -04:00
"- To list all words in a vocabulary:"
2005-12-22 21:44:15 -05:00
{ $code "\"sequences\" words." }
2005-07-18 18:14:13 -04:00
"- To show a word definition:"
2005-12-22 21:44:15 -05:00
{ $code "\\ reverse see" }
2005-10-29 23:25:38 -04:00
} {
2005-12-22 21:44:15 -05:00
{ $heading "Learning more" }
2005-07-18 18:14:13 -04:00
"Hopefully this tutorial has sparked your interest in Factor."
"You can learn more by reading the Factor developer's handbook:"
2005-12-22 21:44:15 -05:00
{ $url "http://factorcode.org/handbook.pdf" }
"Also, point your IRC client to irc.freenode.net and hop in the #concatenative channel to chat with other Factor geeks."
2005-10-29 23:25:38 -04:00
}
} ;
2005-07-18 18:14:13 -04:00
2005-09-01 16:45:36 -04:00
: <tutorial> ( pages -- browser )
2005-10-25 01:31:54 -04:00
tutorial-pages [ <page> ] map <book> <book-browser> ;
2005-07-18 18:14:13 -04:00
2005-07-19 04:23:33 -04:00
: tutorial ( -- )
2005-07-20 18:04:29 -04:00
<tutorial> gadget. ;
2005-08-26 00:55:56 -04:00
: <tutorial-button>
2005-08-26 21:42:43 -04:00
"Tutorial" <label>
2005-10-25 21:52:26 -04:00
[ drop [ tutorial ] pane get pane-call ] <bevel-button> ;