]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel-docs.factor
Merge branch 'master' into no-elements
[factor.git] / core / kernel / kernel-docs.factor
1 USING: generic help.markup help.syntax math memory
2 namespaces sequences kernel.private layouts classes
3 kernel.private vectors combinators quotations strings words
4 assocs arrays math.order ;
5 IN: kernel
6
7 HELP: eq? ( obj1 obj2 -- ? )
8 { $values { "obj1" object } { "obj2" object } { "?" "a boolean" } }
9 { $description "Tests if two references point at the same object." } ;
10
11 HELP: drop  ( x -- )                 $shuffle ;
12 HELP: 2drop ( x y -- )               $shuffle ;
13 HELP: 3drop ( x y z -- )             $shuffle ;
14 HELP: dup   ( x -- x x )             $shuffle ;
15 HELP: 2dup  ( x y -- x y x y )       $shuffle ;
16 HELP: 3dup  ( x y z -- x y z x y z ) $shuffle ;
17 HELP: rot   ( x y z -- y z x )       $shuffle ;
18 HELP: -rot  ( x y z -- z x y )       $shuffle ;
19 HELP: dupd  ( x y -- x x y )         $shuffle ;
20 HELP: swapd ( x y z -- y x z )       $shuffle ;
21 HELP: nip   ( x y -- y )             $shuffle ;
22 HELP: 2nip  ( x y z -- z )           $shuffle ;
23 HELP: tuck  ( x y -- y x y )         $shuffle ;
24 HELP: over  ( x y -- x y x )         $shuffle ;
25 HELP: 2over                          $shuffle ;
26 HELP: pick  ( x y z -- x y z x )     $shuffle ;
27 HELP: swap  ( x y -- y x )           $shuffle ;
28 HELP: spin                           $shuffle ;
29 HELP: roll                           $shuffle ;
30 HELP: -roll                          $shuffle ;
31
32 HELP: datastack ( -- ds )
33 { $values { "ds" array } }
34 { $description "Outputs an array containing a copy of the data stack contents right before the call to this word, with the top of the stack at the end of the array." } ;
35
36 HELP: set-datastack ( ds -- )
37 { $values { "ds" array } }
38 { $description "Replaces the data stack contents with a copy of an array. The end of the array becomes the top of the stack." } ;
39
40 HELP: retainstack ( -- rs )
41 { $values { "rs" array } }
42 { $description "Outputs an array containing a copy of the retain stack contents right before the call to this word, with the top of the stack at the end of the array." } ;
43
44 HELP: set-retainstack ( rs -- )
45 { $values { "rs" array } }
46 { $description "Replaces the retain stack contents with a copy of an array. The end of the array becomes the top of the stack." } ;
47
48 HELP: callstack ( -- cs )
49 { $values { "cs" callstack } }
50 { $description "Outputs a copy of the call stack contents, with the top of the stack at the end of the vector. The stack frame of the caller word is " { $emphasis "not" } " included." } ;
51
52 HELP: set-callstack ( cs -- )
53 { $values { "cs" callstack } }
54 { $description "Replaces the call stack contents. The end of the vector becomes the top of the stack. Control flow is transferred immediately to the new call stack." } ;
55
56 HELP: clear
57 { $description "Clears the data stack." } ;
58
59 HELP: build
60 { $description "The current build number. Factor increments this number whenever a new boot image is created." } ;
61
62 HELP: hashcode*
63 { $values { "depth" integer } { "obj" object } { "code" fixnum } }
64 { $contract "Outputs the hashcode of an object. The hashcode operation must satisfy the following properties:"
65 { $list
66     { "If two objects are equal under " { $link = } ", they must have equal hashcodes." }
67     { "If the hashcode of an object depends on the values of its slots, the hashcode of the slots must be computed recursively by calling " { $link hashcode* } " with a " { $snippet "level" } " parameter decremented by one. This avoids excessive work while still computing well-distributed hashcodes. The " { $link recursive-hashcode } " combinator can help with implementing this logic," }
68     { "The hashcode should be a " { $link fixnum } ", however returning a " { $link bignum } " will not cause any problems other than potential performance degradation." }
69     { "The hashcode is only permitted to change between two invocations if the object or one of its slot values was mutated." }
70 }
71 "If mutable objects are used as hashtable keys, they must not be mutated in such a way that their hashcode changes. Doing so will violate bucket sorting invariants and result in undefined behavior. See " { $link "hashtables.keys" } " for details." } ;
72
73 HELP: hashcode
74 { $values { "obj" object } { "code" fixnum } }
75 { $description "Computes the hashcode of an object with a default hashing depth. See " { $link hashcode* } " for the hashcode contract." } ;
76
77 { hashcode hashcode* } related-words
78
79 HELP: =
80 { $values { "obj1" object } { "obj2" object } { "?" "a boolean" } }
81 { $description
82     "Tests if two objects are equal. If " { $snippet "obj1" } " and " { $snippet "obj2" } " point to the same object, outputs " { $link t } ". Otherwise, calls the " { $link equal? } " generic word."
83 }
84 { $examples
85     { $example "USING: kernel prettyprint ;" "5 5 = ." "t" }
86     { $example "USING: kernel prettyprint ;" "5 005 = ." "t" }
87     { $example "USING: kernel prettyprint ;" "5 5.0 = ." "f" }
88     { $example "USING: arrays kernel prettyprint ;" "{ \"a\" \"b\" } \"a\" \"b\" 2array = ." "t" }
89     { $example "USING: arrays kernel prettyprint ;" "{ \"a\" \"b\" } [ \"a\" \"b\" ] = ." "f" }
90 } ;
91
92 HELP: equal?
93 { $values { "obj1" object } { "obj2" object } { "?" "a boolean" } }
94 { $contract
95     "Tests if two objects are equal."
96     $nl
97     "User code should call " { $link = } " instead; that word first tests the case where the objects are " { $link eq? } ", and so by extension, methods defined on " { $link equal? } " assume they are never called on " { $link eq? } " objects."
98     $nl
99     "Method definitions should ensure that this is an equality relation, modulo the assumption that the two objects are not " { $link eq? } ". That is, for any three non-" { $link eq? } " objects " { $snippet "a" } ", " { $snippet "b" } " and " { $snippet "c" } ", we must have:"
100     { $list
101         { { $snippet "a = b" } " implies " { $snippet "b = a" } }
102         { { $snippet "a = b" } " and " { $snippet "b = c" } " implies " { $snippet "a = c" } }
103     }
104     "If a class defines a custom equality comparison test, it should also define a compatible method for the " { $link hashcode* } " generic word."
105 }
106 { $examples
107     "An example demonstrating why this word should only be used to define methods on, and never called directly:"
108     { $example "USING: kernel prettyprint ;" "5 5 equal? ." "f" }
109     "Using " { $link = } " gives the expected behavior:"
110     { $example "USING: kernel prettyprint ;" "5 5 = ." "t" }
111 } ;
112
113 HELP: identity-tuple
114 { $class-description "A class defining an " { $link equal? } " method which always returns f." }
115 { $examples
116     "To define a tuple class such that two instances are only equal if they are both the same instance, inherit from the " { $link identity-tuple } " class. This class defines a method on " { $link equal? } " which always returns " { $link f } ". Since " { $link = } " handles the case where the two objects are " { $link eq? } ", this method will never be called with two " { $link eq? } " objects, so such a definition is valid:"
117     { $code "TUPLE: foo < identity-tuple ;" }
118     "By calling " { $link = } " on instances of " { $snippet "foo" } " we get the results we expect:"
119     { $unchecked-example "T{ foo } dup = ." "t" }
120     { $unchecked-example "T{ foo } dup clone = ." "f" }
121 } ;
122
123 HELP: clone
124 { $values { "obj" object } { "cloned" "a new object" } }
125 { $contract "Outputs a new object equal to the given object. This is not guaranteed to actually copy the object; it does nothing with immutable objects, and does not copy words either. However, sequences and tuples can be cloned to obtain a shallow copy of the original." } ;
126
127 HELP: ?
128 { $values { "?" "a generalized boolean" } { "true" object } { "false" object } { "true/false" "one two input objects" } }
129 { $description "Chooses between two values depending on the boolean value of " { $snippet "cond" } "." } ;
130
131 HELP: >boolean
132 { $values { "obj" "a generalized boolean" } { "?" "a boolean" } }
133 { $description "Convert a generalized boolean into a boolean. That is, " { $link f } " retains its value, whereas anything else becomes " { $link t } "." } ;
134
135 HELP: not
136 { $values { "obj" "a generalized boolean" } { "?" "a boolean" } }
137 { $description "For " { $link f } " outputs " { $link t } " and for anything else outputs " { $link f } "." }
138 { $notes "This word implements boolean not, so applying it to integers will not yield useful results (all integers have a true value). Bitwise not is the " { $link bitnot } " word." } ;
139
140 HELP: and
141 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "?" "a generalized boolean" } }
142 { $description "If both inputs are true, outputs " { $snippet "obj2" } ". otherwise outputs " { $link f } "." }
143 { $notes "This word implements boolean and, so applying it to integers will not yield useful results (all integers have a true value). Bitwise and is the " { $link bitand } " word." }
144 { $examples
145     "Usually only the boolean value of the result is used, however you can also explicitly rely on the behavior that if both inputs are true, the second is output:"
146     { $example "USING: kernel prettyprint ;" "t f and ." "f" }
147     { $example "USING: kernel prettyprint ;" "t 7 and ." "7" }
148     { $example "USING: kernel prettyprint ;" "\"hi\" 12.0 and ." "12.0" }
149 } ;
150
151 HELP: or
152 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "?" "a generalized boolean" } }
153 { $description "If both inputs are false, outputs " { $link f } ". otherwise outputs the first of " { $snippet "obj1" } " and " { $snippet "obj2" } " which is true." }
154 { $notes "This word implements boolean inclusive or, so applying it to integers will not yield useful results (all integers have a true value). Bitwise inclusive or is the " { $link bitor } " word." }
155 { $examples
156     "Usually only the boolean value of the result is used, however you can also explicitly rely on the behavior that the result will be the first true input:"
157     { $example "USING: kernel prettyprint ;" "t f or ." "t" }
158     { $example "USING: kernel prettyprint ;" "\"hi\" 12.0 or ." "\"hi\"" }
159 } ;
160
161 HELP: xor
162 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "?" "a generalized boolean" } }
163 { $description "If exactly one input is false, outputs the other input. Otherwise outputs " { $link f } "." }
164 { $notes "This word implements boolean exclusive or, so applying it to integers will not yield useful results (all integers have a true value). Bitwise exclusive or is the " { $link bitxor } " word." } ;
165
166 HELP: both?
167 { $values { "quot" { $quotation "( obj -- ? )" } } { "x" object } { "y" object } { "?" "a boolean" } }
168 { $description "Tests if the quotation yields a true value when applied to both " { $snippet "x" } " and " { $snippet "y" } "." }
169 { $examples
170     { $example "USING: kernel math prettyprint ;" "3 5 [ odd? ] both? ." "t" }
171     { $example "USING: kernel math prettyprint ;" "12 7 [ even? ] both? ." "f" }
172 } ;
173
174 HELP: either?
175 { $values { "quot" { $quotation "( obj -- ? )" } } { "x" object } { "y" object } { "?" "a boolean" } }
176 { $description "Tests if the quotation yields a true value when applied to either " { $snippet "x" } " or " { $snippet "y" } "." }
177 { $examples
178     { $example "USING: kernel math prettyprint ;" "3 6 [ odd? ] either? ." "t" }
179     { $example "USING: kernel math prettyprint ;" "5 7 [ even? ] either? ." "f" }
180 } ;
181
182 HELP: call
183 { $values { "callable" callable } }
184 { $description "Calls a quotation." }
185 { $examples
186     "The following two lines are equivalent:"
187     { $code "2 [ 2 + 3 * ] call" "2 2 + 3 *" }
188 } ;
189
190 HELP: call-clear ( quot -- )
191 { $values { "quot" callable } }
192 { $description "Calls a quotation with an empty call stack. If the quotation returns, Factor will exit.." }
193 { $notes "Used to implement " { $link "threads" } "." } ;
194
195 HELP: slip
196 { $values { "quot" quotation } { "x" object } }
197 { $description "Calls a quotation while hiding the top of the stack." } ;
198
199 HELP: 2slip
200 { $values { "quot" quotation } { "x" object } { "y" object } }
201 { $description "Calls a quotation while hiding the top two stack elements." } ;
202
203 HELP: 3slip
204 { $values { "quot" quotation } { "x" object } { "y" object } { "z" object } }
205 { $description "Calls a quotation while hiding the top three stack elements." } ;
206
207 HELP: keep
208 { $values { "quot" { $quotation "( x -- ... )" } } { "x" object } }
209 { $description "Call a quotation with a value on the stack, restoring the value when the quotation returns." }
210 { $examples
211     { $example "USING: arrays kernel prettyprint ;" "2 \"greetings\" [ <array> ] keep 2array ." "{ { \"greetings\" \"greetings\" } \"greetings\" }" }
212 } ;
213
214 HELP: 2keep
215 { $values { "quot" { $quotation "( x y -- ... )" } } { "x" object } { "y" object } }
216 { $description "Call a quotation with two values on the stack, restoring the values when the quotation returns." } ;
217
218 HELP: 3keep
219 { $values { "quot" { $quotation "( x y z -- ... )" } } { "x" object } { "y" object } { "z" object } }
220 { $description "Call a quotation with three values on the stack, restoring the values when the quotation returns." } ;
221
222 HELP: bi
223 { $values { "x" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( x -- ... )" } } }
224 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "x" } "." }
225 { $examples
226     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x -- )" } ", then the following two lines are equivalent:"
227     { $code
228         "[ p ] [ q ] bi"
229         "dup p q"
230     }
231     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x -- y )" } ", then the following two lines are equivalent:"
232     { $code
233         "[ p ] [ q ] bi"
234         "dup p swap q"
235     }
236     "In general, the following two lines are equivalent:"
237     { $code
238         "[ p ] [ q ] bi"
239         "[ p ] keep q"
240     }
241     
242 } ;
243
244 HELP: 2bi
245 { $values { "x" object } { "y" object } { "p" { $quotation "( x y -- ... )" } } { "q" { $quotation "( x y -- ... )" } } }
246 { $description "Applies " { $snippet "p" } " to the two input values, then applies " { $snippet "q" } " to the two input values." }
247 { $examples
248     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x y -- )" } ", then the following two lines are equivalent:"
249     { $code
250         "[ p ] [ q ] 2bi"
251         "2dup p q"
252     }
253     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x y -- z )" } ", then the following two lines are equivalent:"
254     { $code
255         "[ p ] [ q ] 2bi"
256         "2dup p -rot q"
257     }
258     "In general, the following two lines are equivalent:"
259     { $code
260         "[ p ] [ q ] 2bi"
261         "[ p ] 2keep q"
262     }
263 } ;
264
265 HELP: 3bi
266 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation "( x y z -- ... )" } } { "q" { $quotation "( x y z -- ... )" } } }
267 { $description "Applies " { $snippet "p" } " to the three input values, then applies " { $snippet "q" } " to the three input values." }
268 { $examples
269     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x y z -- )" } ", then the following two lines are equivalent:"
270     { $code
271         "[ p ] [ q ] 3bi"
272         "3dup p q"
273     }
274     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x y z -- w )" } ", then the following two lines are equivalent:"
275     { $code
276         "[ p ] [ q ] 3bi"
277         "3dup p -roll q"
278     }
279     "In general, the following two lines are equivalent:"
280     { $code
281         "[ p ] [ q ] 3bi"
282         "[ p ] 3keep q"
283     }
284 } ;
285
286 HELP: tri
287 { $values { "x" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( x -- ... )" } } { "r" { $quotation "( x -- ... )" } } }
288 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "x" } ", and finally applies " { $snippet "r" } " to " { $snippet "x" } "." }
289 { $examples
290     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x -- )" } ", then the following two lines are equivalent:"
291     { $code
292         "[ p ] [ q ] [ r ] tri"
293         "dup p dup q r"
294     }
295     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x -- y )" } ", then the following two lines are equivalent:"
296     { $code
297         "[ p ] [ q ] [ r ] tri"
298         "dup p over q rot r"
299     }
300     "In general, the following two lines are equivalent:"
301     { $code
302         "[ p ] [ q ] [ r ] tri"
303         "[ p ] keep [ q ] keep r"
304     }
305 } ;
306
307 HELP: 2tri
308 { $values { "x" object } { "y" object } { "p" { $quotation "( x y -- ... )" } } { "q" { $quotation "( x y -- ... )" } } { "r" { $quotation "( x y -- ... )" } } }
309 { $description "Applies " { $snippet "p" } " to the two input values, then applies " { $snippet "q" } " to the two input values, and finally applies " { $snippet "r" } " to the two input values." }
310 { $examples
311     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x y -- )" } ", then the following two lines are equivalent:"
312     { $code
313         "[ p ] [ q ] [ r ] 2tri"
314         "2dup p 2dup q r"
315     }
316     "In general, the following two lines are equivalent:"
317     { $code
318         "[ p ] [ q ] [ r ] 2tri"
319         "[ p ] 2keep [ q ] 2keep r"
320     }
321 } ;
322
323 HELP: 3tri
324 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation "( x y z -- ... )" } } { "q" { $quotation "( x y z -- ... )" } } { "r" { $quotation "( x y z -- ... )" } } }
325 { $description "Applies " { $snippet "p" } " to the three input values, then applies " { $snippet "q" } " to the three input values, and finally applies " { $snippet "r" } " to the three input values." }
326 { $examples
327     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x y z -- )" } ", then the following two lines are equivalent:"
328     { $code
329         "[ p ] [ q ] [ r ] 3tri"
330         "3dup p 3dup q r"
331     }
332     "In general, the following two lines are equivalent:"
333     { $code
334         "[ p ] [ q ] [ r ] 3tri"
335         "[ p ] 3keep [ q ] 3keep r"
336     }
337 } ;
338
339
340 HELP: bi*
341 { $values { "x" object } { "y" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( y -- ... )" } } }
342 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } "." }
343 { $examples
344     "The following two lines are equivalent:"
345     { $code
346         "[ p ] [ q ] bi*"
347         "[ p ] dip q"
348     }
349 } ;
350
351 HELP: 2bi*
352 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "p" { $quotation "( w x -- ... )" } } { "q" { $quotation "( y z -- ... )" } } }
353 { $description "Applies " { $snippet "p" } " to " { $snippet "w" } " and " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } " and " { $snippet "z" } "." }
354 { $examples
355     "The following two lines are equivalent:"
356     { $code
357         "[ p ] [ q ] 2bi*"
358         "[ p ] 2dip q"
359     }
360 } ;
361
362 HELP: 2tri*
363 { $values { "u" object } { "v" object } { "w" object } { "x" object } { "y" object } { "z" object } { "p" { $quotation "( u v -- ... )" } } { "q" { $quotation "( w x -- ... )" } } { "r" { $quotation "( y z -- ... )" } } }
364 { $description "Applies " { $snippet "p" } " to " { $snippet "u" } " and " { $snippet "v" } ", then applies " { $snippet "q" } " to " { $snippet "w" } " and " { $snippet "x" } ", and finally applies " { $snippet "r" } " to " { $snippet "y" } " and " { $snippet "z" } "." }
365 { $examples
366     "The following two lines are equivalent:"
367     { $code
368         "[ p ] [ q ] [ r ] 2tri*"
369         "[ [ p ] 2dip q ] 2dip r"
370     }
371 } ;
372
373 HELP: tri*
374 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( y -- ... )" } } { "r" { $quotation "( z -- ... )" } } }
375 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } ", and finally applies " { $snippet "r" } " to " { $snippet "z" } "." }
376 { $examples
377     "The following two lines are equivalent:"
378     { $code
379         "[ p ] [ q ] [ r ] tri*"
380         "[ [ p ] dip q ] dip r"
381     }
382 } ;
383
384 HELP: bi@
385 { $values { "x" object } { "y" object } { "quot" { $quotation "( obj -- ... )" } } }
386 { $description "Applies the quotation to " { $snippet "x" } ", then to " { $snippet "y" } "." }
387 { $examples
388     "The following two lines are equivalent:"
389     { $code
390         "[ p ] bi@"
391         "[ p ] dip p"
392     }
393     "The following two lines are also equivalent:"
394     { $code
395         "[ p ] bi@"
396         "[ p ] [ p ] bi*"
397     }
398 } ;
399
400 HELP: 2bi@
401 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "quot" { $quotation "( obj1 obj2 -- ... )" } } }
402 { $description "Applies the quotation to " { $snippet "w" } " and " { $snippet "x" } ", then to " { $snippet "y" } " and " { $snippet "z" } "." }
403 { $examples
404     "The following two lines are equivalent:"
405     { $code
406         "[ p ] 2bi@"
407         "[ p ] 2dip p"
408     }
409     "The following two lines are also equivalent:"
410     { $code
411         "[ p ] 2bi@"
412         "[ p ] [ p ] 2bi*"
413     }
414 } ;
415
416 HELP: tri@
417 { $values { "x" object } { "y" object } { "z" object } { "quot" { $quotation "( obj -- ... )" } } }
418 { $description "Applies the quotation to " { $snippet "x" } ", then to " { $snippet "y" } ", and finally to " { $snippet "z" } "." }
419 { $examples
420     "The following two lines are equivalent:"
421     { $code
422         "[ p ] tri@"
423         "[ [ p ] dip p ] dip p"
424     }
425     "The following two lines are also equivalent:"
426     { $code
427         "[ p ] tri@"
428         "[ p ] [ p ] [ p ] tri*"
429     }
430 } ;
431
432 HELP: 2tri@
433 { $values { "u" object } { "v" object } { "w" object } { "x" object } { "y" object } { "z" object } { "quot" { $quotation "( obj1 obj2 -- ... )" } } }
434 { $description "Applies the quotation to " { $snippet "u" } " and " { $snippet "v" } ", then to " { $snippet "w" } " and " { $snippet "x" } ", and then to " { $snippet "y" } " and " { $snippet "z" } "." }
435 { $examples
436     "The following two lines are equivalent:"
437     { $code
438         "[ p ] 2tri@"
439         "[ [ p ] 2dip p ] 2dip p"
440     }
441     "The following two lines are also equivalent:"
442     { $code
443         "[ p ] 2tri@"
444         "[ p ] [ p ] [ p ] 2tri*"
445     }
446 } ;
447
448 HELP: if
449 { $values { "?" "a generalized boolean" } { "true" quotation } { "false" quotation } }
450 { $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation. Otherwise calls the " { $snippet "true" } " quotation."
451 $nl
452 "The " { $snippet "cond" } " value is removed from the stack before either quotation is called." } ;
453
454 HELP: when
455 { $values { "?" "a generalized boolean" } { "true" quotation } }
456 { $description "If " { $snippet "cond" } " is not " { $link f } ", calls the " { $snippet "true" } " quotation."
457 $nl
458 "The " { $snippet "cond" } " value is removed from the stack before the quotation is called." } ;
459
460 HELP: unless
461 { $values { "?" "a generalized boolean" } { "false" quotation } }
462 { $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation."
463 $nl
464 "The " { $snippet "cond" } " value is removed from the stack before the quotation is called." } ;
465
466 HELP: if*
467 { $values { "?" "a generalized boolean" } { "true" { $quotation "( cond -- ... )" } } { "false" quotation } }
468 { $description "Alternative conditional form that preserves the " { $snippet "cond" } " value if it is true."
469 $nl
470 "If the condition is true, it is retained on the stack before the " { $snippet "true" } " quotation is called. Otherwise, the condition is removed from the stack and the " { $snippet "false" } " quotation is called."
471 $nl
472 "The following two lines are equivalent:"
473 { $code "X [ Y ] [ Z ] if*" "X dup [ Y ] [ drop Z ] if" } } ;
474
475 HELP: when*
476 { $values { "?" "a generalized boolean" } { "true" { $quotation "( cond -- ... )" } } }
477 { $description "Variant of " { $link if* } " with no false quotation."
478 $nl
479 "The following two lines are equivalent:"
480 { $code "X [ Y ] when*" "X dup [ Y ] [ drop ] if" } } ;
481
482 HELP: unless*
483 { $values { "?" "a generalized boolean" } { "false" "a quotation " } }
484 { $description "Variant of " { $link if* } " with no true quotation." }
485 { $notes
486 "The following two lines are equivalent:"
487 { $code "X [ Y ] unless*" "X dup [ ] [ drop Y ] if" } } ;
488
489 HELP: ?if
490 { $values { "default" object } { "cond" "a generalized boolean" } { "true" { $quotation "( cond -- ... )" } } { "false" { $quotation "( default -- ... )" } } }
491 { $description "If the condition is " { $link f } ", the " { $snippet "false" } " quotation is called with the " { $snippet "default" } " value on the stack. Otherwise, the " { $snippet "true" } " quotation is called with the condition on the stack." }
492 { $notes
493 "The following two lines are equivalent:"
494 { $code "[ X ] [ Y ] ?if" "dup [ nip X ] [ drop Y ] if" }
495 "The following two lines are equivalent:"
496 { $code "[ ] [ ] ?if" "swap or" } } ;
497
498 HELP: die
499 { $description "Starts the front-end processor (FEP), which is a low-level debugger which can inspect memory addresses and the like. The FEP is also entered when a critical error occurs." }
500 { $notes
501     "The term FEP originates from the Lisp machines of old. According to the Jargon File,"
502     $nl
503     { $strong "fepped out" } " /fept owt/ " { $emphasis "adj." }  " The Symbolics 3600 LISP Machine has a Front-End Processor called a `FEP' (compare sense 2 of box). When the main processor gets wedged, the FEP takes control of the keyboard and screen. Such a machine is said to have `fepped out' or `dropped into the fep'." 
504     $nl
505     { $url "http://www.jargon.net/jargonfile/f/feppedout.html" }
506 } ;
507
508 HELP: (clone) ( obj -- newobj )
509 { $values { "obj" object } { "newobj" "a shallow copy" } }
510 { $description "Outputs a byte-by-byte copy of the given object. User code should call " { $link clone } " instead." } ;
511
512 HELP: declare
513 { $values { "spec" "an array of class words" } }
514 { $description "Declares that the elements at the top of the stack are instances of the classes in " { $snippet "spec" } "." }
515 { $warning "The compiler blindly trusts declarations, and false declarations can lead to crashes, memory corruption and other undesirable behavior." }
516 { $examples
517     "The optimizer cannot do anything with the below code:"
518     { $code "2 + 10 *" }
519     "However, if we declare that the top of the stack is a " { $link float } ", then type checks and generic dispatch are eliminated, and the compiler can use unsafe intrinsics:"
520     { $code "{ float } declare 2 + 10 *" }
521 } ;
522
523 HELP: tag ( object -- n )
524 { $values { "object" object } { "n" "a tag number" } }
525 { $description "Outputs an object's tag number, between zero and one less than " { $link num-tags } ". This is implementation detail and user code should call " { $link class } " instead." } ;
526
527 HELP: getenv ( n -- obj )
528 { $values { "n" "a non-negative integer" } { "obj" object } }
529 { $description "Reads an object from the Factor VM's environment table. User code never has to read the environment table directly; instead, use one of the callers of this word." } ;
530
531 HELP: setenv ( obj n -- )
532 { $values { "n" "a non-negative integer" } { "obj" object } }
533 { $description "Writes an object to the Factor VM's environment table. User code never has to write to the environment table directly; instead, use one of the callers of this word." } ;
534
535 HELP: object
536 { $class-description
537     "The class of all objects. If a generic word defines a method specializing on this class, the method is used as a fallback, if no other applicable method is found. For instance:"
538     { $code "GENERIC: enclose" "M: number enclose 1array ;" "M: object enclose ;" }
539 } ;
540
541 HELP: null
542 { $class-description
543     "The canonical empty class with no instances."
544 } ;
545
546 HELP: most
547 { $values { "x" object } { "y" object } { "quot" { $quotation "( x y -- ? )" } } { "z" "either " { $snippet "x" } " or " { $snippet "y" } } }
548 { $description "If the quotation yields a true value when applied to " { $snippet "x" } " and " { $snippet "y" } ", outputs " { $snippet "x" } ", otherwise outputs " { $snippet "y" } "." } ;
549
550 HELP: curry
551 { $values { "obj" object } { "quot" callable } { "curry" curry } }
552 { $description "Partial application. Outputs a " { $link callable } " which first pushes " { $snippet "obj" } " and then calls " { $snippet "quot" } "." }
553 { $class-description "The class of objects created by " { $link curry } ". These objects print identically to quotations and implement the sequence protocol, however they only use two cells of storage; a reference to the object and a reference to the underlying quotation." }
554 { $notes "Even if " { $snippet "obj" } " is a word, it will be pushed as a literal."
555 $nl
556 "This operation is efficient and does not copy the quotation." }
557 { $examples
558     { $example "USING: kernel prettyprint ;" "5 [ . ] curry ." "[ 5 . ]" }
559     { $example "USING: kernel prettyprint ;" "\\ = [ see ] curry ." "[ \\ = see ]" }
560     { $example "USING: kernel math prettyprint sequences ;" "{ 1 2 3 } 2 [ - ] curry map ." "{ -1 0 1 }" }
561 } ;
562
563 HELP: 2curry
564 { $values { "obj1" object } { "obj2" object } { "quot" callable } { "curry" curry } }
565 { $description "Outputs a " { $link callable } " which pushes " { $snippet "obj1" } " and " { $snippet "obj2" } " and then calls " { $snippet "quot" } "." }
566 { $notes "This operation is efficient and does not copy the quotation." }
567 { $examples
568     { $example "USING: kernel math prettyprint ;" "5 4 [ + ] 2curry ." "[ 5 4 + ]" }
569 } ;
570
571 HELP: 3curry
572 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "quot" callable } { "curry" curry } }
573 { $description "Outputs a " { $link callable } " which pushes " { $snippet "obj1" } ", " { $snippet "obj2" } " and " { $snippet "obj3" } ", and then calls " { $snippet "quot" } "." }
574 { $notes "This operation is efficient and does not copy the quotation." } ;
575
576 HELP: with
577 { $values { "param" object } { "obj" object } { "quot" { $quotation "( param elt -- ... )" } } { "obj" object } { "curry" curry } }
578 { $description "Partial application on the left. The following two lines are equivalent:"
579     { $code "swap [ swap A ] curry B" }
580     { $code "[ A ] with B" }
581     
582 }
583 { $notes "This operation is efficient and does not copy the quotation." }
584 { $examples
585     { $example "USING: kernel math prettyprint sequences ;" "2 { 1 2 3 } [ - ] with map ." "{ 1 0 -1 }" }
586 } ;
587
588 HELP: compose
589 { $values { "quot1" callable } { "quot2" callable } { "compose" compose } }
590 { $description "Quotation composition. Outputs a " { $link callable } " which calls " { $snippet "quot1" } " followed by " { $snippet "quot2" } "." }
591 { $notes
592     "The following two lines are equivalent:"
593     { $code
594         "compose call"
595         "append call"
596     }
597     "However, " { $link compose } " runs in constant time, and the optimizing compiler is able to compile code which calls composed quotations."
598 } ;
599
600
601 HELP: prepose
602 { $values { "quot1" callable } { "quot2" callable } { "compose" compose } }
603 { $description "Quotation composition. Outputs a " { $link callable } " which calls " { $snippet "quot2" } " followed by " { $snippet "quot1" } "." }
604 { $notes "See " { $link compose } " for details." } ;
605
606 { compose prepose } related-words
607
608 HELP: dip
609 { $values { "x" object } { "quot" quotation } }
610 { $description "Calls " { $snippet "quot" } " with " { $snippet "obj" } " hidden on the retain stack." }
611 { $examples
612     { $example "USING: arrays kernel math prettyprint ;" "10 20 30 [ / ] dip 2array ." "{ 1/2 30 }" }
613 } ;
614
615 HELP: 2dip
616 { $values { "x" object } { "y" object } { "quot" quotation } }
617 { $description "Calls " { $snippet "quot" } " with " { $snippet "x" } " and " { $snippet "y" } " hidden on the retain stack." }
618 { $notes "The following are equivalent:"
619     { $code "[ [ foo bar ] dip ] dip" }
620     { $code "[ foo bar ] 2dip" }
621 } ;
622
623 HELP: 3dip
624 { $values { "x" object } { "y" object } { "z" object } { "quot" quotation } }
625 { $description "Calls " { $snippet "quot" } " with " { $snippet "x" } ", " { $snippet "y" } " and " { $snippet "z" } " hidden on the retain stack." }
626 { $notes "The following are equivalent:"
627     { $code "[ [ [ foo bar ] dip ] dip ] dip" }
628     { $code "[ foo bar ] 3dip" }
629 } ;
630
631 HELP: 4dip
632 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "quot" quotation } }
633 { $description "Calls " { $snippet "quot" } " with " { $snippet "w" } ", " { $snippet "x" } ", " { $snippet "y" } " and " { $snippet "z" } " hidden on the retain stack." }
634 { $notes "The following are equivalent:"
635     { $code "[ [ [ [ foo bar ] dip ] dip ] dip ] dip" }
636     { $code "[ foo bar ] 4dip" }
637 } ;
638
639 HELP: while
640 { $values { "pred" { $quotation "( -- ? )" } } { "body" "a quotation" } { "tail" "a quotation" } }
641 { $description "Calls " { $snippet "body" } " until " { $snippet "pred" } " returns " { $link f } "." } ;
642
643 HELP: until
644 { $values { "pred" { $quotation "( -- ? )" } } { "body" "a quotation" } { "tail" "a quotation" } }
645 { $description "Calls " { $snippet "body" } " until " { $snippet "pred" } " returns " { $link t } "." } ;
646
647 HELP: do
648 { $values { "pred" { $quotation "( -- ? )" } } { "body" "a quotation" } { "tail" "a quotation" } }
649 { $description "Executes one iteration of a " { $link while } " or " { $link until } " loop." } ;
650
651 HELP: loop
652 { $values
653      { "pred" quotation } }
654      { $description "Calls the quotation repeatedly until it outputs " { $link f } "." }
655 { $examples "Loop until we hit a zero:"
656     { $unchecked-example "USING: kernel random math io ; "
657     " [ \"hi\" write bl 10 random zero? not ] loop"
658     "hi hi hi" }
659     "A fun loop:"
660     { $example "USING: kernel prettyprint math ; "
661     "3 [ dup . 7 + 11 mod dup 3 = not ] loop drop"
662     "3\n10\n6\n2\n9\n5\n1\n8\n4\n0\n7" }
663 } ;
664
665 ARTICLE: "looping-combinators" "Looping combinators"
666 "In most cases, loops should be written using high-level combinators (such as " { $link "sequences-combinators" } ") or tail recursion. However, sometimes, the best way to express intent is with a loop."
667 { $subsection while }
668 { $subsection until }
669 "The above two combinators take a " { $snippet "tail" } " quotation. Strictly speaking, the " { $snippet "tail" } " is not necessary, since the following are equivalent:"
670 { $code
671     "[ P ] [ Q ] [ T ] while"
672     "[ P ] [ Q ] [ ] while T"
673 }
674 "However, depending on the stack effects of " { $snippet "pred" } " and " { $snippet "quot" } ", the " { $snippet "tail" } " quotation might need to be non-empty in order to balance out the stack effect of branches for stack effect inference."
675 $nl
676 "To execute one iteration of a loop, use the following word:"
677 { $subsection do }
678 "This word is intended as a modifier. The normal " { $link while } " loop never executes the body if the predicate returns first on the first iteration. To ensure the body executes at least once, use " { $link do } ":"
679 { $code
680     "[ P ] [ Q ] [ T ] do while"
681 }
682 "A simpler looping combinator which executes a single quotation until it returns " { $link f } ":"
683 { $subsection loop } ;
684
685 HELP: assert
686 { $values { "got" "the obtained value" } { "expect" "the expected value" } }
687 { $description "Throws an " { $link assert } " error." }
688 { $error-description "Thrown when a unit test or other assertion fails." } ;
689
690 HELP: assert=
691 { $values { "a" object } { "b" object } }
692 { $description "Throws an " { $link assert } " error if " { $snippet "a" } " does not equal " { $snippet "b" } "." } ;
693
694 ARTICLE: "shuffle-words" "Shuffle words"
695 "Shuffle words rearrange items at the top of the data stack. They control the flow of data between words that perform actions."
696 $nl
697 "The " { $link "cleave-combinators" } ", " { $link "spread-combinators" } " and " { $link "apply-combinators" } " are closely related to shuffle words and should be used instead where possible because they can result in clearer code; also, see the advice in " { $link "cookbook-philosophy" } "."
698 $nl
699 "Removing stack elements:"
700 { $subsection drop }
701 { $subsection 2drop }
702 { $subsection 3drop }
703 { $subsection nip }
704 { $subsection 2nip }
705 "Duplicating stack elements:"
706 { $subsection dup }
707 { $subsection 2dup }
708 { $subsection 3dup }
709 { $subsection dupd }
710 { $subsection over }
711 { $subsection 2over }
712 { $subsection pick }
713 { $subsection tuck }
714 "Permuting stack elements:"
715 { $subsection swap }
716 { $subsection swapd }
717 { $subsection rot }
718 { $subsection -rot }
719 { $subsection spin }
720 { $subsection roll }
721 { $subsection -roll } ;
722
723 ARTICLE: "cleave-shuffle-equivalence" "Expressing shuffle words with cleave combinators"
724 "Cleave combinators are defined in terms of shuffle words, and mappings from certain shuffle idioms to cleave combinators are discussed in the documentation for " { $link bi } ", " { $link 2bi } ", " { $link 3bi } ", " { $link tri } ", " { $link 2tri } " and " { $link 3tri } "."
725 $nl
726 "Certain shuffle words can also be expressed in terms of the cleave combinators. Internalizing such identities can help with understanding and writing code using cleave combinators:"
727 { $code
728     ": keep  [ ] bi ;"
729     ": 2keep [ ] 2bi ;"
730     ": 3keep [ ] 3bi ;"
731     ""
732     ": dup   [ ] [ ] bi ;"
733     ": 2dup  [ ] [ ] 2bi ;"
734     ": 3dup  [ ] [ ] 3bi ;"
735     ""
736     ": tuck  [ nip ] [ ] 2bi ;"
737     ": swap  [ nip ] [ drop ] 2bi ;"
738     ""
739     ": over  [ ] [ drop ] 2bi ;"
740     ": pick  [ ] [ 2drop ] 3bi ;"
741     ": 2over [ ] [ drop ] 3bi ;"
742 } ;
743
744 ARTICLE: "cleave-combinators" "Cleave combinators"
745 "The cleave combinators apply multiple quotations to a single value."
746 $nl
747 "Two quotations:"
748 { $subsection bi }
749 { $subsection 2bi }
750 { $subsection 3bi }
751 "Three quotations:"
752 { $subsection tri }
753 { $subsection 2tri }
754 { $subsection 3tri }
755 "Technically, the cleave combinators are redundant because they can be simulated using shuffle words and other combinators, and in addition, they do not reduce token counts by much, if at all. However, they can make code more readable by expressing intention and exploiting any inherent symmetry. For example, a piece of code which performs three operations on the top of the stack can be written in one of two ways:"
756 { $code
757     "! First alternative; uses keep"
758     "[ 1 + ] keep"
759     "[ 1 - ] keep"
760     "2 *"
761     "! Second alternative: uses tri"
762     "[ 1 + ]"
763     "[ 1 - ]"
764     "[ 2 * ] tri"
765 }
766 "The latter is more aesthetically pleasing than the former."
767 $nl
768 "A generalization of the above combinators to any number of quotations can be found in " { $link "combinators" } "."
769 { $subsection "cleave-shuffle-equivalence" } ;
770
771 ARTICLE: "spread-shuffle-equivalence" "Expressing shuffle words with spread combinators"
772 "Spread combinators are defined in terms of shuffle words, and mappings from certain shuffle idioms to spread combinators are discussed in the documentation for " { $link bi* } ", " { $link 2bi* } ", " { $link tri* } ", and " { $link 2tri* } "."
773 $nl
774 "Certain shuffle words can also be expressed in terms of the spread combinators. Internalizing such identities can help with understanding and writing code using spread combinators:"
775 { $code
776     ": dip   [ ] bi* ;"
777     ": 2dip  [ ] [ ] tri* ;"
778     ""
779     ": slip  [ call ] [ ] bi* ;"
780     ": 2slip [ call ] [ ] [ ] tri* ;"
781     ""
782     ": nip   [ drop ] [ ] bi* ;"
783     ": 2nip  [ drop ] [ drop ] [ ] tri* ;"
784     ""
785     ": rot"
786     "    [ [ drop ] [      ] [ drop ] tri* ]"
787     "    [ [ drop ] [ drop ] [      ] tri* ]"
788     "    [ [      ] [ drop ] [ drop ] tri* ]"
789     "    3tri ;"
790     ""
791     ": -rot"
792     "    [ [ drop ] [ drop ] [      ] tri* ]"
793     "    [ [      ] [ drop ] [ drop ] tri* ]"
794     "    [ [ drop ] [      ] [ drop ] tri* ]"
795     "    3tri ;"
796     ""
797     ": spin"
798     "    [ [ drop ] [ drop ] [      ] tri* ]"
799     "    [ [ drop ] [      ] [ drop ] tri* ]"
800     "    [ [      ] [ drop ] [ drop ] tri* ]"
801     "    3tri ;"
802 } ;
803
804 ARTICLE: "spread-combinators" "Spread combinators"
805 "The spread combinators apply multiple quotations to multiple values. The " { $snippet "*" } " suffix signifies spreading."
806 $nl
807 "Two quotations:"
808 { $subsection bi* }
809 { $subsection 2bi* }
810 "Three quotations:"
811 { $subsection tri* }
812 { $subsection 2tri* }
813 "Technically, the spread combinators are redundant because they can be simulated using shuffle words and other combinators, and in addition, they do not reduce token counts by much, if at all. However, they can make code more readable by expressing intention and exploiting any inherent symmetry. For example, a piece of code which performs three operations on three related values can be written in one of two ways:"
814 { $code
815     "! First alternative; uses dip"
816     "[ [ 1 + ] dip 1 - ] dip 2 *"
817     "! Second alternative: uses tri*"
818     "[ 1 + ] [ 1 - ] [ 2 * ] tri*"
819 }
820 "A generalization of the above combinators to any number of quotations can be found in " { $link "combinators" } "."
821 { $subsection "spread-shuffle-equivalence" } ;
822
823 ARTICLE: "apply-combinators" "Apply combinators"
824 "The apply combinators apply a single quotation to multiple values. The " { $snippet "@" } " suffix signifies application."
825 $nl
826 "Two quotations:"
827 { $subsection bi@ }
828 { $subsection 2bi@ }
829 "Three quotations:"
830 { $subsection tri@ }
831 { $subsection 2tri@ }
832 "A pair of utility words built from " { $link bi@ } ":"
833 { $subsection both? }
834 { $subsection either? } ;
835
836 ARTICLE: "slip-keep-combinators" "Retain stack combinators"
837 "Sometimes an additional storage area is needed to hold objects. The " { $emphasis "retain stack" } " is an auxilliary stack for this purpose. Objects can be moved between the data and retain stacks using a set of combinators."
838 $nl
839 "The dip combinators invoke the quotation at the top of the stack, hiding the values underneath:"
840 { $subsection dip }
841 { $subsection 2dip }
842 { $subsection 3dip }
843 { $subsection 4dip }
844 "The slip combinators invoke a quotation further down on the stack. They are most useful for implementing other combinators:"
845 { $subsection slip }
846 { $subsection 2slip }
847 { $subsection 3slip }
848 "The keep combinators invoke a quotation which takes a number of values off the stack, and then they restore those values:"
849 { $subsection keep }
850 { $subsection 2keep }
851 { $subsection 3keep } ;
852
853 ARTICLE: "compositional-combinators" "Compositional combinators"
854 "Quotations can be composed using efficient quotation-specific operations:"
855 { $subsection curry }
856 { $subsection 2curry }
857 { $subsection 3curry }
858 { $subsection with }
859 { $subsection compose }
860 { $subsection prepose }
861 "Quotations also implement the sequence protocol, and can be manipulated with sequence words; see " { $link "quotations" } "." ;
862
863 ARTICLE: "implementing-combinators" "Implementing combinators"
864 "The following pair of words invoke words and quotations reflectively:"
865 { $subsection call }
866 { $subsection execute }
867 "These words are used to implement combinators. Note that combinator definitions must be followed by the " { $link POSTPONE: inline } " declaration in order to compile in the optimizing compiler; for example:"
868 { $code
869     ": keep ( x quot -- x )"
870     "    over [ call ] dip ; inline"
871 }
872 "Word inlining is documented in " { $link "declarations" } "." ;
873
874 ARTICLE: "booleans" "Booleans"
875 "In Factor, any object that is not " { $link f } " has a true value, and " { $link f } " has a false value. The " { $link t } " object is the canonical true value."
876 { $subsection f }
877 { $subsection t }
878 "The " { $link f } " object is the unique instance of the " { $link f } " class; the two are distinct objects. The latter is also a parsing word which adds the " { $link f } " object to the parse tree at parse time. To refer to the class itself you must use " { $link POSTPONE: POSTPONE: } " or " { $link POSTPONE: \ } " to prevent the parsing word from executing."
879 $nl
880 "Here is the " { $link f } " object:"
881 { $example "f ." "f" }
882 "Here is the " { $link f } " class:"
883 { $example "\\ f ." "POSTPONE: f" }
884 "They are not equal:"
885 { $example "f \\ f = ." "f" }
886 "Here is an array containing the " { $link f } " object:"
887 { $example "{ f } ." "{ f }" }
888 "Here is an array containing the " { $link f } " class:"
889 { $example "{ POSTPONE: f } ." "{ POSTPONE: f }" }
890 "The " { $link f } " object is an instance of the " { $link f } " class:"
891 { $example "USE: classes" "f class ." "POSTPONE: f" }
892 "The " { $link f } " class is an instance of " { $link word } ":"
893 { $example "USE: classes" "\\ f class ." "word" }
894 "On the other hand, " { $link t } " is just a word, and there is no class which it is a unique instance of."
895 { $example "t \\ t eq? ." "t" }
896 "Many words which search collections confuse the case of no element being present with an element being found equal to " { $link f } ". If this distinction is imporant, there is usually an alternative word which can be used; for example, compare " { $link at } " with " { $link at* } "." ;
897
898 ARTICLE: "conditionals-boolean-equivalence" "Expressing conditionals with boolean logic"
899 "Certain simple conditional forms can be expressed in a simpler manner using boolean logic."
900 $nl
901 "The following two lines are equivalent:"
902 { $code "[ drop f ] unless" "swap and" }
903 "The following two lines are equivalent:"
904 { $code "[ ] [ ] ?if" "swap or" }
905 "The following two lines are equivalent, where " { $snippet "L" } " is a literal:"
906 { $code "[ L ] unless*" "L or" } ;
907
908 ARTICLE: "conditionals" "Conditionals and logic"
909 "The basic conditionals:"
910 { $subsection if }
911 { $subsection when }
912 { $subsection unless }
913 "Forms abstracting a common stack shuffle pattern:"
914 { $subsection if* }
915 { $subsection when* }
916 { $subsection unless* }
917 "Another form abstracting a common stack shuffle pattern:"
918 { $subsection ?if }
919 "Sometimes instead of branching, you just need to pick one of two values:"
920 { $subsection ? }
921 "There are some logical operations on booleans:"
922 { $subsection >boolean }
923 { $subsection not }
924 { $subsection and }
925 { $subsection or }
926 { $subsection xor }
927 { $subsection "conditionals-boolean-equivalence" }
928 "See " { $link "combinators" } " for forms which abstract away common patterns involving multiple nested branches."
929 { $see-also "booleans" "bitwise-arithmetic" both? either? } ;
930
931 ARTICLE: "equality" "Equality"
932 "There are two distinct notions of “sameness” when it comes to objects."
933 $nl
934 "You can test if two references point to the same object (" { $emphasis "identity comparison" } "). This is rarely used; it is mostly useful with large, mutable objects where the object identity matters but the value is transient:"
935 { $subsection eq? }
936 "You can test if two objects are equal in a domain-specific sense, usually by being instances of the same class, and having equal slot values (" { $emphasis "value comparison" } "):"
937 { $subsection = }
938 "A third form of equality is provided by " { $link number= } ". It compares numeric value while disregarding types."
939 $nl
940 "Custom value comparison methods for use with " { $link = } " can be defined on a generic word:"
941 { $subsection equal? }
942 "Utility class:"
943 { $subsection identity-tuple }
944 "An object can be cloned; the clone has distinct identity but equal value:"
945 { $subsection clone } ;
946
947 ARTICLE: "assertions" "Assertions"
948 "Some words to make assertions easier to enforce:"
949 { $subsection assert }
950 { $subsection assert= } ;
951
952 ARTICLE: "dataflow-combinators" "Data flow combinators"
953 "Data flow combinators pass values between quotations:"
954 { $subsection "slip-keep-combinators" }
955 { $subsection "cleave-combinators" }
956 { $subsection "spread-combinators" }
957 { $subsection "apply-combinators" } ;
958
959 ARTICLE: "dataflow" "Data and control flow"
960 { $subsection "evaluator" }
961 { $subsection "words" }
962 { $subsection "effects" }
963 { $subsection "booleans" }
964 { $subsection "shuffle-words" }
965 "A central concept in Factor is that of a " { $emphasis "combinator" } ", which is a word taking code as input."
966 { $subsection "dataflow-combinators" }
967 { $subsection "conditionals" }
968 { $subsection "looping-combinators" }
969 { $subsection "compositional-combinators" }
970 { $subsection "combinators" }
971 "More combinators are defined for working on data structures, such as " { $link "sequences-combinators" } " and " { $link "assocs-combinators" } "."
972 $nl
973 "Advanced topics:"
974 { $subsection "assertions" }
975 { $subsection "implementing-combinators" }
976 { $subsection "macros" }
977 { $subsection "errors" }
978 { $subsection "continuations" } ;
979
980 ABOUT: "dataflow"
981