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