]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel-docs.factor
kernel: new combinator 2with = with with
[factor.git] / core / kernel / kernel-docs.factor
1 USING: arrays classes combinators help.markup help.syntax
2 kernel.private layouts math quotations words ;
3 IN: kernel
4
5 HELP: eq?
6 { $values { "obj1" object } { "obj2" object } { "?" boolean } }
7 { $description "Tests if two references point at the same object." } ;
8
9 HELP: drop  $shuffle ;
10 HELP: 2drop $shuffle ;
11 HELP: 3drop $shuffle ;
12 HELP: 4drop $shuffle ;
13 HELP: dup   $shuffle ;
14 HELP: 2dup  $shuffle ;
15 HELP: 3dup  $shuffle ;
16 HELP: 4dup  $shuffle ;
17 HELP: nip   $shuffle ;
18 HELP: 2nip  $shuffle ;
19 HELP: over  $shuffle ;
20 HELP: 2over $shuffle ;
21 HELP: pick  $shuffle ;
22 HELP: swap  $shuffle ;
23
24 HELP: rot   $complex-shuffle ;
25 HELP: -rot  $complex-shuffle ;
26 HELP: dupd  $complex-shuffle ;
27 HELP: swapd $complex-shuffle ;
28
29 HELP: datastack
30 { $values { "array" array } }
31 { $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." } ;
32
33 HELP: set-datastack
34 { $values { "array" array } }
35 { $description "Replaces the data stack contents with a copy of an array. The end of the array becomes the top of the stack." } ;
36
37 HELP: retainstack
38 { $values { "array" array } }
39 { $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." } ;
40
41 HELP: set-retainstack
42 { $values { "array" array } }
43 { $description "Replaces the retain stack contents with a copy of an array. The end of the array becomes the top of the stack." } ;
44
45 HELP: callstack
46 { $values { "callstack" callstack } }
47 { $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." } ;
48
49 HELP: set-callstack
50 { $values { "callstack" callstack } }
51 { $description "Replaces the call stack contents. Control flow is transferred immediately to the innermost frame of the new call stack." } ;
52
53 HELP: clear
54 { $description "Clears the data stack." } ;
55
56 HELP: build
57 { $values { "n" integer } }
58 { $description "The current build number. Factor increments this number whenever a new boot image is created." } ;
59
60 HELP: hashcode*
61 { $values { "depth" integer } { "obj" object } { "code" fixnum } }
62 { $contract "Outputs the hashcode of an object. The hashcode operation must satisfy the following properties:"
63 { $list
64     { "If two objects are equal under " { $link = } ", they must have equal hashcodes." }
65     { "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," }
66     { "The hashcode should be a " { $link fixnum } ", however returning a " { $link bignum } " will not cause any problems other than potential performance degradation." }
67     { "The hashcode is only permitted to change between two invocations if the object or one of its slot values was mutated." }
68 }
69 "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." } ;
70
71 HELP: hashcode
72 { $values { "obj" object } { "code" fixnum } }
73 { $description "Computes the hashcode of an object with a default hashing depth. See " { $link hashcode* } " for the hashcode contract." } ;
74
75 HELP: identity-hashcode
76 { $values { "obj" object } { "code" fixnum } }
77 { $description "Outputs the identity hashcode of an object. The identity hashcode is not guaranteed to be unique, however it will not change during the object's lifetime." } ;
78
79 { hashcode hashcode* identity-hashcode } related-words
80
81 HELP: =
82 { $values { "obj1" object } { "obj2" object } { "?" boolean } }
83 { $description
84     "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."
85 }
86 { $examples
87     { $example "USING: kernel prettyprint ;" "5 5 = ." "t" }
88     { $example "USING: kernel prettyprint ;" "5 005 = ." "t" }
89     { $example "USING: kernel prettyprint ;" "5 5.0 = ." "f" }
90     { $example "USING: arrays kernel prettyprint ;" "{ \"a\" \"b\" } \"a\" \"b\" 2array = ." "t" }
91     { $example "USING: arrays kernel prettyprint ;" "{ \"a\" \"b\" } [ \"a\" \"b\" ] = ." "f" }
92 } ;
93
94 HELP: equal?
95 { $values { "obj1" object } { "obj2" object } { "?" boolean } }
96 { $contract
97     "Tests if two objects are equal."
98     $nl
99     "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."
100     $nl
101     "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:"
102     { $list
103         { { $snippet "a = b" } " implies " { $snippet "b = a" } }
104         { { $snippet "a = b" } " and " { $snippet "b = c" } " implies " { $snippet "a = c" } }
105     }
106     "If a class defines a custom equality comparison test, it should also define a compatible method for the " { $link hashcode* } " generic word."
107 }
108 { $examples
109     "An example demonstrating why this word should only be used to define methods on, and never called directly:"
110     { $example "USING: kernel prettyprint ;" "5 5 equal? ." "f" }
111     "Using " { $link = } " gives the expected behavior:"
112     { $example "USING: kernel prettyprint ;" "5 5 = ." "t" }
113 } ;
114
115 HELP: identity-tuple
116 { $class-description "A class defining an " { $link equal? } " method which always returns f." }
117 { $examples
118     "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:"
119     { $code "TUPLE: foo < identity-tuple ;" }
120     "By calling " { $link = } " on instances of " { $snippet "foo" } " we get the results we expect:"
121     { $unchecked-example "T{ foo } dup = ." "t" }
122     { $unchecked-example "T{ foo } dup clone = ." "f" }
123 } ;
124
125 HELP: clone
126 { $values { "obj" object } { "cloned" "a new object" } }
127 { $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." } ;
128
129 HELP: ?
130 { $values { "?" "a generalized boolean" } { "true" object } { "false" object } { "true/false" "one two input objects" } }
131 { $description "Chooses between two values depending on the boolean value of " { $snippet "cond" } "." } ;
132
133 HELP: boolean
134 { $class-description "A union of the " { $link POSTPONE: t } " and " { $link POSTPONE: f } " classes." } ;
135
136 HELP: >boolean
137 { $values { "obj" "a generalized boolean" } { "?" boolean } }
138 { $description "Convert a generalized boolean into a boolean. That is, " { $link f } " retains its value, whereas anything else becomes " { $link t } "." } ;
139
140 HELP: not
141 { $values { "obj" "a generalized boolean" } { "?" boolean } }
142 { $description "For " { $link f } " outputs " { $link t } " and for anything else outputs " { $link f } "." }
143 { $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." } ;
144
145 HELP: and
146 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "?" "a generalized boolean" } }
147 { $description "If both inputs are true, outputs " { $snippet "obj2" } ". otherwise outputs " { $link f } "." }
148 { $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." }
149 { $examples
150     "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:"
151     { $example "USING: kernel prettyprint ;" "t f and ." "f" }
152     { $example "USING: kernel prettyprint ;" "t 7 and ." "7" }
153     { $example "USING: kernel prettyprint ;" "\"hi\" 12.0 and ." "12.0" }
154 } ;
155
156 HELP: or
157 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "?" "a generalized boolean" } }
158 { $description "If both inputs are false, outputs " { $link f } ". otherwise outputs the first of " { $snippet "obj1" } " and " { $snippet "obj2" } " which is true." }
159 { $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." }
160 { $examples
161     "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:"
162     { $example "USING: kernel prettyprint ;" "t f or ." "t" }
163     { $example "USING: kernel prettyprint ;" "\"hi\" 12.0 or ." "\"hi\"" }
164 } ;
165
166 HELP: xor
167 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "?" "a generalized boolean" } }
168 { $description "If exactly one input is false, outputs the other input. Otherwise outputs " { $link f } "." }
169 { $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." } ;
170
171 HELP: both?
172 { $values { "x" object } { "y" object } { "quot" { $quotation ( ... obj -- ... ? ) } } { "?" boolean } }
173 { $description "Tests if the quotation yields a true value when applied to both " { $snippet "x" } " and " { $snippet "y" } "." }
174 { $examples
175     { $example "USING: kernel math prettyprint ;" "3 5 [ odd? ] both? ." "t" }
176     { $example "USING: kernel math prettyprint ;" "12 7 [ even? ] both? ." "f" }
177 } ;
178
179 HELP: either?
180 { $values { "x" object } { "y" object } { "quot" { $quotation ( ... obj -- ... ? ) } } { "?" boolean } }
181 { $description "Tests if the quotation yields a true value when applied to either " { $snippet "x" } " or " { $snippet "y" } "." }
182 { $examples
183     { $example "USING: kernel math prettyprint ;" "3 6 [ odd? ] either? ." "t" }
184     { $example "USING: kernel math prettyprint ;" "5 7 [ even? ] either? ." "f" }
185 } ;
186
187 HELP: same?
188 { $values { "x" object } { "y" object } { "quot" { $quotation ( ... obj -- ... obj' ) } } { "?" boolean } }
189 { $description "Applies the quotation to both " { $snippet "x" } " and " { $snippet "y" } ", and then checks if the results are equal." }
190 { $examples
191     { $example "USING: kernel math prettyprint ;" "4 5 [ 2/ ] same? ." "t" }
192     { $example "USING: kernel math prettyprint ;" "3 7 [ sq ] same? ." "f" }
193 } ;
194
195 HELP: execute
196 { $values { "word" word } }
197 { $description "Executes a word. Words which " { $link execute } " an input parameter must be declared " { $link POSTPONE: inline } " so that a caller which passes in a literal word can have a static stack effect." }
198 { $examples
199     { $example "USING: kernel io words ;" "IN: scratchpad" ": twice ( word -- ) dup execute execute ; inline\n: hello ( -- ) \"Hello\" print ;\n\\ hello twice" "Hello\nHello" }
200 } ;
201
202 { execute POSTPONE: execute( } related-words
203
204 HELP: (execute)
205 { $values { "word" word } }
206 { $description "Executes a word without checking if it is a word first." }
207 { $warning "This word is in the " { $vocab-link "kernel.private" } " vocabulary because it is unsafe. Calling with a parameter that is not a word will crash Factor. Use " { $link execute } " instead." } ;
208
209 HELP: call
210 { $values { "callable" callable } }
211 { $description "Calls a quotation. Words which " { $link call } " an input parameter must be declared " { $link POSTPONE: inline } " so that a caller which passes in a literal quotation can have a static stack effect." }
212 { $examples
213     "The following two lines are equivalent:"
214     { $code "2 [ 2 + 3 * ] call" "2 2 + 3 *" }
215 } ;
216
217 { call POSTPONE: call( } related-words
218
219 HELP: keep
220 { $values { "x" object } { "quot" { $quotation ( ..a x -- ..b ) } } }
221 { $description "Call a quotation with a value on the stack, restoring the value when the quotation returns." }
222 { $examples
223     { $example "USING: arrays kernel prettyprint ;" "2 \"greetings\" [ <array> ] keep 2array ." "{ { \"greetings\" \"greetings\" } \"greetings\" }" }
224 } ;
225
226 HELP: 2keep
227 { $values { "x" object } { "y" object } { "quot" { $quotation ( ..a x y -- ..b ) } } }
228 { $description "Call a quotation with two values on the stack, restoring the values when the quotation returns." } ;
229
230 HELP: 3keep
231 { $values { "x" object } { "y" object } { "z" object } { "quot" { $quotation ( ..a x y z -- ..b ) } } }
232 { $description "Call a quotation with three values on the stack, restoring the values when the quotation returns." } ;
233
234 HELP: bi
235 { $values { "x" object } { "p" { $quotation ( ..a x -- ..b ) } } { "q" { $quotation ( ..c x -- ..d ) } } }
236 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "x" } "." }
237 { $examples
238     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x -- )" } ", then the following two lines are equivalent:"
239     { $code
240         "[ p ] [ q ] bi"
241         "dup p q"
242     }
243     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x -- y )" } ", then the following two lines are equivalent:"
244     { $code
245         "[ p ] [ q ] bi"
246         "dup p swap q"
247     }
248     "In general, the following two lines are equivalent:"
249     { $code
250         "[ p ] [ q ] bi"
251         "[ p ] keep q"
252     }
253
254 } ;
255
256 HELP: 2bi
257 { $values { "x" object } { "y" object } { "p" { $quotation ( x y -- ... ) } } { "q" { $quotation ( x y -- ... ) } } }
258 { $description "Applies " { $snippet "p" } " to the two input values, then applies " { $snippet "q" } " to the two input values." }
259 { $examples
260     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x y -- )" } ", then the following two lines are equivalent:"
261     { $code
262         "[ p ] [ q ] 2bi"
263         "2dup p q"
264     }
265     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x y -- z )" } ", then the following two lines are equivalent:"
266     { $code
267         "[ p ] [ q ] 2bi"
268         "2dup p -rot q"
269     }
270     "In general, the following two lines are equivalent:"
271     { $code
272         "[ p ] [ q ] 2bi"
273         "[ p ] 2keep q"
274     }
275 } ;
276
277 HELP: 3bi
278 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation ( x y z -- ... ) } } { "q" { $quotation ( x y z -- ... ) } } }
279 { $description "Applies " { $snippet "p" } " to the three input values, then applies " { $snippet "q" } " to the three input values." }
280 { $examples
281     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x y z -- )" } ", then the following two lines are equivalent:"
282     { $code
283         "[ p ] [ q ] 3bi"
284         "3dup p q"
285     }
286     "In general, the following two lines are equivalent:"
287     { $code
288         "[ p ] [ q ] 3bi"
289         "[ p ] 3keep q"
290     }
291 } ;
292
293 HELP: tri
294 { $values { "x" object } { "p" { $quotation ( x -- ... ) } } { "q" { $quotation ( x -- ... ) } } { "r" { $quotation ( x -- ... ) } } }
295 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "x" } ", and finally applies " { $snippet "r" } " to " { $snippet "x" } "." }
296 { $examples
297     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x -- )" } ", then the following two lines are equivalent:"
298     { $code
299         "[ p ] [ q ] [ r ] tri"
300         "dup p dup q r"
301     }
302     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x -- y )" } ", then the following two lines are equivalent:"
303     { $code
304         "[ p ] [ q ] [ r ] tri"
305         "dup p over q rot r"
306     }
307     "In general, the following two lines are equivalent:"
308     { $code
309         "[ p ] [ q ] [ r ] tri"
310         "[ p ] keep [ q ] keep r"
311     }
312 } ;
313
314 HELP: 2tri
315 { $values { "x" object } { "y" object } { "p" { $quotation ( x y -- ... ) } } { "q" { $quotation ( x y -- ... ) } } { "r" { $quotation ( x y -- ... ) } } }
316 { $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." }
317 { $examples
318     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x y -- )" } ", then the following two lines are equivalent:"
319     { $code
320         "[ p ] [ q ] [ r ] 2tri"
321         "2dup p 2dup q r"
322     }
323     "In general, the following two lines are equivalent:"
324     { $code
325         "[ p ] [ q ] [ r ] 2tri"
326         "[ p ] 2keep [ q ] 2keep r"
327     }
328 } ;
329
330 HELP: 3tri
331 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation ( x y z -- ... ) } } { "q" { $quotation ( x y z -- ... ) } } { "r" { $quotation ( x y z -- ... ) } } }
332 { $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." }
333 { $examples
334     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x y z -- )" } ", then the following two lines are equivalent:"
335     { $code
336         "[ p ] [ q ] [ r ] 3tri"
337         "3dup p 3dup q r"
338     }
339     "In general, the following two lines are equivalent:"
340     { $code
341         "[ p ] [ q ] [ r ] 3tri"
342         "[ p ] 3keep [ q ] 3keep r"
343     }
344 } ;
345
346
347 HELP: bi*
348 { $values { "x" object } { "y" object } { "p" { $quotation ( x -- ... ) } } { "q" { $quotation ( y -- ... ) } } }
349 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } "." }
350 { $examples
351     "The following two lines are equivalent:"
352     { $code
353         "[ p ] [ q ] bi*"
354         "[ p ] dip q"
355     }
356 } ;
357
358 HELP: 2bi*
359 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "p" { $quotation ( w x -- ... ) } } { "q" { $quotation ( y z -- ... ) } } }
360 { $description "Applies " { $snippet "p" } " to " { $snippet "w" } " and " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } " and " { $snippet "z" } "." }
361 { $examples
362     "The following two lines are equivalent:"
363     { $code
364         "[ p ] [ q ] 2bi*"
365         "[ p ] 2dip q"
366     }
367 } ;
368
369 HELP: 2tri*
370 { $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 -- ... ) } } }
371 { $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" } "." }
372 { $examples
373     "The following two lines are equivalent:"
374     { $code
375         "[ p ] [ q ] [ r ] 2tri*"
376         "[ [ p ] 2dip q ] 2dip r"
377     }
378 } ;
379
380 HELP: tri*
381 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation ( x -- ... ) } } { "q" { $quotation ( y -- ... ) } } { "r" { $quotation ( z -- ... ) } } }
382 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } ", and finally applies " { $snippet "r" } " to " { $snippet "z" } "." }
383 { $examples
384     "The following two lines are equivalent:"
385     { $code
386         "[ p ] [ q ] [ r ] tri*"
387         "[ [ p ] dip q ] dip r"
388     }
389 } ;
390
391 HELP: bi@
392 { $values { "x" object } { "y" object } { "quot" { $quotation ( obj -- ... ) } } }
393 { $description "Applies the quotation to " { $snippet "x" } ", then to " { $snippet "y" } "." }
394 { $examples
395     "The following two lines are equivalent:"
396     { $code
397         "[ p ] bi@"
398         "[ p ] dip p"
399     }
400     "The following two lines are also equivalent:"
401     { $code
402         "[ p ] bi@"
403         "[ p ] [ p ] bi*"
404     }
405 } ;
406
407 HELP: 2bi@
408 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "quot" { $quotation ( obj1 obj2 -- ... ) } } }
409 { $description "Applies the quotation to " { $snippet "w" } " and " { $snippet "x" } ", then to " { $snippet "y" } " and " { $snippet "z" } "." }
410 { $examples
411     "The following two lines are equivalent:"
412     { $code
413         "[ p ] 2bi@"
414         "[ p ] 2dip p"
415     }
416     "The following two lines are also equivalent:"
417     { $code
418         "[ p ] 2bi@"
419         "[ p ] [ p ] 2bi*"
420     }
421 } ;
422
423 HELP: tri@
424 { $values { "x" object } { "y" object } { "z" object } { "quot" { $quotation ( obj -- ... ) } } }
425 { $description "Applies the quotation to " { $snippet "x" } ", then to " { $snippet "y" } ", and finally to " { $snippet "z" } "." }
426 { $examples
427     "The following two lines are equivalent:"
428     { $code
429         "[ p ] tri@"
430         "[ [ p ] dip p ] dip p"
431     }
432     "The following two lines are also equivalent:"
433     { $code
434         "[ p ] tri@"
435         "[ p ] [ p ] [ p ] tri*"
436     }
437 } ;
438
439 HELP: 2tri@
440 { $values { "u" object } { "v" object } { "w" object } { "x" object } { "y" object } { "z" object } { "quot" { $quotation ( obj1 obj2 -- ... ) } } }
441 { $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" } "." }
442 { $examples
443     "The following two lines are equivalent:"
444     { $code
445         "[ p ] 2tri@"
446         "[ [ p ] 2dip p ] 2dip p"
447     }
448     "The following two lines are also equivalent:"
449     { $code
450         "[ p ] 2tri@"
451         "[ p ] [ p ] [ p ] 2tri*"
452     }
453 } ;
454
455 HELP: bi-curry
456 { $values { "x" object } { "p" { $quotation ( x -- ... ) } } { "q" { $quotation ( x -- ... ) } } { "p'" { $snippet "[ x p ]" } } { "q'" { $snippet "[ x q ]" } } }
457 { $description "Partially applies " { $snippet "p" } " and " { $snippet "q" } " to " { $snippet "x" } "." }
458 { $notes
459   "The following two lines are equivalent:"
460   { $code
461     "[ p ] [ q ] bi-curry [ call ] bi@"
462     "[ p ] [ q ] bi"
463   }
464   "Higher-arity variants of " { $link bi } " can be built from " { $link bi-curry } ":"
465   { $code
466     "[ p ] [ q ] bi-curry bi == [ p ] [ q ] 2bi"
467     "[ p ] [ q ] bi-curry bi-curry bi == [ p ] [ q ] 3bi"
468   }
469   "The combination " { $snippet "bi-curry bi*" } " cannot be expressed with the non-currying dataflow combinators alone; it is equivalent to a stack shuffle preceding " { $link 2bi* } ":"
470   { $code
471     "[ p ] [ q ] bi-curry bi*"
472     "[ swap ] keep [ p ] [ q ] 2bi*"
473   }
474   "To put it another way, " { $snippet "bi-curry bi*" } " handles the case where you have three values " { $snippet "a b c" } " on the stack, and you wish to apply " { $snippet "p" } " to " { $snippet "a c" } " and " { $snippet "q" } " to " { $snippet "b c" } "."
475 } ;
476
477 HELP: tri-curry
478 { $values
479   { "x" object }
480   { "p" { $quotation ( x -- ... ) } }
481   { "q" { $quotation ( x -- ... ) } }
482   { "r" { $quotation ( x -- ... ) } }
483   { "p'" { $snippet "[ x p ]" } }
484   { "q'" { $snippet "[ x q ]" } }
485   { "r'" { $snippet "[ x r ]" } }
486 }
487 { $description "Partially applies " { $snippet "p" } ", " { $snippet "q" } " and " { $snippet "r" } " to " { $snippet "x" } "." }
488 { $notes
489   "The following two lines are equivalent:"
490   { $code
491     "[ p ] [ q ] [ r ] tri-curry [ call ] tri@"
492     "[ p ] [ q ] [ r ] tri"
493   }
494   "Higher-arity variants of " { $link tri } " can be built from " { $link tri-curry } ":"
495   { $code
496     "[ p ] [ q ] [ r ] tri-curry tri == [ p ] [ q ] [ r ] 2tri"
497     "[ p ] [ q ] [ r ] tri-curry tri-curry bi == [ p ] [ q ] [ r ] 3tri"
498   }
499   "The combination " { $snippet "tri-curry tri*" } " cannot be expressed with the non-currying dataflow combinators alone; it handles the case where you have four values " { $snippet "a b c d" } " on the stack, and you wish to apply " { $snippet "p" } " to " { $snippet "a d" } ", " { $snippet "q" } " to " { $snippet "b d" } " and " { $snippet "r" } " to " { $snippet "c d" } "." } ;
500
501 HELP: bi-curry*
502 { $values { "x" object } { "y" object } { "p" { $quotation ( x -- ... ) } } { "q" { $quotation ( y -- ... ) } } { "p'" { $snippet "[ x p ]" } } { "q'" { $snippet "[ y q ]" } } }
503 { $description "Partially applies " { $snippet "p" } " to " { $snippet "x" } ", and " { $snippet "q" } " to " { $snippet "y" } "." }
504 { $notes
505   "The following two lines are equivalent:"
506   { $code
507     "[ p ] [ q ] bi-curry* [ call ] bi@"
508     "[ p ] [ q ] bi*"
509   }
510   "The combination " { $snippet "bi-curry* bi" } " is equivalent to a stack shuffle preceding " { $link 2bi* } ":"
511   { $code
512     "[ p ] [ q ] bi-curry* bi"
513     "[ over ] dip [ p ] [ q ] 2bi*"
514   }
515   "In other words, " { $snippet "bi-curry* bi" } " handles the case where you have the three values " { $snippet "a b c" } " on the stack, and you wish to apply " { $snippet "p" } " to " { $snippet "a b" } " and " { $snippet "q" } " to " { $snippet "a c" } "."
516   $nl
517   "The combination " { $snippet "bi-curry* bi*" } " is equivalent to a stack shuffle preceding " { $link 2bi* } ":"
518   { $code
519     "[ p ] [ q ] bi-curry* bi*"
520     "[ swap ] dip [ p ] [ q ] 2bi*"
521   }
522   "In other words, " { $snippet "bi-curry* bi*" } " handles the case where you have the four values " { $snippet "a b c d" } " on the stack, and you wish to apply " { $snippet "p" } " to " { $snippet "a c" } " and " { $snippet "q" } " to " { $snippet "b d" } "."
523
524 } ;
525
526 HELP: tri-curry*
527 { $values
528   { "x" object }
529   { "y" object }
530   { "z" object }
531   { "p" { $quotation ( x -- ... ) } }
532   { "q" { $quotation ( y -- ... ) } }
533   { "r" { $quotation ( z -- ... ) } }
534   { "p'" { $snippet "[ x p ]" } }
535   { "q'" { $snippet "[ y q ]" } }
536   { "r'" { $snippet "[ z r ]" } }
537 }
538 { $description "Partially applies " { $snippet "p" } " to " { $snippet "x" } ", " { $snippet "q" } " to " { $snippet "y" } " and " { $snippet "r" } " to " { $snippet "z" } "." }
539 { $notes
540   "The following two lines are equivalent:"
541   { $code
542     "[ p ] [ q ] [ r ] tri-curry* [ call ] tri@"
543     "[ p ] [ q ] [ r ] tri*"
544   }
545   "The combination " { $snippet "tri-curry* tri" } " is equivalent to a stack shuffle preceding " { $link 2tri* } ":"
546   { $code
547     "[ p ] [ q ] [ r ] tri-curry* tri"
548     "[ [ over ] dip over ] dip [ p ] [ q ] [ r ] 2tri*"
549   }
550 } ;
551
552 HELP: bi-curry@
553 { $values { "x" object } { "y" object } { "q" { $quotation ( obj -- ... ) } } { "p'" { $snippet "[ x q ]" } } { "q'" { $snippet "[ y q ]" } } }
554 { $description "Partially applies " { $snippet "q" } " to " { $snippet "x" } " and " { $snippet "y" } "." }
555 { $notes
556   "The following two lines are equivalent:"
557   { $code
558     "[ q ] bi-curry@"
559     "[ q ] [ q ] bi-curry*"
560   }
561 } ;
562
563 HELP: tri-curry@
564 { $values
565   { "x" object }
566   { "y" object }
567   { "z" object }
568   { "q" { $quotation ( obj -- ... ) } }
569   { "p'" { $snippet "[ x q ]" } }
570   { "q'" { $snippet "[ y q ]" } }
571   { "r'" { $snippet "[ z q ]" } }
572 }
573 { $description "Partially applies " { $snippet "q" } " to " { $snippet "x" } ", " { $snippet "y" } " and " { $snippet "z" } "." }
574 { $notes
575   "The following two lines are equivalent:"
576   { $code
577     "[ q ] tri-curry@"
578     "[ q ] [ q ] [ q ] tri-curry*"
579   }
580 } ;
581
582 HELP: if
583 { $values { "?" "a generalized boolean" } { "true" quotation } { "false" quotation } }
584 { $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation. Otherwise calls the " { $snippet "true" } " quotation."
585 $nl
586 "The " { $snippet "cond" } " value is removed from the stack before either quotation is called." }
587 { $examples
588     { $example
589         "USING: io kernel math ;"
590         "10 3 < [ \"Math is broken\" print ] [ \"Math is good\" print ] if"
591         "Math is good"
592     }
593 }
594 { $notes { $snippet "if" } " is executed as a primitive when preceded by two literal quotations. The below definition is not executed unless one of its arguments is a non-literal quotation, such as a quotation constructed with " { $link curry } " or " { $link compose } ", or for " { $link "fry" } " or quotations including " { $link "locals" } "." } ;
595
596 HELP: when
597 { $values { "?" "a generalized boolean" } { "true" quotation } }
598 { $description "If " { $snippet "cond" } " is not " { $link f } ", calls the " { $snippet "true" } " quotation."
599 $nl
600 "The " { $snippet "cond" } " value is removed from the stack before the quotation is called." }
601 { $examples
602     { $example
603         "USING: kernel math prettyprint ;"
604         "-5 dup 0 < [ 3 + ] when ."
605         "-2"
606     }
607 } ;
608
609 HELP: unless
610 { $values { "?" "a generalized boolean" } { "false" quotation } }
611 { $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation."
612 $nl
613 "The " { $snippet "cond" } " value is removed from the stack before the quotation is called." }
614 { $examples
615     { $example
616         "USING: kernel math prettyprint sequences ;"
617         "IN: scratchpad"
618         ""
619         "CONSTANT: american-cities {"
620         "    \"San Francisco\""
621         "    \"Los Angeles\""
622         "    \"New York\""
623         "}"
624         ""
625         ": add-tax ( price city -- price' )"
626         "    american-cities member? [ 1.1 * ] unless ;"
627         ""
628         "123 \"Ottawa\" add-tax ."
629         "135.3"
630     }
631 } ;
632
633 HELP: if*
634 { $values { "?" "a generalized boolean" } { "true" { $quotation ( ..a ? -- ..b ) } } { "false" { $quotation ( ..a -- ..b ) } } }
635 { $description "Alternative conditional form that preserves the " { $snippet "cond" } " value if it is true."
636 $nl
637 "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."
638 $nl
639 "The following two lines are equivalent:"
640 { $code "X [ Y ] [ Z ] if*" "X dup [ Y ] [ drop Z ] if" } }
641 { $examples
642     "Notice how in this example, the same value is tested by the conditional, and then used in the true branch; the false branch does not need to drop the value because of how " { $link if* } " works:"
643     { $example
644         "USING: assocs io kernel math.parser ;"
645         "IN: scratchpad"
646         ""
647         ": curry-price ( meat -- price )
648     {
649         { \"Beef\" 10 }
650         { \"Chicken\" 12 }
651         { \"Lamb\" 13 }
652     } at ;
653
654 : order-curry ( meat -- )
655     curry-price [
656         \"Your order will be \" write
657         number>string write
658         \" dollars.\" write
659     ] [ \"Invalid order.\" print ] if* ;"
660         ""
661         "\"Deer\" order-curry"
662         "Invalid order."
663     }
664 } ;
665
666 HELP: when*
667 { $values { "?" "a generalized boolean" } { "true" { $quotation ( ..a ? -- ..a ) } } }
668 { $description "Variant of " { $link if* } " with no false quotation."
669 $nl
670 "The following two lines are equivalent:"
671 { $code "X [ Y ] when*" "X dup [ Y ] [ drop ] if" } } ;
672
673 HELP: unless*
674 { $values { "?" "a generalized boolean" } { "false" { $quotation ( ..a -- ..a x ) } } { "x" object } }
675 { $description "Variant of " { $link if* } " with no true quotation." }
676 { $notes
677 "The following two lines are equivalent:"
678 { $code "X [ Y ] unless*" "X dup [ ] [ drop Y ] if" } } ;
679
680 HELP: ?if
681 { $values { "default" object } { "cond" "a generalized boolean" } { "true" { $quotation ( ..a cond -- ..b ) } } { "false" { $quotation ( ..a default -- ..b ) } } }
682 { $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." }
683 { $notes
684 "The following two lines are equivalent:"
685 { $code "[ X ] [ Y ] ?if" "dup [ nip X ] [ drop Y ] if" }
686 "The following two lines are equivalent:"
687 { $code "[ ] [ ] ?if" "swap or" } } ;
688
689 HELP: die
690 { $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." }
691 { $notes
692     "The term FEP originates from the Lisp machines of old. According to the Jargon File,"
693     $nl
694     { $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'."
695     $nl
696     { $url "http://www.jargon.net/jargonfile/f/feppedout.html" }
697 } ;
698
699 HELP: (clone)
700 { $values { "obj" object } { "newobj" "a shallow copy" } }
701 { $description "Outputs a byte-by-byte copy of the given object. User code should call " { $link clone } " instead." } ;
702
703 HELP: declare
704 { $values { "spec" "an array of class words" } }
705 { $description "Declares that the elements at the top of the stack are instances of the classes in " { $snippet "spec" } "." }
706 { $warning "The compiler blindly trusts declarations, and false declarations can lead to crashes, memory corruption and other undesirable behavior." }
707 { $examples
708     "The optimizer cannot do anything with the below code:"
709     { $code "2 + 10 *" }
710     "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:"
711     { $code "{ float } declare 2 + 10 *" }
712 } ;
713
714 HELP: tag
715 { $values { "object" object } { "n" "a tag number" } }
716 { $description "Outputs an object's tag number, between zero and one less than " { $link num-types } ". This is implementation detail and user code should call " { $link class } " instead." } ;
717
718 HELP: special-object
719 { $values { "n" "a non-negative integer" } { "obj" object } }
720 { $description "Reads an object from the Factor VM's special object table. User code never has to read the special object table directly; instead, use one of the callers of this word." } ;
721
722 HELP: set-special-object
723 { $values { "obj" object } { "n" "a non-negative integer" } }
724 { $description "Writes an object to the Factor VM's special object table. User code never has to write to the special object table directly; instead, use one of the callers of this word." } ;
725
726 HELP: object
727 { $class-description
728     "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:"
729     { $code "GENERIC: enclose ( number -- array )" "M: number enclose 1array ;" "M: object enclose ;" }
730 } ;
731
732 HELP: null
733 { $class-description
734     "The canonical empty class with no instances."
735 }
736 { $notes
737     "Unlike " { $snippet "null" } " in Java or " { $snippet "NULL" } " in C++, this is not a value signifying empty, or nothing. Use " { $link f } " for this purpose."
738 } ;
739
740 HELP: most
741 { $values { "x" object } { "y" object } { "quot" { $quotation ( x y -- ? ) } } { "z" "either " { $snippet "x" } " or " { $snippet "y" } } }
742 { $description "If the quotation yields a true value when applied to " { $snippet "x" } " and " { $snippet "y" } ", outputs " { $snippet "x" } ", otherwise outputs " { $snippet "y" } "." } ;
743
744 HELP: curry
745 { $values { "obj" object } { "quot" callable } { "curry" curry } }
746 { $description "Partial application. Outputs a " { $link callable } " which first pushes " { $snippet "obj" } " and then calls " { $snippet "quot" } "." }
747 { $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." }
748 { $notes "Even if " { $snippet "obj" } " is a word, it will be pushed as a literal."
749 $nl
750 "This operation is efficient and does not copy the quotation." }
751 { $examples
752     { $example "USING: kernel prettyprint ;" "5 [ . ] curry ." "[ 5 . ]" }
753     { $example "USING: kernel prettyprint see ;" "\\ = [ see ] curry ." "[ \\ = see ]" }
754     { $example "USING: kernel math prettyprint sequences ;" "{ 1 2 3 } 2 [ - ] curry map ." "{ -1 0 1 }" }
755 } ;
756
757 HELP: 2curry
758 { $values { "obj1" object } { "obj2" object } { "quot" callable } { "curry" curry } }
759 { $description "Outputs a " { $link callable } " which pushes " { $snippet "obj1" } " and " { $snippet "obj2" } " and then calls " { $snippet "quot" } "." }
760 { $notes "This operation is efficient and does not copy the quotation." }
761 { $examples
762     { $example "USING: kernel math prettyprint ;" "5 4 [ + ] 2curry ." "[ 5 4 + ]" }
763 } ;
764
765 HELP: 3curry
766 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "quot" callable } { "curry" curry } }
767 { $description "Outputs a " { $link callable } " which pushes " { $snippet "obj1" } ", " { $snippet "obj2" } " and " { $snippet "obj3" } ", and then calls " { $snippet "quot" } "." }
768 { $notes "This operation is efficient and does not copy the quotation." } ;
769
770 HELP: with
771 { $values { "param" object } { "obj" object } { "quot" { $quotation ( param elt -- ... ) } } { "curry" curry } }
772 { $description "Partial application on the left. The following two lines are equivalent:"
773     { $code "swap [ swap A ] curry B" }
774     { $code "[ A ] with B" }
775
776 }
777 { $notes "This operation is efficient and does not copy the quotation." }
778 { $examples
779     { $example "USING: kernel math prettyprint sequences ;" "1 { 1 2 3 } [ / ] with map ." "{ 1 1/2 1/3 }" }
780     { $example "USING: kernel math prettyprint sequences ;" "1000 100 5 iota [ sq + + ] 2with map ." "{ 1100 1101 1104 1109 1116 }" }
781 } ;
782
783 HELP: 2with
784 { $values
785   { "param1" object }
786   { "param1" object }
787   { "obj" object }
788   { "quot" { $quotation ( param1 param2 elt -- ... ) } }
789   { "curry" curry }
790 }
791 { $description "Partial application on the left of two parameters." } ;
792
793 HELP: compose
794 { $values { "quot1" callable } { "quot2" callable } { "compose" compose } }
795 { $description "Quotation composition. Outputs a " { $link callable } " which calls " { $snippet "quot1" } " followed by " { $snippet "quot2" } "." }
796 { $notes
797     "The following two lines are equivalent:"
798     { $code
799         "compose call"
800         "append call"
801     }
802     "However, " { $link compose } " runs in constant time, and the optimizing compiler is able to compile code which calls composed quotations."
803 } ;
804
805
806 HELP: prepose
807 { $values { "quot1" callable } { "quot2" callable } { "compose" compose } }
808 { $description "Quotation composition. Outputs a " { $link callable } " which calls " { $snippet "quot2" } " followed by " { $snippet "quot1" } "." }
809 { $notes "See " { $link compose } " for details." } ;
810
811 { compose prepose } related-words
812
813 HELP: dip
814 { $values { "x" object } { "quot" quotation } }
815 { $description "Removes " { $snippet "x" } " from the datastack, calls " { $snippet "quot" } ", and restores " { $snippet "x" } " to the top of the datastack when " { $snippet "quot" } " is finished." }
816 { $examples
817     { $example "USING: arrays kernel math prettyprint ;" "10 20 30 [ / ] dip 2array ." "{ 1/2 30 }" }
818 }
819 { $notes { $snippet "dip" } " is executed as a primitive when preceded by a literal quotation. The below definition is not executed unless its argument is a non-literal quotation, such as a quotation constructed with " { $link curry } " or " { $link compose } ", or for " { $link "fry" } " or quotations including " { $link "locals" } "." } ;
820
821 HELP: 2dip
822 { $values { "x" object } { "y" object } { "quot" quotation } }
823 { $description "Removes " { $snippet "x" } " and " { $snippet "y" } " from the datastack, calls " { $snippet "quot" } ", and restores the removed objects to the top of the datastack when " { $snippet "quot" } " is finished." }
824 { $notes "The following are equivalent:"
825     { $code "[ [ foo bar ] dip ] dip" }
826     { $code "[ foo bar ] 2dip" }
827 } ;
828
829 HELP: 3dip
830 { $values { "x" object } { "y" object } { "z" object } { "quot" quotation } }
831 { $description "Removes " { $snippet "x" } ", " { $snippet "y" } ", and " { $snippet "z" } " from the datastack, calls " { $snippet "quot" } ", and restores the removed objects to the top of the datastack when " { $snippet "quot" } " is finished." }
832 { $notes "The following are equivalent:"
833     { $code "[ [ [ foo bar ] dip ] dip ] dip" }
834     { $code "[ foo bar ] 3dip" }
835 } ;
836
837 HELP: 4dip
838 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "quot" quotation } }
839 { $description "Removes " { $snippet "w" } ", " { $snippet "x" } ", " { $snippet "y" } ", and " { $snippet "z" } " from the datastack, calls " { $snippet "quot" } ", and restores the removed objects to the top of the datastack when " { $snippet "quot" } " is finished." }
840 { $notes "The following are equivalent:"
841     { $code "[ [ [ [ foo bar ] dip ] dip ] dip ] dip" }
842     { $code "[ foo bar ] 4dip" }
843 } ;
844
845 HELP: while
846 { $values { "pred" { $quotation ( ..a -- ..b ? ) } } { "body" { $quotation ( ..b -- ..a ) } } }
847 { $description "Calls " { $snippet "body" } " until " { $snippet "pred" } " returns " { $link f } "." } ;
848
849 HELP: until
850 { $values { "pred" { $quotation ( ..a -- ..b ? ) } } { "body" { $quotation ( ..b -- ..a ) } } }
851 { $description "Calls " { $snippet "body" } " until " { $snippet "pred" } " returns " { $link t } "." } ;
852
853 HELP: do
854 { $values { "pred" { $quotation ( ..a -- ..b ? ) } } { "body" { $quotation ( ..b -- ..a ) } } }
855 { $description "Executes one iteration of a " { $link while } " or " { $link until } " loop." } ;
856
857 HELP: loop
858 { $values
859      { "pred" quotation } }
860      { $description "Calls the quotation repeatedly until it outputs " { $link f } "." }
861 { $examples "Loop until we hit a zero:"
862     { $unchecked-example "USING: kernel random math io ; "
863     " [ \"hi\" write bl 10 random zero? not ] loop"
864     "hi hi hi" }
865     "A fun loop:"
866     { $example "USING: kernel prettyprint math ; "
867     "3 [ dup . 7 + 11 mod dup 3 = not ] loop drop"
868     "3\n10\n6\n2\n9\n5\n1\n8\n4\n0\n7" }
869 } ;
870
871 ARTICLE: "looping-combinators" "Looping combinators"
872 "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."
873 { $subsections
874     while
875     until
876 }
877 "To execute one iteration of a loop, use the following word:"
878 { $subsections do }
879 "This word is intended as a modifier. The normal " { $link while } " loop never executes the body if the predicate returns false on the first iteration. To ensure the body executes at least once, use " { $link do } ":"
880 { $code
881     "[ P ] [ Q ] do while"
882 }
883 "A simpler looping combinator which executes a single quotation until it returns " { $link f } ":"
884 { $subsections loop } ;
885
886 HELP: assert
887 { $values { "got" "the obtained value" } { "expect" "the expected value" } }
888 { $description "Throws an " { $link assert } " error." }
889 { $error-description "Thrown when a unit test or other assertion fails." } ;
890
891 HELP: assert=
892 { $values { "a" object } { "b" object } }
893 { $description "Throws an " { $link assert } " error if " { $snippet "a" } " does not equal " { $snippet "b" } "." } ;
894
895 HELP: become
896 { $values { "old" array } { "new" array } }
897 { $description "Replaces all references to objects in " { $snippet "old" } " with the corresponding object in " { $snippet "new" } ". This word is used to implement tuple reshaping. See " { $link "tuple-redefinition" } "." } ;
898
899 ARTICLE: "shuffle-words-complex" "Complex shuffle words"
900 "These shuffle words tend to make code difficult to read and to reason about. Code that uses them should almost always be rewritten using " { $link "locals" } " or " { $link "dataflow-combinators" } "."
901 $nl
902 "Duplicating stack elements deep in the stack:"
903 { $subsections
904     dupd
905 }
906 "Permuting stack elements deep in the stack:"
907 { $subsections
908     swapd
909     rot
910     -rot
911 } ;
912
913 ARTICLE: "shuffle-words" "Shuffle words"
914 "Shuffle words rearrange items at the top of the data stack as indicated by their stack effects. They provide simple data flow control between words. More complex data flow control is available with the " { $link "dataflow-combinators" } " and with " { $link "locals" } "."
915 $nl
916 "Removing stack elements:"
917 { $subsections
918     drop
919     2drop
920     3drop
921     nip
922     2nip
923 }
924 "Duplicating stack elements:"
925 { $subsections
926     dup
927     2dup
928     3dup
929     over
930     2over
931     pick
932 }
933 "Permuting stack elements:"
934 { $subsections
935     swap
936 }
937 "There are additional, more complex stack shuffling words whose use is not recommended."
938 { $subsections
939     "shuffle-words-complex"
940 } ;
941
942 ARTICLE: "equality" "Equality"
943 "There are two distinct notions of “sameness” when it comes to objects."
944 $nl
945 "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:"
946 { $subsections eq? }
947 "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" } "):"
948 { $subsections = }
949 "A third form of equality is provided by " { $link number= } ". It compares numeric value while disregarding types."
950 $nl
951 "Custom value comparison methods for use with " { $link = } " can be defined on a generic word:"
952 { $subsections equal? }
953 "Utility class:"
954 { $subsections identity-tuple }
955 "An object can be cloned; the clone has distinct identity but equal value:"
956 { $subsections clone } ;
957
958 ARTICLE: "assertions" "Assertions"
959 "Some words to make assertions easier to enforce:"
960 { $subsections
961     assert
962     assert=
963 } ;