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