]> gitweb.factorcode.org Git - factor.git/blob - basis/help/tutorial/tutorial.factor
unicode: make this the API for all unicode things.
[factor.git] / basis / help / tutorial / tutorial.factor
1 USING: help.markup help.syntax ui.commands ui.operations
2 editors vocabs.loader kernel sequences prettyprint tools.test
3 vocabs.refresh strings unicode ui.tools.browser ui.tools.common ;
4 IN: help.tutorial
5
6 ARTICLE: "first-program-start" "Creating a vocabulary for your first program"
7 "Factor source code is organized into " { $link "vocabularies" } ". Before we can write our first program, we must create a vocabulary for it."
8 $nl
9 "Start by loading the scaffold tool:"
10 { $code "USE: tools.scaffold" }
11 $nl
12 "Then, ask the scaffold tool to create a new vocabulary named " { $snippet "palindrome" } ":"
13 { $code "\"palindrome\" scaffold-work" }
14 $nl
15 "If you look at the output, you will see that a few files were created in your “work” directory, and that the new source file was loaded."
16 $nl
17 "The following phrase will print the full path of your work directory:"
18 { $code "\"work\" resource-path ." }
19 $nl
20 "The work directory is one of several " { $link "vocabs.roots" } " where Factor searches for vocabularies. It is possible to define new vocabulary roots; see " { $link "add-vocab-roots" } ". To keep things simple in this tutorial, we'll just use the work directory, though."
21 $nl
22 "Open the work directory in your file manager, and open the subdirectory named " { $snippet "palindrome" } ". Inside this subdirectory you will see a file named " { $snippet "palindrome.factor" } ". Open this file in your text editor."
23 $nl
24 "You are now ready to go on to the next section: " { $link "first-program-logic" } "." ;
25
26 ARTICLE: "first-program-logic" "Writing some logic in your first program"
27 "The Factor workflow is to edit source code on disk and then to refresh the live image. Let's examine the file that we just created with the scaffold tool."
28 $nl
29 "Your " { $snippet "palindrome.factor" } " file should look like the following after the previous section:"
30 { $code
31     "! Copyright (C) 2012 Your name."
32     "! See http://factorcode.org/license.txt for BSD license."
33     "USING: ;"
34     "IN: palindrome"
35 }
36 $nl
37 "Notice that the file ends with an " { $link POSTPONE: IN: } " form telling Factor that all definitions in this source file should go into the " { $snippet "palindrome" } " vocabulary using the " { $link POSTPONE: IN: } " word. We will be adding new definitions after the " { $link POSTPONE: IN: } " form."
38 $nl
39 "In order to be able to call the words defined in the " { $snippet "palindrome" } " vocabulary, you need to issue the following command in the listener:"
40 { $code "USE: palindrome" }
41 $nl
42 "Now, we will be making some additions to the file. Since the file was loaded by the scaffold tool in the previous step, you need to tell Factor to reload it if it changes. Factor has a handy feature for this; pressing " { $command tool "common" refresh-all } " in the listener window will reload any changed source files. You can also force a single vocabulary to reload, in case the refresh feature does not pick up changes from disk:"
43 { $code "\"palindrome\" reload" }
44 $nl
45 "We will now write our first word using " { $link POSTPONE: : } ". This word will test if a string is a palindrome; it will take a string as input, and give back a boolean as output. We will call this word " { $snippet "palindrome?" } ", following a naming convention that words returning booleans have names ending with " { $snippet "?" } "."
46 $nl
47 "Recall that a string is a palindrome if it is spelled the same forwards or backwards; that is, if the string is equal to its reverse. We can express this in Factor as follows:"
48 { $code ": palindrome? ( string -- ? ) dup reverse = ;" }
49 "Place this definition at the end of your source file."
50 $nl
51 "Now we have changed the source file, we must reload it into Factor so that we can test the new definition. To do this, simply go to the Factor listener and press " { $command tool "common" refresh-all } ". This will find any previously-loaded source files which have changed on disk, and reload them."
52 $nl
53 "When you do this, you will get an error about the " { $link dup } " word not being found. This is because this word is part of the " { $vocab-link "kernel" } " vocabulary, but this vocabulary is not part of the source file's " { $link "word-search" } ". You must explicitly list dependencies in source files. This allows Factor to automatically load required vocabularies and makes larger programs easier to maintain."
54 $nl
55 "To add the word to the search path, first convince yourself that this word is in the " { $vocab-link "kernel" } " vocabulary. Enter " { $snippet "dup" } " in the listener's input area, and press " { $operation com-browse } ". This will open the documentation browser tool, viewing the help for the " { $link dup } " word. One of the subheadings in the help article will mention the word's vocabulary."
56 $nl
57 "Go back to the third line in your source file and change it to:"
58 { $code "USING: kernel ;" }
59 $nl
60 "Next, find out what vocabulary " { $link reverse } " lives in; type the word name " { $snippet "reverse" } " in the listener's input area, and press " { $operation com-browse } "."
61 $nl
62 "It lives in the " { $vocab-link "sequences" } " vocabulary, so we add that to the search path:"
63 { $code "USING: kernel sequences ;" }
64 $nl
65 "Finally, check what vocabulary " { $link = } " lives in, and confirm that it's in the " { $vocab-link "kernel" } " vocabulary, which we've already added to the search path."
66 $nl
67 "Now press " { $command tool "common" refresh-all } " again, and the source file should reload without any errors. You can now go on and learn about " { $link "first-program-test" } "." ;
68
69 ARTICLE: "first-program-test" "Testing your first program"
70 "Your " { $snippet "palindrome.factor" } " file should look like the following after the previous section:"
71 { $code
72     "! Copyright (C) 2012 Your name."
73     "! See http://factorcode.org/license.txt for BSD license."
74     "USING: kernel sequences ;"
75     "IN: palindrome"
76     ""
77     ": palindrome? ( string -- ? ) dup reverse = ;"
78 }
79 $nl
80 "We will now test our new word in the listener. If you haven't done so already, add the palindrome vocabulary to the listener's vocabulary search path:"
81 { $code "USE: palindrome" }
82 $nl
83 "Next, push a string on the stack (by surrounding text with quotes in the listener and then hitting " { $snippet "ENTER" } "):"
84 { $code "\"hello\"" }
85 $nl
86 "Note that the stack display in the listener now shows this string. Having supplied the input, we call our word:"
87 { $code "palindrome?" }
88 $nl
89 "The stack display should now have a boolean false - " { $link f } " - which is the word's output. Since “hello” is not a palindrome, this is what we expect. We can get rid of this boolean by calling " { $link drop } ". The stack should be empty after this is done."
90 $nl
91 "Now, let's try it with a palindrome; we will push the string and call the word in the same line of code:"
92 { $code "\"racecar\" palindrome?" }
93 $nl
94 "The stack should now contain a boolean true - " { $link t } ". We can print it and drop it using the " { $link . } " word:"
95 { $code "." }
96 $nl
97 "What we just did is called " { $emphasis "interactive testing" } ". A more advanced technique which comes into play with larger programs is " { $link "tools.test" } "."
98 $nl
99 "Create a test harness file using the scaffold tool:"
100 { $code "\"palindrome\" scaffold-tests" }
101 $nl
102 "Now, open the file named " { $snippet "palindrome-tests.factor" } "; it is located in the same directory as " { $snippet "palindrome.factor" } ", and it was created by the scaffold tool."
103 $nl
104 "We will add some unit tests, which are similar to the interactive tests we did above. Unit tests are defined with the " { $link POSTPONE: unit-test } " word, which takes a sequence of expected outputs, and a piece of code. It runs the code, and asserts that it outputs the expected values."
105 $nl
106 "Add the following two lines to " { $snippet "palindrome-tests.factor" } ":"
107 { $code
108     "[ f ] [ \"hello\" palindrome? ] unit-test"
109     "[ t ] [ \"racecar\" palindrome? ] unit-test"
110 }
111 $nl
112 "Now, you can run unit tests:"
113 { $code "\"palindrome\" test" }
114 $nl
115 "It should report that all your tests have been run and there were no test failures, displaying the following output:"
116 $nl
117 { $snippet
118     "Unit Test: { [ f ] [ \"hello\" palindrome? ] }"
119     "\n"
120     "Unit Test: { [ t ] [ \"racecar\" palindrome? ] }"
121 }
122 $nl
123 "Now you can read about " { $link "first-program-extend" } "." ;
124
125 ARTICLE: "first-program-extend" "Extending your first program"
126 "Our palindrome program works well, however we'd like to extend it to ignore spaces and non-alphabetical characters in the input."
127 $nl
128 "For example, we'd like it to identify the following as a palindrome:"
129 { $code "\"A man, a plan, a canal: Panama.\"" }
130 $nl
131 "However, right now, the simplistic algorithm we use says this is not a palindrome:"
132 { $unchecked-example "\"A man, a plan, a canal: Panama.\" palindrome? ." "f" }
133 $nl
134 "We would like it to output " { $link t } " there. We can encode this requirement with a unit test that we add to " { $snippet "palindrome-tests.factor" } ":"
135 { $code "[ t ] [ \"A man, a plan, a canal: Panama.\" palindrome? ] unit-test" }
136 $nl
137 "If you now run unit tests, you will see a unit test failure:"
138 { $code "\"palindrome\" test" }
139 $nl
140 "The next step is to, of course, fix our code so that the unit test can pass."
141 $nl
142 "We begin by writing a word which removes blanks and non-alphabetical characters from a string, and then converts the string to lower case. We call this word " { $snippet "normalize" } ". To figure out how to write this word, we begin with some interactive experimentation in the listener."
143 $nl
144 "Start by pushing a character on the stack; notice that characters are really just integers:"
145 { $code "CHAR: a" }
146 $nl
147 "Now, use the " { $link Letter? } " word to test if it is an alphabetical character, upper or lower case:"
148 { $unchecked-example "Letter? ." "t" }
149 "Note: you might receive an error message that asks if you want to use the " { $link "ascii" } " or " { $link "unicode" } " versions of the " { $link Letter? } " word. Choosing the Unicode version will allow Factor to continue running your code."
150 $nl
151 "This gives the expected result."
152 $nl
153 "Now try with a non-alphabetical character:"
154 { $code "CHAR: #" }
155 { $unchecked-example "Letter? ." "f" }
156 $nl
157 "What we want to do is given a string, remove all characters which do not match the " { $link Letter? } " predicate. Let's push a string on the stack:"
158 { $code "\"A man, a plan, a canal: Panama.\"" }
159 $nl
160 "Now, place a quotation containing " { $link Letter? } " on the stack; quoting code places it on the stack instead of executing it immediately:"
161 { $code "[ Letter? ]" }
162 "Note: " { $link "quotations" } " are similar to anonymous functions or blocks of code that have not been executed yet."
163 $nl
164 "Finally, we pass the string and the quotation to the " { $link filter } " word, which will run your quotation and return a new string that contains only characters for which " { $link Letter? } " returns \"true\":"
165 { $code "filter" }
166 $nl
167 "The stack should now contain the following string: "
168 { $snippet "AmanaplanacanalPanama" } ". "
169 "This is almost what we want; we just need to convert the string to lower case now. This can be done by calling " { $link >lower } "; the " { $snippet ">" } " prefix is a naming convention for conversion operations, and should be read as “to”:"
170 { $code ">lower" }
171 $nl
172 "Finally, let's print the top of the stack and discard it:"
173 { $code "." }
174 $nl
175 "This will output " { $snippet "amanaplanacanalpanama" } ". This string is in the form that we want, and we evaluated the following code to get it into this form:"
176 { $code "[ Letter? ] filter >lower" }
177 $nl
178 "This code starts with a string on the stack, removes non-alphabetical characters, and converts the result to lower case, leaving a new string on the stack. We put this code in a new word, and add the new word to " { $snippet "palindrome.factor" } ":"
179 { $code ": normalize ( string -- string' ) [ Letter? ] filter >lower ;" }
180 $nl
181 "You will need to add " { $vocab-link "unicode.case" } " and " { $vocab-link "unicode.categories" } " to the vocabulary search path, so that " { $link >lower } " and " { $link Letter? } " can be used in the source file."
182 $nl
183 "We modify " { $snippet "palindrome?" } " to first apply " { $snippet "normalize" } " to its input:"
184 { $code ": palindrome? ( string -- ? ) normalize dup reverse = ;" }
185 $nl
186 "Factor compiles the file from the top down. So, be sure to place the definition for " { $snippet "normalize" } " above the definition for " { $snippet "palindrome?" } "."
187 $nl
188 "Now if you press " { $command tool "common" refresh-all } ", the source file should reload without any errors. You can run unit tests again, and this time, they will all pass:"
189 { $code "\"palindrome\" test" }
190 $nl
191 "Congratulations, you have now completed " { $link "first-program" } "!" ;
192
193 ARTICLE: "first-program" "Your first program"
194 "In this tutorial, we will write a simple Factor program which prompts the user to enter a word, and tests if it is a palindrome (that is, the word is spelled the same backwards and forwards)."
195 $nl
196 "In this tutorial, you will learn about basic Factor development tools."
197 $nl
198 "Note: when you come across boxes with Factor code examples, you can click on them to copy and paste the code into your listener, to be run by then hitting " { $snippet "ENTER" } "."
199 $nl
200 { $subsections
201     "first-program-start"
202     "first-program-logic"
203     "first-program-test"
204     "first-program-extend"
205 } ;
206
207 ABOUT: "first-program"