]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-docs.factor
sequences: fix docs
[factor.git] / core / sequences / sequences-docs.factor
1 USING: arrays generic.single help.markup help.syntax kernel
2 layouts math math.order quotations sequences.private vectors ;
3 IN: sequences
4
5 HELP: sequence
6 { $class-description "A mixin class whose instances are sequences. Custom implementations of the sequence protocol should be declared as instances of this mixin for all sequence functionality to work correctly:"
7     { $code "INSTANCE: my-sequence sequence" }
8 } ;
9
10 HELP: length
11 { $values { "seq" sequence } { "n" "a non-negative integer" } }
12 { $contract "Outputs the length of the sequence. All sequences support this operation." } ;
13
14 HELP: set-length
15 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
16 { $contract "Resizes a sequence. The initial contents of the new area is undefined." }
17 { $errors "Throws a " { $link no-method } " error if the sequence is not resizable, and a " { $link bounds-error } " if the new length is negative." }
18 { $side-effects "seq" } ;
19
20 HELP: lengthen
21 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
22 { $contract "Ensures the sequence has a length of at least " { $snippet "n" } " elements. This word differs from " { $link set-length } " in two respects:"
23     { $list
24         { "This word does not shrink the sequence if " { $snippet "n" } " is less than its length." }
25         { "The word doubles the underlying storage of " { $snippet "seq" } ", whereas " { $link set-length } " is permitted to set it to equal " { $snippet "n" } ". This ensures that repeated calls to this word with constant increments of " { $snippet "n" } " do not result in a quadratic amount of copying, so that for example " { $link push-all } " can run efficiently when used in a loop." }
26     }
27 } ;
28
29 HELP: nth
30 { $values { "n" "a non-negative integer" } { "seq" sequence } { "elt" "the element at the " { $snippet "n" } "th index" } }
31 { $contract "Outputs the " { $snippet "n" } "th element of the sequence. Elements are numbered from zero, so the last element has an index one less than the length of the sequence. All sequences support this operation." }
32 { $errors "Throws a " { $link bounds-error } " if the index is negative, or greater than or equal to the length of the sequence." } ;
33
34 HELP: set-nth
35 { $values { "elt" object } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
36 { $contract "Sets the " { $snippet "n" } "th element of the sequence. Storing beyond the end of a resizable sequence such as a vector or string buffer grows the sequence." }
37 { $errors "Throws an error if the index is negative, or if the sequence is not resizable and the index is greater than or equal to the length of the sequence."
38 $nl
39 "Throws an error if the sequence cannot hold elements of the given type." }
40 { $side-effects "seq" } ;
41
42 HELP: nths
43 { $values
44     { "indices" sequence } { "seq" sequence }
45     { "seq'" sequence } }
46 { $description "Outputs a sequence of elements from the input sequence indexed by the indices." }
47 { $examples
48     { $example "USING: prettyprint sequences ;"
49                "{ 0 2 } { \"a\" \"b\" \"c\" } nths ."
50                "{ \"a\" \"c\" }"
51     }
52 } ;
53
54 HELP: immutable
55 { $values { "seq" sequence } }
56 { $description "Throws an " { $link immutable } " error." }
57 { $error-description "Thrown if an attempt is made to modify an immutable sequence." } ;
58
59 HELP: new-sequence
60 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a mutable sequence" } }
61 { $contract "Outputs a mutable sequence of length " { $snippet "len" } " which can hold the elements of " { $snippet "seq" } ". The initial contents of the sequence are undefined." } ;
62
63 HELP: new-resizable
64 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a resizable mutable sequence" } }
65 { $contract "Outputs a resizable mutable sequence with an initial capacity of " { $snippet "len" } " elements and zero length, which can hold the elements of " { $snippet "seq" } "." }
66 { $examples
67     { $example "USING: prettyprint sequences ;" "300 V{ } new-resizable ." "V{ }" }
68     { $example "USING: prettyprint sequences ;" "300 SBUF\" \" new-resizable ." "SBUF\" \"" }
69 } ;
70
71 HELP: like
72 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
73 { $contract "Outputs a sequence with the same elements as " { $snippet "seq" } ", but " { $emphasis "like" } " the template sequence, in the sense that it either has the same class as the template sequence, or if the template sequence is a virtual sequence, the same class as the template sequence's underlying sequence."
74 $nl
75 "The default implementation does nothing." }
76 { $notes "Unlike " { $link clone-like } ", the output sequence might share storage with the input sequence." } ;
77
78 HELP: empty?
79 { $values { "seq" sequence } { "?" boolean } }
80 { $description "Tests if the sequence has zero length." } ;
81
82 HELP: if-empty
83 { $values { "seq" sequence } { "quot1" quotation } { "quot2" quotation } }
84 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped and " { $snippet "quot1" } " is called. Otherwise, if the sequence has any elements, " { $snippet "quot2" } " is called on it." }
85 { $examples
86     { $example
87         "USING: kernel prettyprint sequences ;"
88         "{ 1 2 3 } [ \"empty sequence\" ] [ sum ] if-empty ."
89         "6"
90     }
91 } ;
92
93 HELP: when-empty
94 { $values
95     { "seq" sequence } { "quot" "the first quotation of an " { $link if-empty } }
96     { "seq/obj" object }
97 }
98 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped and the " { $snippet "quot" } " is called." }
99 { $examples "This word is equivalent to " { $link if-empty } " with an empty second quotation:"
100     { $example
101     "USING: sequences prettyprint ;"
102     "{ } [ { 4 5 6 } ] [ ] if-empty ."
103     "{ 4 5 6 }"
104     }
105     { $example
106     "USING: sequences prettyprint ;"
107     "{ } [ { 4 5 6 } ] when-empty ."
108     "{ 4 5 6 }"
109     }
110 } ;
111
112 HELP: unless-empty
113 { $values
114     { "seq" sequence } { "quot" "the second quotation of an " { $link if-empty } } }
115 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped. Otherwise, the " { $snippet "quot" } " is called on the sequence." }
116 { $examples "This word is equivalent to " { $link if-empty } " with an empty first quotation:"
117     { $example
118     "USING: sequences prettyprint ;"
119     "{ 4 5 6 } [ ] [ sum . ] if-empty"
120     "15"
121     }
122     { $example
123     "USING: sequences prettyprint ;"
124     "{ 4 5 6 } [ sum . ] unless-empty"
125     "15"
126     }
127 } ;
128
129 HELP: delete-all
130 { $values { "seq" "a resizable sequence" } }
131 { $description "Resizes the sequence to zero length, removing all elements. Not all sequences are resizable." }
132 { $errors "Throws an error if the sequence is not resizable." }
133 { $side-effects "seq" } ;
134
135 HELP: resize
136 { $values { "n" "a non-negative integer" } { "seq" sequence } { "newseq" "a new sequence" } }
137 { $description "Creates a new sequence of the same type as " { $snippet "seq" } " with " { $snippet "n" } " elements, and copies the contents of " { $snippet "seq" } " into the new sequence. If " { $snippet "n" } " exceeds the length of " { $snippet "seq" } ", the remaining elements are filled with a default value; " { $link f } " for arrays and 0 for strings." }
138 { $notes "This generic word is only implemented for strings and arrays." } ;
139
140 HELP: first
141 { $values { "seq" sequence } { "first" "the first element of the sequence" } }
142 { $description "Outputs the first element of the sequence." }
143 { $errors "Throws an error if the sequence is empty." } ;
144
145 HELP: second
146 { $values { "seq" sequence } { "second" "the second element of the sequence" } }
147 { $description "Outputs the second element of the sequence." }
148 { $errors "Throws an error if the sequence contains less than two elements." } ;
149
150 HELP: third
151 { $values { "seq" sequence } { "third" "the third element of the sequence" } }
152 { $description "Outputs the third element of the sequence." }
153 { $errors "Throws an error if the sequence contains less than three elements." } ;
154
155 HELP: fourth
156 { $values { "seq" sequence } { "fourth" "the fourth element of the sequence" } }
157 { $description "Outputs the fourth element of the sequence." }
158 { $errors "Throws an error if the sequence contains less than four elements." } ;
159
160 HELP: push
161 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
162 { $description "Adds an element at the end of the sequence. The sequence length is adjusted accordingly." }
163 { $errors "Throws an error if " { $snippet "seq" } " is not resizable, or if the type of " { $snippet "elt" } " is not permitted in " { $snippet "seq" } "." }
164 { $side-effects "seq" } ;
165
166 HELP: bounds-check?
167 { $values { "n" integer } { "seq" sequence } { "?" boolean } }
168 { $description "Tests if the index is within the bounds of the sequence." } ;
169
170 HELP: bounds-error
171 { $values { "n" integer } { "seq" sequence } }
172 { $description "Throws a " { $link bounds-error } "." }
173 { $error-description "Thrown by " { $link nth } ", " { $link set-nth } " and " { $link set-length } " if the given index lies beyond the bounds of the sequence." } ;
174
175 HELP: bounds-check
176 { $values { "n" integer } { "seq" sequence } }
177 { $description "Throws an error if " { $snippet "n" } " is negative or if it is greater than or equal to the length of " { $snippet "seq" } ". Otherwise the two inputs remain on the stack." } ;
178
179 HELP: ?nth
180 { $values { "n" integer } { "seq" sequence } { "elt/f" { $maybe object } } }
181 { $description "A forgiving version of " { $link nth } ". If the index is out of bounds, or if the sequence is " { $link f } ", simply outputs " { $link f } "." } ;
182
183 HELP: ??nth
184 { $values { "n" integer } { "seq" sequence } { "elt/f" { $maybe object } } { "?" boolean } }
185 { $description "A forgiving version of " { $link nth } ". If the index is out of bounds, or if the sequence is " { $link f } ", simply outputs " { $link f } ". Also outputs a boolean to distinguish between the sequence containing an " { $link f } " or an out of bounds index." } ;
186
187 { nth ?nth ??nth } related-words
188
189 HELP: ?set-nth
190 { $values { "elt" object } { "n" integer } { "seq" sequence } }
191 { $description "A forgiving version of " { $link set-nth } ". If the index is out of bounds, does nothing." } ;
192
193 HELP: ?first
194 { $values { "seq" sequence } { "elt/f" { $maybe object } } }
195 { $description "A forgiving version of " { $link first } ". If the sequence is empty, or if the sequence is " { $link f } ", simply outputs " { $link f } "." }
196 { $examples
197     "On an empty sequence:"
198     { $example "USING: sequences prettyprint ;"
199                "{ } ?first ."
200                "f"
201     }
202     "Works like first on sequences with elements:"
203     { $example "USING: sequences prettyprint ;"
204                "{ 1 2 3 } ?first ."
205                "1"
206     }
207 } ;
208
209
210 HELP: ?second
211 { $values { "seq" sequence } { "elt/f" { $maybe object } } }
212 { $description "A forgiving version of " { $link second } ". If the sequence has less than two elements, or if the sequence is " { $link f } ", simply outputs " { $link f } "." } ;
213
214 HELP: ?last
215 { $values { "seq" sequence } { "elt/f" { $maybe object } } }
216 { $description "A forgiving version of " { $link last } ". If the sequence is empty, or if the sequence is " { $link f } ", simply outputs " { $link f } "." } ;
217
218 HELP: nth-unsafe
219 { $values { "n" integer } { "seq" sequence } { "elt" object } }
220 { $contract "Unsafe variant of " { $link nth } " that does not perform bounds checks." } ;
221
222 HELP: set-nth-unsafe
223 { $values { "elt" object } { "n" integer } { "seq" sequence } }
224 { $contract "Unsafe variant of " { $link set-nth } " that does not perform bounds checks." } ;
225
226 HELP: exchange-unsafe
227 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
228 { $description "Unsafe variant of " { $link exchange } " that does not perform bounds checks." } ;
229
230 HELP: first-unsafe
231 { $values { "seq" sequence } { "first" "the first element" } }
232 { $contract "Unsafe variant of " { $link first } " that does not perform bounds checks." } ;
233
234 HELP: first2-unsafe
235 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
236 { $contract "Unsafe variant of " { $link first2 } " that does not perform bounds checks." } ;
237
238 HELP: first3-unsafe
239 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
240 { $contract "Unsafe variant of " { $link first3 } " that does not perform bounds checks." } ;
241
242 HELP: first4-unsafe
243 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
244 { $contract "Unsafe variant of " { $link first4 } " that does not perform bounds checks." } ;
245
246 HELP: 1sequence
247 { $values { "obj" object } { "exemplar" sequence } { "seq" sequence } }
248 { $description "Creates a one-element sequence of the same type as " { $snippet "exemplar" } "." } ;
249
250 HELP: 2sequence
251 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "seq" sequence } }
252 { $description "Creates a two-element sequence of the same type as " { $snippet "exemplar" } "." } ;
253
254 HELP: 3sequence
255 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "exemplar" sequence } { "seq" sequence } }
256 { $description "Creates a three-element sequence of the same type as " { $snippet "exemplar" } "." } ;
257
258 HELP: 4sequence
259 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "obj4" object } { "exemplar" sequence } { "seq" sequence } }
260 { $description "Creates a four-element sequence of the same type as " { $snippet "exemplar" } "." } ;
261
262 HELP: first2
263 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
264 { $description "Pushes the first two elements of a sequence." }
265 { $errors "Throws an error if the sequence has less than two elements." } ;
266
267 HELP: first3
268 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
269 { $description "Pushes the first three elements of a sequence." }
270 { $errors "Throws an error if the sequence has less than three elements." } ;
271
272 HELP: first4
273 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
274 { $description "Pushes the first four elements of a sequence." }
275 { $errors "Throws an error if the sequence has less than four elements." } ;
276
277 HELP: array-capacity
278 { $class-description "A predicate class whose instances are fixnums of valid array sizes for the current architecture. The minimum value is zero and the maximum value is " { $link max-array-capacity } "." }
279 { $description "Low-level array length accessor." }
280 { $warning "This word is in the " { $vocab-link "sequences.private" } " vocabulary because it is unsafe. It does not check types, so improper use can corrupt memory." }
281 { $see-also integer-array-capacity } ;
282
283 HELP: integer-array-capacity
284 { $class-description "A predicate class whose instances are integer of valid array sizes for the current architecture. The minimum value is zero and the maximum value is " { $link max-array-capacity } "." }
285 { $description "Low-level array length accessor." }
286 { $warning "This word is in the " { $vocab-link "sequences.private" } " vocabulary because it is unsafe. It does not check types, so improper use can corrupt memory." }
287 { $see-also array-capacity } ;
288
289 HELP: array-nth
290 { $values { "n" "a non-negative fixnum" } { "array" array } { "elt" object } }
291 { $description "Low-level array element accessor." }
292 { $warning "This word is in the " { $vocab-link "sequences.private" } " vocabulary because it is unsafe. It does not check types or array bounds, and improper use can corrupt memory. User code must use " { $link nth } " instead." } ;
293
294 HELP: set-array-nth
295 { $values { "elt" object } { "n" "a non-negative fixnum" } { "array" array } }
296 { $description "Low-level array element mutator." }
297 { $warning "This word is in the " { $vocab-link "sequences.private" } " vocabulary because it is unsafe. It does not check types or array bounds, and improper use can corrupt memory. User code must use " { $link set-nth } " instead." } ;
298
299 HELP: collect
300 { $values { "n" "a non-negative integer" } { "quot" { $quotation ( ... n -- ... value ) } } { "into" "a sequence of length at least " { $snippet "n" } } }
301 { $description "A primitive mapping operation that applies a quotation to all integers from 0 up to but not including " { $snippet "n" } ", and collects the results in a new array. User code should use " { $link map } " instead." }
302 { $examples
303   { $example
304     "USING: kernel math.parser prettyprint sequences sequences.private ;"
305     "10 [ number>string ] 10 f new-sequence [ collect ] keep ."
306     "{ \"0\" \"1\" \"2\" \"3\" \"4\" \"5\" \"6\" \"7\" \"8\" \"9\" }"
307   }
308 } ;
309
310 HELP: each
311 { $values { "seq" sequence } { "quot" { $quotation ( ... x -- ... ) } } }
312 { $description "Applies the quotation to each element of the sequence in order." } ;
313
314 HELP: reduce
315 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "result" "the final result" } }
316 { $description "Combines successive elements of the sequence using a binary operation, and outputs the final result. On the first iteration, the two inputs to the quotation are " { $snippet "identity" } ", and the first element of the sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the corresponding element of the sequence." }
317 { $examples
318     { $example "USING: math prettyprint sequences ;" "{ 1 5 3 } 0 [ + ] reduce ." "9" }
319 } ;
320
321 HELP: reduce-index
322 { $values
323     { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt index -- ... next ) } } { "result" object } }
324 { $description "Combines successive elements of the sequence and their indices binary operations, and outputs the final result. On the first iteration, the three inputs to the quotation are " { $snippet "identity" } ", the first element of the sequence, and its index, 0. On successive iterations, the first input is the result of the previous iteration, the second input is the corresponding element of the sequence, and the third is its index." }
325 { $examples { $example "USING: sequences prettyprint math ;"
326     "{ 10 50 90 } 0 [ + + ] reduce-index ."
327     "153"
328 } } ;
329
330 HELP: accumulate-as
331 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "exemplar" sequence } { "final" "the final result" } { "newseq" "a new sequence" } }
332 { $description "Combines successive elements of the sequence using a binary operation, and outputs a sequence of the same type as " { $snippet "exemplar" } " containing intermediate results, together with the final result."
333 $nl
334 "The first element of the output sequence is " { $snippet "identity" } ". Then, on the first iteration, the two inputs to the quotation are " { $snippet "identity" } " and the first element of the input sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the next element of the input sequence."
335 $nl
336 "When given the empty sequence, outputs a new empty sequence together with the " { $snippet "identity" } "." }
337 { $notes "May be named " { $snippet "scan" } " or " { $snippet "prefix sum" } " in other languages." } ;
338
339 HELP: accumulate
340 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "final" "the final result" } { "newseq" "a new sequence" } }
341 { $description "Combines successive elements of the sequence using a binary operation, and outputs a sequence of intermediate results, together with the final result."
342 $nl
343 "The first element of the output sequence is " { $snippet "identity" } ". Then, on the first iteration, the two inputs to the quotation are " { $snippet "identity" } " and the first element of the input sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the next element of the input sequence."
344 $nl
345 "When given the empty sequence, outputs a new empty sequence together with the " { $snippet "identity" } "." }
346 { $examples
347     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate . ." "{ 0 2 4 6 8 }\n10" }
348 }
349 { $notes "May be named " { $snippet "scan" } " or " { $snippet "prefix sum" } " in other languages." } ;
350
351 HELP: accumulate!
352 { $values { "seq" "a mutable sequence" } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "final" "the final result" } }
353 { $description "Combines successive elements of the sequence using a binary operation, and outputs the original sequence of intermediate results, together with the final result."
354 $nl
355 "The first element of the new sequence is " { $snippet "identity" } ". Then, on the first iteration, the two inputs to the quotation are " { $snippet "identity" } ", and the first element of the old sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the corresponding element of the old sequence."
356 $nl
357 "When given the empty sequence, outputs the same empty sequence together with the " { $snippet "identity" } "." }
358 { $errors "Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
359 { $side-effects "seq" }
360 { $examples
361     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate! . ." "{ 0 2 4 6 8 }\n10" }
362 }
363 { $notes "May be named " { $snippet "scan" } " or " { $snippet "prefix sum" } " in other languages." } ;
364
365 HELP: accumulate*-as
366 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "exemplar" sequence } { "newseq" "a new sequence" } }
367 { $description "Combines successive elements of the sequence using a binary operation, and outputs a sequence of the same type as " { $snippet "exemplar" } " containing all results."
368 $nl
369 "On the first iteration, the two inputs to the quotation are " { $snippet "identity" } " and the first element of the input sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the next element of the input sequence."
370 $nl
371 "When given the empty sequence, outputs a new empty sequence" }
372 { $notes "May be named " { $snippet "scan" } " or " { $snippet "prefix sum" } " in other languages." } ;
373
374 HELP: accumulate*
375 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "newseq" sequence } }
376 { $description "Combines successive elements of the sequence using a binary operation, and outputs a sequence of all results."
377 $nl
378 "On the first iteration, the two inputs to the quotation are " { $snippet "identity" } " and the first element of the input sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the next element of the input sequence."
379 $nl
380 "When given the empty sequence, outputs a new empty sequence." }
381 { $examples
382     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate* ." "{ 2 4 6 8 10 }" }
383 }
384 { $notes "May be named " { $snippet "scan" } " or " { $snippet "prefix sum" } " in other languages." } ;
385
386 HELP: accumulate*!
387 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } }
388 { $description "Combines successive elements of the sequence using a binary operation, and outputs the original sequence of all results."
389 $nl
390 "On the first iteration, the two inputs to the quotation are " { $snippet "identity" } " and the first element of the input sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the next element of the input sequence."
391 $nl
392 "When given the empty sequence, outputs the same empty sequence." }
393 { $errors "Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
394 { $side-effects "seq" }
395 { $examples
396     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate*! ." "{ 2 4 6 8 10 }" }
397 }
398 { $notes "May be named " { $snippet "scan" } " or " { $snippet "prefix sum" } " in other languages." } ;
399
400 { accumulate accumulate! accumulate-as accumulate* accumulate*! accumulate*-as } related-words
401
402 HELP: map
403 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... newelt ) } } { "newseq" "a new sequence" } }
404 { $description "Applies the quotation to each element of the sequence in order. The new elements are collected into a sequence of the same class as the input sequence." } ;
405
406 HELP: map-as
407 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... newelt ) } } { "exemplar" sequence } { "newseq" "a new sequence" } }
408 { $description "Applies the quotation to each element of the sequence in order. The new elements are collected into a sequence of the same class as " { $snippet "exemplar" } "." }
409 { $examples
410     "The following example converts a string into an array of one-element strings:"
411     { $example "USING: prettyprint strings sequences ;" "\"Hello\" [ 1string ] { } map-as ." "{ \"H\" \"e\" \"l\" \"l\" \"o\" }" }
412     "Note that " { $link map } " could not be used here, because it would create another string to hold results, and one-element strings cannot themselves be elements of strings."
413 } ;
414
415 HELP: each-index
416 { $values
417     { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... ) } } }
418 { $description "Calls the quotation with the element of the sequence and its index on the stack, with the index on the top of the stack." }
419 { $examples { $example "USING: arrays sequences prettyprint ;"
420 "{ 10 20 30 } [ 2array . ] each-index"
421 "{ 10 0 }\n{ 20 1 }\n{ 30 2 }"
422 } } ;
423
424 HELP: map-index
425 { $values
426   { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... newelt ) } } { "newseq" sequence } }
427 { $description "Calls the quotation with the element of the sequence and its index on the stack, with the index on the top of the stack. Collects the outputs of the quotation and outputs them in a sequence of the same type as the input sequence." }
428 { $examples
429     { $example "USING: arrays sequences prettyprint ;"
430         "{ 10 20 30 } [ 2array ] map-index ."
431         "{ { 10 0 } { 20 1 } { 30 2 } }"
432     }
433 } ;
434
435 HELP: map-index-as
436 { $values
437   { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... newelt ) } } { "exemplar" sequence } { "newseq" sequence } }
438 { $description "Calls the quotation with the element of the sequence and its index on the stack, with the index on the top of the stack. Collects the outputs of the quotation and outputs them in a sequence of the same type as the " { $snippet "exemplar" } " sequence." }
439 { $examples
440     { $example "USING: arrays sequences prettyprint ;"
441         "{ 10 20 30 } [ 2array ] V{ } map-index-as ."
442         "V{ { 10 0 } { 20 1 } { 30 2 } }"
443     }
444 } ;
445
446 { map map! map-as map-index map-index-as } related-words
447
448 HELP: change-nth
449 { $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" { $quotation ( ..a elt -- ..b newelt ) } } }
450 { $description "Applies the quotation to the " { $snippet "i" } "th element of the sequence, storing the result back into the sequence." }
451 { $errors "Throws an error if the sequence is immutable, if the index is out of bounds, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
452 { $side-effects "seq" } ;
453
454 HELP: map!
455 { $values { "seq" "a mutable sequence" } { "quot" { $quotation ( ... elt -- ... newelt ) } } }
456 { $description "Applies the quotation to each element yielding a new element, storing the new elements back in the original sequence. Returns the original sequence." }
457 { $errors "Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
458 { $side-effects "seq" } ;
459
460 HELP: min-length
461 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
462 { $description "Outputs the minimum of the lengths of the two sequences." } ;
463
464 HELP: max-length
465 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
466 { $description "Outputs the maximum of the lengths of the two sequences." } ;
467
468 HELP: 2each
469 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... ) } } }
470 { $description "Applies the quotation to pairs of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } "." } ;
471
472 HELP: 3each
473 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation ( ... elt1 elt2 elt3 -- ... ) } } }
474 { $description "Applies the quotation to triples of elements from " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } "." } ;
475
476 HELP: 2reduce
477 { $values { "seq1" sequence }
478           { "seq2" sequence }
479           { "identity" object }
480           { "quot" { $quotation ( ... prev elt1 elt2 -- ... next ) } }
481           { "result" "the final result" } }
482 { $description "Combines successive pairs of elements from the two sequences using a ternary operation. The first input value at each iteration except the first one is the result of the previous iteration. The first input value at the first iteration is " { $snippet "identity" } "." } ;
483
484 HELP: 2map
485 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... newelt ) } } { "newseq" "a new sequence" } }
486 { $description "Applies the quotation to each pair of elements in turn, yielding new elements which are collected into a new sequence having the same class as " { $snippet "seq1" } "." } ;
487
488 HELP: 3map
489 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation ( ... elt1 elt2 elt3 -- ... newelt ) } } { "newseq" "a new sequence" } }
490 { $description "Applies the quotation to each triple of elements in turn, yielding new elements which are collected into a new sequence having the same class as " { $snippet "seq1" } "." } ;
491
492 HELP: 2map-as
493 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... newelt ) } } { "exemplar" sequence } { "newseq" "a new sequence" } }
494 { $description "Applies the quotation to each pair of elements in turn, yielding new elements which are collected into a new sequence having the same class as " { $snippet "exemplar" } "." } ;
495
496 HELP: 3map-as
497 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation ( ... elt1 elt2 elt3 -- ... newelt ) } } { "exemplar" sequence } { "newseq" "a new sequence" } }
498 { $description "Applies the quotation to each triple of elements in turn, yielding new elements which are collected into a new sequence having the same class as " { $snippet "exemplar" } "." } ;
499
500 HELP: 2all?
501 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... ? ) } } { "?" boolean } }
502 { $description "Tests if all pairwise elements of " { $snippet "seq1" } " and " { $snippet "seq2" } " fulfill the predicate. If the sequences have different lengths, then only the smallest sequences items are compared with the other." }
503 { $examples
504   { $example
505     "USING: math prettyprint sequences ;"
506     "{ 1 2 3 4 } { 2 4 6 8 } [ <= ] 2all? ."
507     "t"
508   }
509 } ;
510
511 HELP: 2any?
512 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... ? ) } } { "?" boolean } }
513 { $description "Tests if any pairwise elements of " { $snippet "seq1" } " and " { $snippet "seq2" } " fulfill the predicate. If the sequences have different lengths, then only the smallest sequences items are compared with the other." }
514 { $examples
515   { $example
516     "USING: math prettyprint sequences ;"
517     "{ 2 4 5 8 } { 2 4 6 8 } [ < ] 2any? ."
518     "t"
519   }
520 } ;
521
522 HELP: find
523 { $values { "seq" sequence }
524           { "quot" { $quotation ( ... elt -- ... ? ) } }
525           { "i" "the index of the first match, or " { $link f } }
526           { "elt" "the first matching element, or " { $link f } } }
527 { $description "A simpler variant of " { $link find-from } " where the starting index is 0." } ;
528
529 HELP: find-from
530 { $values { "n" "a starting index" }
531           { "seq" sequence }
532           { "quot" { $quotation ( ... elt -- ... ? ) } }
533           { "i" { $maybe "the index of the first match" } }
534           { "elt" { $maybe "the first matching element" } } }
535 { $description "Applies the quotation to each element of the sequence in turn, until it outputs a true value or the end of the sequence is reached. If the quotation yields a true value for some sequence element, the word outputs the element index and the element itself. Otherwise, the word outputs an index of " { $link f } " and " { $link f } " as the element." } ;
536
537 HELP: find-last
538 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "i" { $maybe "the index of the first match" } } { "elt" { $maybe "the first matching element" } } }
539 { $description "A simpler variant of " { $link find-last-from } " where the starting index is one less than the length of the sequence." } ;
540
541 HELP: find-last-from
542 { $values { "n" "a starting index" } { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "i" { $maybe "the index of the first match" } } { "elt" { $maybe "the first matching element" } } }
543 { $description "Applies the quotation to each element of the sequence in reverse order, until it outputs a true value or the start of the sequence is reached. If the quotation yields a true value for some sequence element, the word outputs the element index and the element itself. Otherwise, the word outputs an index of " { $link f } " and " { $link f } " as the element." } ;
544
545 HELP: find-index
546 { $values { "seq" sequence }
547           { "quot" { $quotation ( ... elt i -- ... ? ) } }
548           { "i" { $maybe "the index of the first match" } }
549           { "elt" { $maybe "the first matching element" } } }
550 { $description "A variant of " { $link find } " where the quotation takes both an element and its index." } ;
551
552 HELP: find-index-from
553 { $values { "n" "a starting index" }
554           { "seq" sequence }
555           { "quot" { $quotation ( ... elt i -- ... ? ) } }
556           { "i" { $maybe "the index of the first match" } }
557           { "elt" { $maybe "the first matching element" } } }
558 { $description "A variant of " { $link find-from } " where the quotation takes both an element and its index."
559   "The search starts from list index " { $snippet "n" } ", skipping elements up until that index." } ;
560
561 HELP: map-find
562 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... result/f ) } } { "result" "the first non-false result of the quotation" } { "elt" { $maybe "the first matching element" } } }
563 { $description "Applies the quotation to each element of the sequence, until the quotation outputs a true value. If the quotation ever yields a result which is not " { $link f } ", then the value is output, along with the element of the sequence which yielded this." } ;
564
565 HELP: map-find-last
566 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... result/f ) } } { "result" "the last non-false result of the quotation" } { "elt" { $maybe "the last matching element" } } }
567 { $description "Applies the quotation to each element of the sequence from the tail, until the quotation outputs a true value. If the quotation ever yields a result which is not " { $link f } ", then the value is output, along with the element of the sequence which yielded this." } ;
568
569 { map-find map-find-last } related-words
570
571 HELP: any?
572 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "?" boolean } }
573 { $description "Tests if the sequence contains an element satisfying the predicate, by applying the predicate to each element in turn until a true value is found. If the sequence is empty or if the end of the sequence is reached, outputs " { $link f } "." } ;
574
575 HELP: none?
576 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "?" boolean } }
577 { $description "Tests if the sequence does not contain any element satisfying the predicate, by applying the predicate to each element in turn until a true value is found. If the sequence is empty or if the end of the sequence is reached, outputs " { $link t } "." } ;
578
579 HELP: all?
580 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "?" boolean } }
581 { $description "Tests if all elements in the sequence satisfy the predicate by checking each element in turn. Given an empty sequence, vacuously outputs " { $link t } "." } ;
582
583 HELP: push-when
584 { $values { "elt" object } { "quot" { $quotation ( ..a elt -- ..b ? ) } } { "accum" "a resizable mutable sequence" } }
585 { $description "Adds the element at the end of the sequence if the quotation yields a true value." }
586 { $notes "This word is a factor of " { $link filter } "." } ;
587
588 HELP: filter
589 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "subseq" "a new sequence" } }
590 { $description "Applies the quotation to each element in turn, and outputs a new sequence containing the elements of the original sequence for which the quotation output a true value." } ;
591
592 HELP: filter-as
593 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "exemplar" sequence } { "subseq" "a new sequence" } }
594 { $description "Applies the quotation to each element in turn, and outputs a new sequence of the same type as " { $snippet "exemplar" } " containing the elements of the original sequence for which the quotation output a true value." } ;
595
596 HELP: filter!
597 { $values { "seq" "a resizable mutable sequence" } { "quot" { $quotation ( ... elt -- ... ? ) } } }
598 { $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a false value." }
599 { $side-effects "seq" } ;
600
601 HELP: reject
602 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "subseq" "a new sequence" } }
603 { $description "Applies the quotation to each element in turn, and outputs a new sequence removing the elements of the original sequence for which the quotation outputs a true value." } ;
604
605 HELP: reject-as
606 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "exemplar" sequence } { "subseq" "a new sequence" } }
607 { $description "Applies the quotation to each element in turn, and outputs a new sequence of the same type as " { $snippet "exemplar" } " remove the elements of the original sequence for which the quotation output a true value." } ;
608
609 HELP: reject!
610 { $values { "seq" "a resizable mutable sequence" } { "quot" { $quotation ( ... elt -- ... ? ) } } }
611 { $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a true value." }
612 { $side-effects "seq" } ;
613
614 HELP: interleave
615 { $values { "seq" sequence } { "between" quotation } { "quot" { $quotation ( ... elt -- ... ) } } }
616 { $description "Applies " { $snippet "quot" } " to each element in turn, also invoking " { $snippet "between" } " in-between each pair of elements." }
617 { $examples { $example "USING: io sequences ;" "{ \"a\" \"b\" \"c\" } [ \"X\" write ] [ write ] interleave" "aXbXc" } } ;
618
619 HELP: index
620 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
621 { $description "Outputs the index of the first element in the sequence equal to " { $snippet "obj" } ". If no element is found, outputs " { $link f } "." } ;
622
623 HELP: index-from
624 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
625 { $description "Outputs the index of the first element in the sequence equal to " { $snippet "obj" } ", starting the search from the " { $snippet "i" } "th element. If no element is found, outputs " { $link f } "." } ;
626
627 HELP: last-index
628 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
629 { $description "Outputs the index of the last element in the sequence equal to " { $snippet "obj" } "; the sequence is traversed back to front. If no element is found, outputs " { $link f } "." } ;
630
631 HELP: last-index-from
632 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
633 { $description "Outputs the index of the last element in the sequence equal to " { $snippet "obj" } ", traversing the sequence backwards starting from the " { $snippet "i" } "th element and finishing at the first. If no element is found, outputs " { $link f } "." } ;
634
635 HELP: member?
636 { $values { "elt" object } { "seq" sequence } { "?" boolean } }
637 { $description "Tests if the sequence contains an element equal to the object." }
638 { $examples
639     "Is a letter in a string:"
640     { $example
641         "USING: sequences prettyprint ;"
642         "CHAR: a \"abc\" member? ."
643         "t"
644     } $nl
645     "Is a number in a sequence:"
646     { $example
647         "USING: sequences prettyprint ;"
648         "4 { 1 2 3 } member? ."
649         "f"
650     }
651 }
652 { $notes "This word uses equality comparison (" { $link = } ")." } ;
653
654 HELP: member-eq?
655 { $values { "elt" object } { "seq" sequence } { "?" boolean } }
656 { $description "Tests if the sequence contains the object." }
657 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
658
659 HELP: remove
660 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
661 { $description "Outputs a new sequence containing all elements of the input sequence except for the given element." }
662 { $notes "This word uses equality comparison (" { $link = } ")." } ;
663
664 HELP: remove-eq
665 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
666 { $description "Outputs a new sequence containing all elements of the input sequence except those equal to the given element." }
667 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
668
669 HELP: remove-nth
670 { $values
671     { "n" integer } { "seq" sequence }
672     { "seq'" sequence } }
673 { $description "Creates a new sequence without the element at index " { $snippet "n" } "." }
674 { $examples "Notice that the original sequence is left intact:" { $example "USING: sequences prettyprint kernel ;"
675     "{ 1 2 3 } 1 over remove-nth . ."
676     "{ 1 3 }\n{ 1 2 3 }"
677 } } ;
678
679 HELP: move
680 { $values { "to" "an index in " { $snippet "seq" } } { "from" "an index in " { $snippet "seq" } } { "seq" "a mutable sequence" } }
681 { $description "Sets the element with index " { $snippet "m" } " to the element with index " { $snippet "n" } "." }
682 { $side-effects "seq" } ;
683
684 HELP: remove!
685 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
686 { $description "Removes all elements equal to " { $snippet "elt" } " from " { $snippet "seq" } " and returns " { $snippet "seq" } "." }
687 { $notes "This word uses equality comparison (" { $link = } ")." }
688 { $side-effects "seq" } ;
689
690 HELP: remove-eq!
691 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
692 { $description "Outputs a new sequence containing all elements of the input sequence except the given element." }
693 { $notes "This word uses identity comparison (" { $link eq? } ")." }
694 { $side-effects "seq" } ;
695
696 HELP: remove-nth!
697 { $values { "n" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
698 { $description "Removes the " { $snippet "n" } "th element from the sequence, shifting all other elements down and reducing its length by one." }
699 { $side-effects "seq" } ;
700
701 HELP: delete-slice
702 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
703 { $description "Removes a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } "." }
704 { $side-effects "seq" } ;
705
706 HELP: replace-slice
707 { $values { "new" sequence } { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "seq'" sequence } }
708 { $description "Replaces a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } " with a new sequence." }
709 { $errors "Throws an error if " { $snippet "new" } " contains elements whose types are not permissible in " { $snippet "seq" } "." } ;
710
711 { push push-either push-when pop pop* prefix suffix suffix! } related-words
712
713 HELP: suffix
714 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
715 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the end of " { $snippet "seq" } "." }
716 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
717 { $examples
718     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 4 suffix ." "{ 1 2 3 4 }" }
719 } ;
720
721 HELP: suffix!
722 { $values { "seq" sequence } { "elt" object } }
723 { $description "Modifiers a sequence in-place by adding " { $snippet "elt" } " to the end of " { $snippet "seq" } ". Outputs " { $snippet "seq" } "." }
724 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq" } "." }
725 { $examples
726     { $example "USING: prettyprint sequences ;" "V{ 1 2 3 } 4 suffix! ." "V{ 1 2 3 4 }" }
727 } ;
728
729 HELP: append!
730 { $values { "seq1" sequence } { "seq2" sequence } }
731 { $description "Modifiers " { $snippet "seq1" } " in-place by adding the elements from " { $snippet "seq2" } " to the end and outputs " { $snippet "seq1" } "." }
732 { $examples
733     { $example "USING: prettyprint sequences ;" "V{ 1 2 3 } { 4 5 6 } append! ." "V{ 1 2 3 4 5 6 }" }
734 } ;
735
736 HELP: prefix
737 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
738 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the beginning of " { $snippet "seq" } "." }
739 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
740 { $examples
741 { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 0 prefix ." "{ 0 1 2 3 }" }
742 } ;
743
744 HELP: sum-lengths
745 { $values { "seq" { $sequence sequence } } { "n" integer } }
746 { $description "Outputs the sum of the lengths of all sequences in " { $snippet "seq" } "." } ;
747
748 HELP: concat
749 { $values { "seq" sequence } { "newseq" sequence } }
750 { $description "Concatenates a sequence of sequences together into one sequence. If " { $snippet "seq" } " is empty, outputs " { $snippet "{ }" } ", otherwise the resulting sequence is of the same class as the first element of " { $snippet "seq" } "." }
751 { $errors "Throws an error if one of the sequences in " { $snippet "seq" } " contains elements not permitted in sequences of the same class as the first element of " { $snippet "seq" } "." } ;
752
753 HELP: concat-as
754 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" sequence } }
755 { $description "Concatenates a sequence of sequences together into one sequence with the same type as " { $snippet "exemplar" } "." }
756 { $errors "Throws an error if one of the sequences in " { $snippet "seq" } " contains elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." } ;
757
758 HELP: join
759 { $values { "seq" sequence } { "glue" sequence } { "newseq" sequence } }
760 { $description "Concatenates a sequence of sequences together into one sequence, placing a copy of " { $snippet "glue" } " between each pair of sequences. The resulting sequence is of the same class as " { $snippet "glue" } "." }
761 { $examples
762     "Join a list of strings:"
763     { $example "USING: sequences prettyprint ;"
764         "{ \"cat\" \"dog\" \"ant\" } \" \" join ."
765         "\"cat dog ant\""
766     }
767 }
768 { $notes "If the " { $snippet "glue" } " sequence is empty, this word calls " { $link concat-as } "." }
769 { $errors "Throws an error if one of the sequences in " { $snippet "seq" } " contains elements not permitted in sequences of the same class as " { $snippet "glue" } "." } ;
770
771 HELP: join-as
772 { $values { "seq" sequence } { "glue" sequence } { "exemplar" sequence } { "newseq" sequence } }
773 { $description "Concatenates a sequence of sequences together into one sequence, placing a copy of " { $snippet "glue" } " between each pair of sequences. The resulting sequence is of the same class as " { $snippet "glue" } "." }
774 { $notes "If the " { $snippet "glue" } " sequence is empty, this word calls " { $link concat-as } "." }
775 { $examples
776     "Join a list of strings as a string buffer:"
777     { $example "USING: sequences prettyprint ;"
778         "{ \"a\" \"b\" \"c\" } \"1\" SBUF\" \" join-as ."
779         "SBUF\" a1b1c\""
780     }
781 }
782 { $errors "Throws an error if one of the sequences in " { $snippet "seq" } " contains elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." } ;
783
784 { join join-as concat concat-as } related-words
785
786 HELP: last
787 { $values { "seq" sequence } { "elt" object } }
788 { $description "Outputs the last element of a sequence." }
789 { $errors "Throws an error if the sequence is empty." } ;
790
791 HELP: pop*
792 { $values { "seq" "a resizable mutable sequence" } }
793 { $description "Removes the last element and shortens the sequence." }
794 { $side-effects "seq" }
795 { $errors "Throws an error if the sequence is empty." } ;
796
797 HELP: pop
798 { $values { "seq" "a resizable mutable sequence" } { "elt" object } }
799 { $description "Outputs the last element after removing it and shortening the sequence." }
800 { $side-effects "seq" }
801 { $errors "Throws an error if the sequence is empty." } ;
802
803 HELP: mismatch
804 { $values { "seq1" sequence } { "seq2" sequence } { "i" "an index" } }
805 { $description "Compares pairs of elements up to the minimum of the sequences' lengths, outputting the first index where the two sequences have non-equal elements, or " { $link f } " if all tested elements were equal." } ;
806
807 HELP: flip
808 { $values { "matrix" "a sequence of equal-length sequences" } { "newmatrix" "a sequence of equal-length sequences" } }
809 { $description "Transposes the matrix; that is, rows become columns and columns become rows." }
810 { $examples { $example "USING: prettyprint sequences ;" "{ { 1 2 3 } { 4 5 6 } } flip ." "{ { 1 4 } { 2 5 } { 3 6 } }" } } ;
811
812 HELP: exchange
813 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
814 { $description "Exchanges the " { $snippet "m" } "th and " { $snippet "n" } "th elements of " { $snippet "seq" } "." } ;
815
816 HELP: reverse!
817 { $values { "seq" "a mutable sequence" } }
818 { $description "Reverses a sequence in-place and outputs that sequence." }
819 { $side-effects "seq" } ;
820
821 HELP: padding
822 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "quot" { $quotation ( ... seq1 seq2 -- ... newseq ) } } { "newseq" "a new sequence" } }
823 { $description "Outputs a new string sequence of " { $snippet "elt" } " repeated, that when appended to " { $snippet "seq" } ", yields a sequence of length " { $snippet "n" } ". If the length of " { $snippet "seq" } " is greater than " { $snippet "n" } ", this word outputs an empty sequence." } ;
824
825 HELP: pad-head
826 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
827 { $description "Outputs a new sequence consisting of " { $snippet "seq" } " padded on the left with enough repetitions of " { $snippet "elt" } " to have the result be of length " { $snippet "n" } "." }
828 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-head print ] each" "---ab\n-quux" } } ;
829
830 HELP: pad-tail
831 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
832 { $description "Outputs a new sequence consisting of " { $snippet "seq" } " padded on the right with enough repetitions of " { $snippet "elt" } " to have the result be of length " { $snippet "n" } "." }
833 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-tail print ] each" "ab---\nquux-" } } ;
834
835 HELP: sequence=
836 { $values { "seq1" sequence } { "seq2" sequence } { "?" boolean } }
837 { $description "Tests if the two sequences have the same length and elements. This is weaker than " { $link = } ", since it does not ensure that the sequences are instances of the same class." } ;
838
839 HELP: reversed
840 { $class-description "A virtual sequence which presents a reversed view of an underlying sequence. New instances can be created by calling " { $link <reversed> } "." } ;
841
842 HELP: reverse
843 { $values { "seq" sequence } { "newseq" "a new sequence" } }
844 { $description "Outputs a new sequence having the same elements as " { $snippet "seq" } " but in reverse order." } ;
845
846 { reverse <reversed> reverse! } related-words
847
848 HELP: <reversed>
849 { $values { "seq" sequence } { "reversed" "a new sequence" } }
850 { $description "Creates an instance of the " { $link reversed } " class." }
851 { $see-also "virtual-sequences" } ;
852
853 HELP: slice-error
854 { $values { "str" "a reason" } }
855 { $description "Throws a " { $link slice-error } "." }
856 { $error-description "Thrown by " { $link <slice> } " if one of the following invalid conditions holds:"
857     { $list
858         "The start index is negative"
859         "The end index is greater than the length of the sequence"
860         "The start index is greater than the end index"
861     }
862 } ;
863
864 HELP: >slice<
865 { $values
866     { "slice" slice }
867     { "from" integer } { "to" integer } { "seq" sequence }
868 }
869 { $description "Sets up the stack for iteration with slots from a " { $link slice } ". Used with iteration in words such as " { $link sequence-operator } "." } ;
870
871 HELP: >underlying<
872 { $values
873     { "slice/seq" { $or slice sequence } }
874     { "from" integer } { "to" integer }
875 }
876 { $description "Sets up the stack for iteration with slots from a " { $link sequence } ". Used with iteration in words such as " { $link sequence-operator } "." } ;
877
878 HELP: slice
879 { $class-description "A virtual sequence which presents a subrange of the elements of an underlying sequence. New instances can be created by calling " { $link <slice> } ". Convenience words are also provided for creating slices where one endpoint is the start or end of the sequence; see " { $link "sequences-slices" } " for a list."
880 $nl
881 "Slices are mutable if the underlying sequence is mutable, and mutating a slice changes the underlying sequence. However, slices cannot be resized after creation." } ;
882
883 HELP: check-slice
884 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } }
885 { $description "Ensures that " { $snippet "m" } " is less than or equal to " { $snippet "m" } ", and that both indices are within bounds for " { $snippet "seq" } "." }
886 { $errors "Throws a " { $link slice-error } " if the preconditions are not met." } ;
887
888 HELP: collapse-slice
889 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "slice" slice } { "m'" "a non-negative integer" } { "n'" "a non-negative integer" } { "seq" sequence } }
890 { $description "Prepares to take the slice of a slice by adjusting the start and end indices accordingly, and replacing the slice with its underlying sequence." }
891 ;
892
893 HELP: <slice>
894 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "slice" slice } }
895 { $description "Outputs a new virtual sequence sharing storage with the subrange of elements in " { $snippet "seq" } " with indices starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } "." }
896 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " are out of bounds." }
897 { $notes "Taking the slice of a slice outputs a slice of the underlying sequence, instead of a slice of a slice. This means that you cannot assume that the " { $snippet "from" } " and " { $snippet "to" } " slots of the resulting slice will be equal to the values you passed to " { $link <slice> } "." } ;
898
899 { <slice> subseq } related-words
900
901 HELP: repetition
902 { $class-description "A virtual sequence consisting of " { $snippet "elt" } " repeated " { $snippet "len" } " times. Repetitions are created by calling " { $link <repetition> } "." } ;
903
904 HELP: <repetition>
905 { $values { "len" "a non-negative integer" } { "elt" object } { "repetition" repetition } }
906 { $description "Creates a new " { $link repetition } "." }
907 { $examples
908     { $example "USING: arrays prettyprint sequences ;" "10 \"X\" <repetition> >array ." "{ \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" }" }
909     { $example "USING: prettyprint sequences ;" "10 \"X\" <repetition> concat ." "\"XXXXXXXXXX\"" }
910 } ;
911 HELP: copy
912 { $values { "src" sequence } { "i" "an index in " { $snippet "dst" } } { "dst" "a mutable sequence" } }
913 { $description "Copies all elements of " { $snippet "src" } " to " { $snippet "dst" } ", with destination indices starting from " { $snippet "i" } ". Grows " { $snippet "dst" } " first if necessary." }
914 { $side-effects "dst" }
915 { $errors "An error is thrown if " { $snippet "dst" } " is not resizable, and not large enough to hold the copied elements." } ;
916
917 HELP: push-all
918 { $values { "src" sequence } { "dst" "a resizable mutable sequence" } }
919 { $description "Appends " { $snippet "src" } " to the end of " { $snippet "dst" } "." }
920 { $side-effects "dst" }
921 { $errors "Throws an error if " { $snippet "src" } " contains elements not permitted in " { $snippet "dst" } "." } ;
922
923 HELP: append
924 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
925 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
926 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
927 { $examples
928     { $example "USING: prettyprint sequences ;"
929         "{ 1 2 } B{ 3 4 } append ."
930         "{ 1 2 3 4 }"
931     }
932     { $example "USING: prettyprint sequences strings ;"
933         "\"go\" \"ing\" append ."
934         "\"going\""
935     }
936 } ;
937
938 HELP: append-as
939 { $values { "seq1" sequence } { "seq2" sequence } { "exemplar" sequence } { "newseq" sequence } }
940 { $description "Outputs a new sequence of the same type as " { $snippet "exemplar" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
941 { $errors "Throws an error if " { $snippet "seq1" } " or " { $snippet "seq2" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." }
942 { $examples
943     { $example "USING: prettyprint sequences ;"
944         "{ 1 2 } B{ 3 4 } B{ } append-as ."
945         "B{ 1 2 3 4 }"
946     }
947     { $example "USING: prettyprint sequences strings ;"
948         "\"go\" \"ing\" SBUF\" \" append-as ."
949         "SBUF\" going\""
950     }
951 } ;
952
953 { append append-as append! 3append 3append-as push-all } related-words
954
955 HELP: prepend
956 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
957 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." }
958 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
959 { $examples { $example "USING: prettyprint sequences ;"
960         "{ 1 2 } B{ 3 4 } prepend ."
961         "{ 3 4 1 2 }"
962     }
963     { $example "USING: prettyprint sequences strings ;"
964         "\"go\" \"car\" prepend ."
965         "\"cargo\""
966     }
967 } ;
968
969 HELP: prepend-as
970 { $values { "seq1" sequence } { "seq2" sequence } { "exemplar" sequence } { "newseq" sequence } }
971 { $description "Outputs a new sequence of the same type as " { $snippet "exemplar" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." }
972 { $errors "Throws an error if " { $snippet "seq1" } " or " { $snippet "seq2" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." }
973 { $examples
974     { $example "USING: prettyprint sequences ;"
975         "{ 3 4 } B{ 1 2 } B{ } prepend-as ."
976         "B{ 1 2 3 4 }"
977     }
978     { $example "USING: prettyprint sequences strings ;"
979         "\"ing\" \"go\" SBUF\" \" prepend-as ."
980         "SBUF\" going\""
981     }
982 } ;
983
984 { prepend prepend-as } related-words
985
986 HELP: 3append
987 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
988 { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn." }
989 { $errors "Throws an error if " { $snippet "seq2" } " or " { $snippet "seq3" } " contain elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
990 { $examples
991     { $example "USING: prettyprint sequences ;"
992         "\"a\" \"b\" \"c\" 3append ."
993         "\"abc\""
994     }
995 } ;
996
997 HELP: 3append-as
998 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "exemplar" sequence } { "newseq" sequence } }
999 { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn of the same type as " { $snippet "exemplar" } "." }
1000 { $errors "Throws an error if " { $snippet "seq1" } ", " { $snippet "seq2" } ", or " { $snippet "seq3" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." }
1001 { $examples
1002     { $example "USING: prettyprint sequences ;"
1003         "\"a\" \"b\" \"c\" SBUF\" \" 3append-as ."
1004         "SBUF\" abc\""
1005     }
1006 } ;
1007
1008 HELP: surround
1009 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
1010 { $description "Outputs a new sequence with " { $snippet "seq1" } " inserted between " { $snippet "seq2" } " and " { $snippet "seq3" } "." }
1011 { $examples
1012     { $example "USING: sequences prettyprint ;"
1013                "\"sssssh\" \"(\" \")\" surround ."
1014                "\"(sssssh)\""
1015     }
1016 } ;
1017
1018 HELP: surround-as
1019 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "exemplar" sequence } { "newseq" sequence } }
1020 { $description "Outputs a new sequence with " { $snippet "seq1" } " inserted between " { $snippet "seq2" } " and " { $snippet "seq3" } " of the same type as " { $snippet "exemplar" } "." }
1021 { $examples
1022     { $example "USING: sequences prettyprint ;"
1023                "\"sssssh\" \"(\" \")\" SBUF\" \" surround-as ."
1024                "SBUF\" (sssssh)\""
1025     }
1026 } ;
1027
1028 { surround surround-as } related-words
1029
1030 HELP: glue
1031 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
1032 { $description "Outputs a new sequence with " { $snippet "seq3" } " inserted between " { $snippet "seq1" } " and " { $snippet "seq2" } "." }
1033 { $examples
1034     { $example "USING: sequences prettyprint ;"
1035                "\"a\" \"b\" \",\" glue ."
1036                "\"a,b\""
1037     }
1038 } ;
1039
1040 HELP: glue-as
1041 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "exemplar" sequence } { "newseq" sequence } }
1042 { $description "Outputs a new sequence with " { $snippet "seq3" } " inserted between " { $snippet "seq1" } " and " { $snippet "seq2" } " of the same type as " { $snippet "exemplar" } "." }
1043 { $examples
1044     { $example "USING: sequences prettyprint ;"
1045                "\"a\" \"b\" \",\" SBUF\" \" glue-as ."
1046                "SBUF\" a,b\""
1047     }
1048 } ;
1049
1050 { glue glue-as } related-words
1051
1052 HELP: subseq
1053 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "subseq" "a new sequence" } }
1054 { $description "Outputs a new sequence consisting of all elements starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } "." }
1055 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
1056
1057 HELP: subseq-as
1058 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "exemplar" sequence } { "subseq" "a new sequence" } }
1059 { $description "Outputs a new sequence consisting of all elements starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } " of type " { $snippet "exemplar" } "." }
1060 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
1061
1062 HELP: clone-like
1063 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
1064 { $description "Outputs a newly-allocated sequence with the same elements as " { $snippet "seq" } " but of the same type as " { $snippet "exemplar" } "." }
1065 { $notes "Unlike " { $link like } ", this word always creates a new sequence which never shares storage with the original." } ;
1066
1067 HELP: head-slice
1068 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
1069 { $description "Outputs a virtual sequence sharing storage with the first " { $snippet "n" } " elements of the input sequence." }
1070 { $errors "Throws an error if the index is out of bounds." } ;
1071
1072 HELP: tail-slice
1073 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
1074 { $description "Outputs a virtual sequence sharing storage with all elements from the " { $snippet "n" } "th index until the end of the input sequence." }
1075 { $errors "Throws an error if the index is out of bounds." } ;
1076
1077 HELP: but-last-slice
1078 { $values { "seq" sequence } { "slice" "a slice" } }
1079 { $description "Outputs a virtual sequence sharing storage with all but the last element of the input sequence." }
1080 { $errors "Throws an error on an empty sequence." } ;
1081
1082 HELP: rest-slice
1083 { $values { "seq" sequence } { "slice" "a slice" } }
1084 { $description "Outputs a virtual sequence sharing storage with all elements from the 1st index until the end of the input sequence." }
1085 { $notes "Equivalent to " { $snippet "1 tail" } }
1086 { $errors "Throws an error on an empty sequence." } ;
1087
1088 HELP: head-slice*
1089 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
1090 { $description "Outputs a virtual sequence sharing storage with all elements of " { $snippet "seq" } " until the " { $snippet "n" } "th element from the end. In other words, it outputs a sequence of the first " { $snippet "l-n" } " elements of the input sequence, where " { $snippet "l" } " is its length." }
1091 { $errors "Throws an error if the index is out of bounds." } ;
1092
1093 { head-slice head-slice* } related-words
1094
1095 HELP: tail-slice*
1096 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
1097 { $description "Outputs a virtual sequence sharing storage with the last " { $snippet "n" } " elements of the input sequence." }
1098 { $errors "Throws an error if the index is out of bounds." } ;
1099
1100 { tail-slice tail-slice* } related-words
1101
1102 HELP: head-to-index
1103 { $values
1104     { "seq" sequence } { "to" integer }
1105     { "zero" object }
1106 }
1107 { $description "Sets up the stack for the " { $link head } " word." } ;
1108
1109 HELP: head
1110 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
1111 { $description "Outputs a new sequence consisting of the first " { $snippet "n" } " elements of the input sequence." }
1112 { $examples
1113     { $example "USING: sequences prettyprint ;"
1114         "{ 1 2 3 4 5 6 7 } 2 head ."
1115         "{ 1 2 }"
1116     }
1117     "When a sequence may not have enough elements:"
1118     { $example "USING: sequences prettyprint ;"
1119         "{ 1 2 } 5 index-or-length head ."
1120         "{ 1 2 }"
1121     }
1122 }
1123 { $errors "Throws an error if the index is out of bounds." } ;
1124
1125 HELP: index-to-tail
1126 { $values
1127     { "seq" sequence } { "from" integer }
1128     { "length" object }
1129 }
1130 { $description "Sets up the stack for the " { $link tail } " word." } ;
1131
1132 HELP: tail
1133 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
1134 { $description "Outputs a new sequence consisting of the input sequence with the first " { $snippet "n" } " items removed." }
1135 { $examples
1136     { $example "USING: sequences prettyprint ;"
1137         "{ 1 2 3 4 5 6 7 } 2 tail ."
1138         "{ 3 4 5 6 7 }"
1139     }
1140     "When a sequence may not have enough elements:"
1141     { $example "USING: sequences prettyprint ;"
1142         "{ 1 2 } 5 index-or-length tail ."
1143         "{ }"
1144     }
1145 }
1146 { $errors "Throws an error if the index is out of bounds." } ;
1147
1148 HELP: but-last
1149 { $values { "seq" sequence } { "headseq" "a new sequence" } }
1150 { $description "Outputs a new sequence consisting of the input sequence with the last item removed." }
1151 { $errors "Throws an error on an empty sequence." } ;
1152
1153 HELP: rest
1154 { $values { "seq" sequence } { "tailseq" "a new sequence" } }
1155 { $description "Outputs a new sequence consisting of the input sequence with the first item removed." }
1156 { $errors "Throws an error on an empty sequence." } ;
1157
1158 HELP: head*
1159 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
1160 { $description "Outputs a new sequence consisting of all elements of " { $snippet "seq" } " until the " { $snippet "n" } "th element from the end. In other words, it removes the last " { $snippet "n" } " elements." }
1161 { $examples
1162     { $example "USING: sequences prettyprint ;"
1163         "{ 1 2 3 4 5 6 7 } 2 head* ."
1164         "{ 1 2 3 4 5 }"
1165     }
1166     "When a sequence may not have enough elements:"
1167     { $example "USING: sequences prettyprint ;"
1168         "{ 1 2 } 5 index-or-length head* ."
1169         "{ }"
1170     }
1171 }
1172 { $errors "Throws an error if the index is out of bounds." } ;
1173
1174 HELP: tail*
1175 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
1176 { $description "Outputs a new sequence consisting of the last " { $snippet "n" } " elements of the input sequence." }
1177 { $examples
1178     { $example "USING: sequences prettyprint ;"
1179         "{ 1 2 3 4 5 6 7 } 2 tail* ."
1180         "{ 6 7 }"
1181     }
1182     "When a sequence may not have enough elements:"
1183     { $example "USING: sequences prettyprint ;"
1184         "{ 1 2 } 5 index-or-length tail* ."
1185         "{ 1 2 }"
1186     }
1187 }
1188 { $errors "Throws an error if the index is out of bounds." } ;
1189
1190 { tail tail* tail-slice tail-slice* } related-words
1191 { head head* head-slice head-slice* } related-words
1192 { cut cut* cut-slice cut-slice* } related-words
1193 { unclip unclip-slice unclip-last unclip-last-slice } related-words
1194 { first last but-last but-last-slice rest rest-slice } related-words
1195
1196 HELP: shorter?
1197 { $values { "seq1" sequence } { "seq2" sequence } { "?" boolean } }
1198 { $description "Tests if the length of " { $snippet "seq1" } " is smaller than the length of " { $snippet "seq2" } "." } ;
1199
1200 HELP: head?
1201 { $values { "seq" sequence } { "begin" sequence } { "?" boolean } }
1202 { $description "Tests if " { $snippet "seq" } " starts with " { $snippet "begin" } ". If " { $snippet "begin" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." }
1203 { $examples
1204   { $example
1205     "USING: prettyprint sequences ;"
1206     "{ \"accept\" \"adept\" \"advance\" \"advice\" \"affect\" } [ \"ad\" head? ] filter ."
1207     "{ \"adept\" \"advance\" \"advice\" }"
1208   }
1209 } ;
1210
1211 HELP: tail?
1212 { $values { "seq" sequence } { "end" sequence } { "?" boolean } }
1213 { $description "Tests if " { $snippet "seq" } " ends with " { $snippet "end" } ". If " { $snippet "end" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
1214
1215 { remove remove-nth remove-eq remove-eq! remove! remove-nth! } related-words
1216
1217 HELP: cut-slice
1218 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before-slice" "a slice" } { "after-slice" "a slice" } }
1219 { $description "Outputs a pair of sequences, where " { $snippet "before-slice" } " is a slice of the first " { $snippet "n" } " elements of " { $snippet "seq" } ", while " { $snippet "after-slice" } " is a slice of the remaining elements." }
1220 { $notes "Unlike " { $link cut } ", this is suitable for use in an iterative algorithm which cuts successive pieces off a sequence." } ;
1221
1222 HELP: cut-slice*
1223 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before-slice" "a slice" } { "after-slice" "a slice" } }
1224 { $description "Outputs a pair of sequences, where " { $snippet "after" } " consists of the last " { $snippet "n" } " elements of " { $snippet "seq" } ", while " { $snippet "before-slice" } " is a slice of the remaining elements." }
1225 { $notes "Unlike " { $link cut* } ", this is suitable for use in an iterative algorithm which cuts successive pieces off a sequence." } ;
1226
1227 HELP: cut
1228 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
1229 { $description "Outputs a pair of sequences, where " { $snippet "before" } " consists of the first " { $snippet "n" } " elements of " { $snippet "seq" } ", while " { $snippet "after" } " holds the remaining elements. Both output sequences have the same type as " { $snippet "seq" } "." }
1230 { $notes "Since this word copies the entire tail of the sequence, it should not be used in a loop. If this is important, consider using " { $link cut-slice } " instead, since it returns a slice for the tail instead of copying." } ;
1231
1232 HELP: cut*
1233 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
1234 { $description "Outputs a pair of sequences, where " { $snippet "after" } " consists of the last " { $snippet "n" } " elements of " { $snippet "seq" } ", while " { $snippet "before" } " holds the remaining elements. Both output sequences have the same type as " { $snippet "seq" } "." } ;
1235
1236 HELP: subseq-starts-at?
1237 { $values { "i" "a start index" } { "seq" sequence } { "subseq" sequence } { "?" boolean } }
1238 { $description "Outputs " { $snippet "t" } " if the subseq starts at the " { $snippet "i" } "th element or outputs " { $link f } " if the sequence is not at that position." } ;
1239
1240 HELP: subseq-index
1241 { $values { "seq" sequence } { "subseq" sequence } { "i/f" "a start index or " { $snippet "f" } } }
1242 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", starting the search from the " { $snippet "n" } "th element. If no matching subsequence is found, outputs " { $link f } "." } ;
1243
1244 HELP: subseq-index-from
1245 { $values { "n" "a start index" } { "seq" sequence } { "subseq" sequence } { "i/f" "a start index or " { $snippet "f" } } }
1246 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", starting the search from the " { $snippet "n" } "th element. If no matching subsequence is found, outputs " { $link f } "." } ;
1247
1248 HELP: subseq-start-from
1249 { $values
1250     { "subseq" object } { "seq" sequence } { "n" integer }
1251     { "i/f" { $maybe integer } }
1252 }
1253 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", or " { $link f } " if no matching subsequence is found starting from " { $snippet "n" } "." } ;
1254
1255 HELP: subseq-start
1256 { $values { "subseq" sequence } { "seq" sequence } { "i/f" "a start index or " { $snippet "f" } } }
1257 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", or " { $link f } " if no matching subsequence is found." } ;
1258
1259 HELP: subseq?
1260 { $values { "subseq" sequence } { "seq" sequence } { "?" boolean } }
1261 { $description "Tests if " { $snippet "seq" } " contains the elements of " { $snippet "subseq" } " as a contiguous subsequence." } ;
1262
1263 HELP: subseq-of?
1264 { $values { "seq" sequence } { "subseq" sequence } { "?" boolean } }
1265 { $description "Tests if " { $snippet "seq" } " contains the elements of " { $snippet "subseq" } " as a contiguous subsequence." } ;
1266
1267 HELP: drop-prefix
1268 { $values { "seq1" sequence } { "seq2" sequence } { "slice1" "a slice" } { "slice2" "a slice" } }
1269 { $description "Outputs a pair of virtual sequences with the common prefix of " { $snippet "seq1" } " and " { $snippet "seq2" } " removed." } ;
1270
1271 HELP: unclip
1272 { $values { "seq" sequence } { "rest" sequence } { "first" object } }
1273 { $description "Outputs a tail sequence and the first element of " { $snippet "seq" } "; the tail sequence consists of all elements of " { $snippet "seq" } " but the first." }
1274 { $examples
1275     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip suffix ." "{ 2 3 1 }" }
1276 } ;
1277
1278 HELP: unclip-slice
1279 { $values { "seq" sequence } { "rest-slice" slice } { "first" object } }
1280 { $description "Outputs a tail sequence and the first element of " { $snippet "seq" } "; the tail sequence consists of all elements of " { $snippet "seq" } " but the first. Unlike " { $link unclip } ", this word does not make a copy of the input sequence, and runs in constant time." }
1281 { $examples { $example "USING: math.order prettyprint sequences ;" "{ 3 -1 -10 5 7 } unclip-slice [ min ] reduce ." "-10" } } ;
1282
1283 HELP: unclip-last
1284 { $values { "seq" sequence } { "butlast" sequence } { "last" object } }
1285 { $description "Outputs a head sequence and the last element of " { $snippet "seq" } "; the head sequence consists of all elements of " { $snippet "seq" } " but the last." }
1286 { $examples
1287     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip-last prefix ." "{ 3 1 2 }" }
1288 } ;
1289
1290 HELP: unclip-last-slice
1291 { $values { "seq" sequence } { "butlast-slice" slice } { "last" object } }
1292 { $description "Outputs a head sequence and the last element of " { $snippet "seq" } "; the head sequence consists of all elements of " { $snippet "seq" } " but the last. Unlike " { $link unclip-last } ", this word does not make a copy of the input sequence, and runs in constant time." } ;
1293
1294 HELP: sum
1295 { $values { "seq" { $sequence number } } { "n" number } }
1296 { $description "Outputs the sum of all elements of " { $snippet "seq" } ". Outputs zero given an empty sequence." } ;
1297
1298 HELP: product
1299 { $values { "seq" { $sequence number } } { "n" number } }
1300 { $description "Outputs the product of all elements of " { $snippet "seq" } ". Outputs one given an empty sequence." } ;
1301
1302 HELP: infimum
1303 { $values { "seq" sequence } { "elt" object } }
1304 { $description "Outputs the least element of " { $snippet "seq" } "." }
1305 { $examples
1306     "Example:"
1307     { $example "USING: sequences prettyprint ;"
1308         "{ 1 2 3 4 5 } infimum ."
1309         "1"
1310     }
1311     "Example:"
1312     { $example "USING: sequences prettyprint ;"
1313         "{ \"c\" \"b\" \"a\" } infimum ."
1314         "\"a\""
1315     }
1316 }
1317 { $errors "Throws an error if the sequence is empty." } ;
1318
1319 HELP: infimum-by
1320 { $values
1321     { "seq" sequence } { "quot" quotation }
1322     { "elt" object }
1323 }
1324 { $description "Outputs the least element of " { $snippet "seq" } " according to the " { $snippet "quot" } "." }
1325 { $examples
1326     "Example:"
1327     { $example "USING: sequences prettyprint ;"
1328         "{ { 1 2 } { 1 2 3 } { 1 2 3 4 } } [ length ] infimum-by ."
1329         "{ 1 2 }"
1330     }
1331 }
1332 { $errors "Throws an error if the sequence is empty." } ;
1333
1334 HELP: supremum
1335 { $values { "seq" sequence } { "elt" object } }
1336 { $description "Outputs the greatest element of " { $snippet "seq" } "." }
1337 { $examples
1338     "Example:"
1339     { $example "USING: sequences prettyprint ;"
1340         "{ 1 2 3 4 5 } supremum ."
1341         "5"
1342     }
1343     "Example:"
1344     { $example "USING: sequences prettyprint ;"
1345         "{ \"c\" \"b\" \"a\" } supremum ."
1346         "\"c\""
1347     }
1348 }
1349 { $errors "Throws an error if the sequence is empty." } ;
1350
1351 HELP: supremum-by
1352 { $values
1353     { "seq" sequence } { "quot" quotation }
1354     { "elt" object }
1355 }
1356 { $description "Outputs the greatest element of " { $snippet "seq" } " according to the " { $snippet "quot" } "." }
1357 { $examples
1358     "Example:"
1359     { $example "USING: sequences prettyprint ;"
1360         "{ { 1 2 } { 1 2 3 } { 1 2 3 4 } } [ length ] supremum-by ."
1361         "{ 1 2 3 4 }"
1362     }
1363 }
1364 { $errors "Throws an error if the sequence is empty." } ;
1365
1366 { min max infimum infimum-by supremum supremum-by } related-words
1367
1368 HELP: shortest
1369 { $values { "seqs" sequence } { "elt" object } }
1370 { $description "Outputs the shortest sequence from " { $snippet "seqs" } "." } ;
1371
1372 HELP: longest
1373 { $values { "seqs" sequence } { "elt" object } }
1374 { $description "Outputs the longest sequence from " { $snippet "seqs" } "." } ;
1375
1376 { shortest longest } related-words
1377
1378 HELP: produce
1379 { $values { "pred" { $quotation ( ..a -- ..b ? ) } } { "quot" { $quotation ( ..b -- ..a obj ) } } { "seq" sequence } }
1380 { $description "Calls " { $snippet "pred" } " repeatedly. If the predicate yields " { $link f } ", stops, otherwise, calls " { $snippet "quot" } " to yield a value. Values are accumulated and returned in a sequence at the end." }
1381 { $examples
1382     "The following example divides a number by two until we reach zero, and accumulates intermediate results:"
1383     { $example "USING: kernel math prettyprint sequences ;" "1337 [ dup 0 > ] [ 2/ dup ] produce nip ." "{ 668 334 167 83 41 20 10 5 2 1 0 }" }
1384     "The following example collects random numbers as long as they are greater than 1:"
1385     { $unchecked-example "USING: kernel prettyprint random sequences ;" "[ 10 random dup 1 > ] [ ] produce nip ." "{ 8 2 2 9 }" }
1386 } ;
1387
1388 HELP: produce-as
1389 { $values { "pred" { $quotation ( ..a -- ..b ? ) } } { "quot" { $quotation ( ..b -- ..a obj ) } } { "exemplar" sequence } { "seq" sequence } }
1390 { $description "Calls " { $snippet "pred" } " repeatedly. If the predicate yields " { $link f } ", stops, otherwise, calls " { $snippet "quot" } " to yield a value. Values are accumulated and returned in a sequence of type " { $snippet "exemplar" } " at the end." }
1391 { $examples "See " { $link produce } " for examples." } ;
1392
1393 HELP: map-sum
1394 { $values { "seq" sequence } { "quot" quotation } { "n" number } }
1395 { $description "Like " { $snippet "map sum" } ", but without creating an intermediate sequence." }
1396 { $examples
1397     { $example
1398         "USING: math ranges sequences prettyprint ;"
1399         "100 [1..b] [ sq ] map-sum ."
1400         "338350"
1401     }
1402 } ;
1403
1404 HELP: count
1405 { $values { "seq" sequence } { "quot" quotation } { "n" integer } }
1406 { $description "Efficiently returns the number of elements that the predicate quotation matches." }
1407 { $examples
1408     { $example
1409         "USING: math ranges sequences prettyprint ;"
1410         "100 [1..b] [ even? ] count ."
1411         "50"
1412     }
1413 } ;
1414
1415 HELP: selector
1416 { $values
1417     { "quot" { $quotation ( ... elt -- ... ? ) } }
1418     { "selector" { $quotation ( ... elt -- ... ) } } { "accum" vector } }
1419 { $description "Creates a new vector to accumulate the values which return true for a predicate. Returns a new quotation which accepts an object to be tested and stored in the collector if the test yields true. The collector is left on the stack for convenience." }
1420 { $examples
1421     { $example "! Find all the even numbers:" "USING: prettyprint sequences math kernel ;"
1422                "10 <iota> [ even? ] selector [ each ] dip ."
1423                "V{ 0 2 4 6 8 }"
1424     }
1425 }
1426 { $notes "Used to implement the " { $link filter } " word. Compare this word with " { $link collector } ", which is an unfiltering version." } ;
1427
1428 HELP: trim-head
1429 { $values
1430     { "seq" sequence } { "quot" quotation }
1431     { "newseq" sequence } }
1432 { $description "Removes elements starting from the left side of a sequence if they match a predicate. Once an element does not match, the test stops and the rest of the sequence is left on the stack as a new sequence." }
1433 { $examples
1434     { $example "USING: prettyprint math sequences ;"
1435                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head ."
1436                "{ 1 2 3 0 0 }"
1437     }
1438 } ;
1439
1440 HELP: trim-head-slice
1441 { $values
1442     { "seq" sequence } { "quot" quotation }
1443     { "slice" slice } }
1444 { $description "Removes elements starting from the left side of a sequence if they match a predicate. Once an element does not match, the test stops and the rest of the sequence is left on the stack as a slice." }
1445 { $examples
1446     { $example "USING: prettyprint math sequences ;"
1447                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head-slice ."
1448                "T{ slice { from 2 } { to 7 } { seq { 0 0 1 2 3 0 0 } } }"
1449     }
1450 } ;
1451
1452 HELP: trim-tail
1453 { $values
1454     { "seq" sequence } { "quot" quotation }
1455     { "newseq" sequence } }
1456 { $description "Removes elements starting from the right side of a sequence if they match a predicate. Once an element does not match, the test stops and the rest of the sequence is left on the stack as a new sequence." }
1457 { $examples
1458     { $example "USING: prettyprint math sequences ;"
1459                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail ."
1460                "{ 0 0 1 2 3 }"
1461     }
1462 } ;
1463
1464 HELP: trim-tail-slice
1465 { $values
1466     { "seq" sequence } { "quot" quotation }
1467     { "slice" slice } }
1468 { $description "Removes elements starting from the right side of a sequence if they match a predicate. Once an element does not match, the test stops and the rest of the sequence is left on the stack as a slice." }
1469 { $examples
1470     { $example "USING: prettyprint math sequences ;"
1471                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail-slice ."
1472                "T{ slice { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1473     }
1474 } ;
1475
1476 HELP: trim
1477 { $values
1478     { "seq" sequence } { "quot" quotation }
1479     { "newseq" sequence } }
1480 { $description "Removes elements starting from the left and right sides of a sequence if they match a predicate. Once an element does not match, the test stops and the rest of the sequence is left on the stack as a new sequence." }
1481 { $examples
1482     { $example "USING: prettyprint math sequences ;"
1483                "{ 0 0 1 2 3 0 0 } [ zero? ] trim ."
1484                "{ 1 2 3 }"
1485     }
1486 } ;
1487
1488 HELP: trim-slice
1489 { $values
1490     { "seq" sequence } { "quot" quotation }
1491     { "slice" slice } }
1492 { $description "Removes elements starting from the left and right sides of a sequence if they match a predicate. Once an element does not match, the test stops and the rest of the sequence is left on the stack as a slice." }
1493 { $examples
1494     { $example "USING: prettyprint math sequences ;"
1495                "{ 0 0 1 2 3 0 0 } [ zero? ] trim-slice ."
1496                "T{ slice { from 2 } { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1497     }
1498 } ;
1499
1500 { trim trim-slice trim-head trim-head-slice trim-tail trim-tail-slice } related-words
1501
1502 HELP: sift
1503 { $values
1504     { "seq" sequence }
1505     { "newseq" sequence } }
1506 { $description "Outputs a new sequence with all instances of " { $link f } " removed." }
1507 { $examples
1508     { $example "USING: prettyprint sequences ;"
1509         "{ \"a\" 3 { } f } sift ."
1510         "{ \"a\" 3 { } }"
1511     }
1512 } ;
1513
1514 HELP: harvest
1515 { $values
1516     { "seq" sequence }
1517     { "newseq" sequence } }
1518 { $description "Outputs a new sequence with all empty sequences removed." }
1519 { $examples
1520     { $example "USING: prettyprint sequences ;"
1521                "{ { } { 2 3 } { 5 } { } } harvest ."
1522                "{ { 2 3 } { 5 } }"
1523     }
1524 } ;
1525
1526 { filter filter-as filter! reject reject-as reject! sift harvest } related-words
1527
1528 HELP: set-first
1529 { $values
1530     { "first" object } { "seq" sequence } }
1531 { $description "Sets the first element of a sequence." }
1532 { $examples
1533     { $example "USING: prettyprint kernel sequences ;"
1534         "{ 1 2 3 4 } 5 over set-first ."
1535         "{ 5 2 3 4 }"
1536     }
1537 } ;
1538
1539 HELP: set-second
1540 { $values
1541     { "second" object } { "seq" sequence } }
1542 { $description "Sets the second element of a sequence." }
1543 { $examples
1544     { $example "USING: prettyprint kernel sequences ;"
1545         "{ 1 2 3 4 } 5 over set-second ."
1546         "{ 1 5 3 4 }"
1547     }
1548 } ;
1549
1550 HELP: set-third
1551 { $values
1552     { "third" object } { "seq" sequence } }
1553 { $description "Sets the third element of a sequence." }
1554 { $examples
1555     { $example "USING: prettyprint kernel sequences ;"
1556         "{ 1 2 3 4 } 5 over set-third ."
1557         "{ 1 2 5 4 }"
1558     }
1559 } ;
1560
1561 HELP: set-fourth
1562 { $values
1563     { "fourth" object } { "seq" sequence } }
1564 { $description "Sets the fourth element of a sequence." }
1565 { $examples
1566     { $example "USING: prettyprint kernel sequences ;"
1567         "{ 1 2 3 4 } 5 over set-fourth ."
1568         "{ 1 2 3 5 }"
1569     }
1570 } ;
1571
1572 { set-first set-second set-third set-fourth } related-words
1573
1574 HELP: replicate
1575 { $values
1576     { "len" integer } { "quot" { $quotation ( ... -- ... newelt ) } }
1577     { "newseq" sequence } }
1578     { $description "Calls the quotation " { $snippet "len" } " times, collecting results into a new array." }
1579 { $examples
1580     { $unchecked-example "USING: kernel prettyprint random sequences ;"
1581         "5 [ 100 random ] replicate ."
1582         "{ 52 10 45 81 30 }"
1583     }
1584 } ;
1585
1586 HELP: replicate-as
1587 { $values
1588     { "len" integer } { "quot" { $quotation ( ... -- ... newelt ) } } { "exemplar" sequence }
1589     { "newseq" sequence } }
1590 { $description "Calls the quotation " { $snippet "len" } " times, collecting results into a new sequence of the same type as the exemplar sequence." }
1591 { $examples
1592     { $unchecked-example "USING: prettyprint kernel sequences ;"
1593         "5 [ 100 random ] B{ } replicate-as ."
1594         "B{ 44 8 2 33 18 }"
1595     }
1596 } ;
1597
1598 { replicate replicate-as } related-words
1599
1600 HELP: partition
1601 { $values
1602     { "seq" sequence } { "quot" quotation }
1603     { "trueseq" sequence } { "falseseq" sequence } }
1604     { $description "Calls a predicate quotation on each element of the input sequence. If the test yields true, the element is added to " { $snippet "trueseq" } "; if false, it's added to " { $snippet "falseseq" } "." }
1605 { $examples
1606     { $example "USING: prettyprint kernel math sequences ;"
1607         "{ 1 2 3 4 5 } [ even? ] partition [ . ] bi@"
1608         "{ 2 4 }\n{ 1 3 5 }"
1609     }
1610 } ;
1611
1612 HELP: virtual-exemplar
1613 { $values
1614     { "seq" sequence }
1615     { "seq'" sequence } }
1616 { $description "Part of the virtual sequence protocol, this word is used to return an exemplar of the underlying storage. This is used in words like " { $link new-sequence } "." } ;
1617
1618 HELP: virtual@
1619 { $values
1620     { "n" integer } { "seq" sequence }
1621     { "n'" integer } { "seq'" sequence } }
1622 { $description "Part of the sequence protocol, this word translates the input index " { $snippet "n" } " into an index and the underlying storage this index points into." } ;
1623
1624 HELP: 2map-reduce
1625 { $values
1626     { "seq1" sequence } { "seq2" sequence } { "map-quot" { $quotation ( ..a elt1 elt2 -- ..a intermediate ) } } { "reduce-quot" { $quotation ( ..a prev intermediate -- ..a next ) } }
1627     { "result" object } }
1628 { $description "Calls " { $snippet "map-quot" } " on each pair of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } " and combines the results using " { $snippet "reduce-quot" } " in the same manner as " { $link reduce } ", except that there is no identity element, and the sequence must have a length of at least 1." }
1629 { $errors "Throws an error if the sequence is empty." }
1630 { $examples { $example "USING: sequences prettyprint math ;"
1631     "{ 10 30 50 } { 200 400 600 } [ + ] [ + ] 2map-reduce ."
1632     "1290"
1633 } } ;
1634
1635 HELP: 2selector
1636 { $values
1637     { "quot" quotation }
1638     { "selector" quotation } { "accum1" vector } { "accum2" vector } }
1639 { $description "Creates two new vectors to accumulate values based on a predicate. The first vector accumulates values for which the predicate yields true; the second for false." } ;
1640
1641 HELP: collector
1642 { $values
1643     { "quot" quotation }
1644     { "quot'" quotation } { "vec" vector } }
1645 { $description "Creates a new quotation that pushes its result to a vector and outputs that vector on the stack." }
1646 { $examples { $example "USING: sequences prettyprint kernel math ;"
1647     "{ 1 2 } [ 30 + ] collector [ each ] dip ."
1648     "V{ 31 32 }"
1649 } } ;
1650
1651 HELP: binary-reduce
1652 { $values
1653     { "seq" sequence } { "start" integer } { "quot" { $quotation ( elt1 elt2 -- newelt ) } }
1654     { "value" object } }
1655 { $description "Like " { $link reduce } ", but splits the sequence in half recursively until each sequence is small enough, and calls the quotation on these smaller sequences. If the quotation computes values that depend on the size of their input, such as bignum arithmetic, then this algorithm can be more efficient than using " { $link reduce } "." }
1656 { $examples "Computing factorial:"
1657     { $example "USING: prettyprint sequences math ;"
1658     "40 <iota> rest-slice 1 [ * ] binary-reduce ."
1659     "20397882081197443358640281739902897356800000000" }
1660 } ;
1661
1662 HELP: follow
1663 { $values
1664     { "obj" object } { "quot" { $quotation ( ... prev -- ... result/f ) } }
1665     { "seq" sequence } }
1666 { $description "Outputs a sequence containing the input object and all of the objects generated by successively feeding the result of the quotation called on the input object to the quotation recursively. Objects yielded by the quotation are added to the output sequence until the quotation yields " { $link f } ", at which point the recursion terminates." }
1667 { $examples "Get random numbers until zero is reached:"
1668     { $unchecked-example
1669     "USING: random sequences prettyprint math ;"
1670     "100 [ random [ f ] when-zero ] follow ."
1671     "{ 100 86 34 32 24 11 7 2 }"
1672 } } ;
1673
1674 HELP: halves
1675 { $values
1676     { "seq" sequence }
1677     { "first-slice" slice } { "second-slice" slice } }
1678 { $description "Splits a sequence into two slices at the midpoint. If the sequence has an odd number of elements, the extra element is returned in the second slice." }
1679 { $examples { $example "USING: arrays sequences prettyprint kernel ;"
1680     "{ 1 2 3 4 5 } halves [ >array . ] bi@"
1681     "{ 1 2 }\n{ 3 4 5 }"
1682 } } ;
1683
1684 HELP: indices
1685 { $values
1686     { "obj" object } { "seq" sequence }
1687     { "indices" sequence } }
1688 { $description "Compares the input object to every element in the sequence and returns a vector containing the index of every position where the element was found." }
1689 { $examples { $example "USING: sequences prettyprint ;"
1690     "2 { 2 4 2 6 2 8 2 10 } indices ."
1691     "V{ 0 2 4 6 }"
1692 } } ;
1693
1694 HELP: insert-nth
1695 { $values
1696     { "elt" object } { "n" integer } { "seq" sequence }
1697     { "seq'" sequence } }
1698 { $description "Creates a new sequence where the " { $snippet "n" } "th index is set to the input object." }
1699 { $examples { $example "USING: prettyprint sequences ;"
1700     "40 3 { 10 20 30 50 } insert-nth ."
1701     "{ 10 20 30 40 50 }"
1702 } } ;
1703
1704 HELP: map-reduce
1705 { $values
1706     { "seq" sequence } { "map-quot" { $quotation ( ..a elt -- ..a intermediate ) } } { "reduce-quot" { $quotation ( ..a prev intermediate -- ..a next ) } }
1707     { "result" object } }
1708 { $description "Calls " { $snippet "map-quot" } " on each element and combines the results using " { $snippet "reduce-quot" } " in the same manner as " { $link reduce } ", except that there is no identity element, and the sequence must have a length of at least 1." }
1709 { $errors "Throws an error if the sequence is empty." }
1710 { $examples { $example "USING: sequences prettyprint math ;"
1711     "{ 1 3 5 } [ sq ] [ + ] map-reduce ."
1712     "35"
1713 } } ;
1714
1715 HELP: new-like
1716 { $values
1717     { "len" integer } { "exemplar" "an exemplar sequence" } { "quot" quotation }
1718     { "seq" sequence } }
1719 { $description "Creates a new sequence of length " { $snippet "len" } " and calls the quotation with this sequence on the stack. The output of the quotation and the original exemplar are then passed to " { $link like } " so that the output sequence is the exemplar's type." } ;
1720
1721 HELP: push-either
1722 { $values
1723     { "elt" object } { "quot" quotation } { "accum1" vector } { "accum2" vector } }
1724 { $description "Pushes the input object onto one of the accumulators; the first if the quotation yields true, the second if false." } ;
1725
1726 HELP: sequence-hashcode
1727 { $values
1728     { "n" integer } { "seq" sequence }
1729     { "x" integer } }
1730 { $description "Iterates over a sequence, computes a hashcode with " { $link hashcode* } " for each element, and combines them using " { $link sequence-hashcode-step } "." } ;
1731
1732 HELP: sequence-hashcode-step
1733 { $values
1734     { "oldhash" integer } { "newpart" integer }
1735     { "newhash" integer } }
1736 { $description "An implementation word that computes a running hashcode of a sequence using some bit-twiddling. The resulting hashcode is always a fixnum." } ;
1737
1738 HELP: index-or-length
1739 { $values
1740     { "seq" sequence } { "n" integer } { "n'" integer } }
1741 { $description "Returns the input sequence and its length or " { $snippet "n" } ", whichever is less." }
1742 { $examples { $example "USING: sequences kernel prettyprint ;"
1743     "\"abcd\" 3 index-or-length [ . ] bi@"
1744     "\"abcd\"\n3"
1745 } } ;
1746
1747 HELP: shorten
1748 { $values
1749     { "n" integer } { "seq" sequence } }
1750 { $description "Shortens a " { $link "growable" } " sequence to be " { $snippet "n" } " elements long." }
1751 { $examples { $example "USING: sequences prettyprint kernel ;"
1752     "V{ 1 2 3 4 5 } 3 over shorten ."
1753     "V{ 1 2 3 }"
1754 } } ;
1755
1756 HELP: <iota>
1757 { $values { "n" integer } { "iota" iota } }
1758 { $description "Creates an immutable virtual sequence containing the integers from 0 to " { $snippet "n-1" } "." }
1759 { $examples
1760   { $example
1761     "USING: math sequences prettyprint ;"
1762     "3 <iota> [ sq ] map ."
1763     "{ 0 1 4 }"
1764   }
1765 } ;
1766
1767 HELP: assert-sequence=
1768 { $values
1769     { "a" sequence } { "b" sequence }
1770 }
1771 { $description "Throws an error if all the elements of two sequences, taken pairwise, are not equal." }
1772 { $notes "The sequences need not be of the same type." }
1773 { $examples
1774   { $code
1775     "USING: prettyprint sequences ;"
1776     "{ 1 2 3 } V{ 1 2 3 } assert-sequence="
1777   }
1778 } ;
1779
1780 HELP: cartesian-find
1781 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... ? ) } } { "elt1" object } { "elt2" object } }
1782 { $description "Applies the quotation to every possible pairing of elements from the two sequences, returning the first two elements where the quotation returns a true value." } ;
1783
1784 HELP: cartesian-each
1785 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... ) } } }
1786 { $description "Applies the quotation to every possible pairing of elements from the two sequences." } ;
1787
1788 HELP: cartesian-map
1789 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... newelt ) } } { "newseq" "a new sequence of sequences" } }
1790 { $description "Applies the quotation to every possible pairing of elements from the two sequences, collecting results into a new sequence of sequences." } ;
1791
1792 HELP: cartesian-product-as
1793 { $values { "seq1" sequence } { "seq2" sequence } { "exemplar" sequence } { "newseq" "a new sequence of sequences of pairs" } }
1794 { $description "Outputs a sequence of all possible pairings of elements from the two sequences so that the output sequence is the exemplar's type." }
1795 { $examples
1796     { $example
1797         "USING: bit-arrays prettyprint sequences ;"
1798         "\"ab\" ?{ t f } { } cartesian-product-as ."
1799         "{ { { 97 t } { 97 f } } { { 98 t } { 98 f } } }"
1800     }
1801 } ;
1802
1803 HELP: cartesian-product
1804 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" "a new sequence of sequences of pairs" } }
1805 { $description "Outputs a sequence of all possible pairings of elements from the two sequences, using the type of " { $snippet "seq2" } "." }
1806 { $examples
1807     { $example
1808         "USING: prettyprint sequences ;"
1809         "{ 1 2 } { 3 4 } cartesian-product ."
1810         "{ { { 1 3 } { 1 4 } } { { 2 3 } { 2 4 } } }"
1811     }
1812     { $example
1813         "USING: prettyprint sequences ;"
1814         "\"abc\" \"def\" cartesian-product ."
1815         "{ { \"ad\" \"ae\" \"af\" } { \"bd\" \"be\" \"bf\" } { \"cd\" \"ce\" \"cf\" } }"
1816     }
1817 } ;
1818
1819 { cartesian-find cartesian-each cartesian-map cartesian-product cartesian-product-as } related-words
1820
1821 ARTICLE: "sequences-unsafe" "Unsafe sequence operations"
1822 "The " { $link nth-unsafe } " and " { $link set-nth-unsafe } " sequence protocol bypasses bounds checks for increased performance."
1823 $nl
1824 "These words assume the sequence index given is within bounds; if it is not, memory corruption can occur. Great care must be exercised when using these words. First, make sure the code in question is actually a bottleneck; next, try improving the algorithm first. If all else fails, then the unsafe sequence words can be used."
1825 $nl
1826 "There is a very important invariant these word must preserve: if at some point in time, the length of a sequence was " { $snippet "n" } ", then any future lookups of elements with indices below " { $snippet "n" } " must not crash the VM, even if the sequence length is now less than " { $snippet "n" } ". For example, vectors preserve this invariant by never shrinking the underlying storage, only growing it as necessary."
1827 $nl
1828 "The justification for this is that the VM should not crash if a resizable sequence is resized during the execution of an iteration combinator."
1829 $nl
1830 "Indeed, iteration combinators are the primary use-case for these words; if the iteration index is already guarded by a loop test which ensures it is within bounds, then additional bounds checks are redundant. For example, see the implementation of " { $link each } "." ;
1831
1832 ARTICLE: "sequence-protocol" "Sequence protocol"
1833 "All sequences must be instances of a mixin class:"
1834 { $subsections sequence sequence? }
1835 "All sequences must know their length:"
1836 { $subsections length }
1837 "At least one of the following two generic words must have a method for accessing elements; the " { $link sequence } " mixin has default definitions which are mutually recursive:"
1838 { $subsections nth nth-unsafe ?nth }
1839 "Note that sequences are always indexed starting from zero."
1840 $nl
1841 "At least one of the following two generic words must have a method for storing elements; the " { $link sequence } " mixin has default definitions which are mutually recursive:"
1842 { $subsections set-nth set-nth-unsafe ?set-nth }
1843 "If your sequence is immutable, then you must implement either " { $link set-nth } " or " { $link set-nth-unsafe } " to simply call " { $link immutable } " to signal an error."
1844 $nl
1845 "The following two generic words are optional, as not all sequences are resizable:"
1846 { $subsections set-length lengthen }
1847 "An optional generic word for creating sequences of the same class as a given sequence:"
1848 { $subsections like }
1849 "Optional generic words for optimization purposes:"
1850 { $subsections new-sequence new-resizable }
1851 { $see-also "sequences-unsafe" } ;
1852
1853 ARTICLE: "virtual-sequences-protocol" "Virtual sequence protocol"
1854 "Virtual sequences must know their length:"
1855 { $subsections length }
1856 "An exemplar of the underlying storage:"
1857 { $subsections virtual-exemplar }
1858 "The index and the underlying storage where the value is located:"
1859 { $subsections virtual@ } ;
1860
1861 ARTICLE: "virtual-sequences" "Virtual sequences"
1862 "A virtual sequence is an implementation of the " { $link "sequence-protocol" } " which does not store its own elements, and instead computes them, either from scratch or by retrieving them from another sequence."
1863 $nl
1864 "Implementations include the following:"
1865 { $subsections reversed slice }
1866 "Virtual sequences can be implemented with the " { $link "virtual-sequences-protocol" } ", by translating an index in the virtual sequence into an index in another sequence."
1867 { $see-also "sequences-integers" } ;
1868
1869 ARTICLE: "sequences-integers" "Counted loops"
1870 "A virtual sequence is defined for iterating over integers from zero."
1871 { $subsection <iota> }
1872 "For example, calling " { $link <iota> } " on the integer 3 produces a sequence containing the elements 0, 1, and 2. This is very useful for performing counted loops using words such as " { $link each } ":"
1873 { $example "USING: sequences prettyprint ; 3 <iota> [ . ] each" "0\n1\n2" }
1874 "A common idiom is to iterate over a sequence, while also maintaining a loop counter. This can be done using " { $link each-index } ", " { $link map-index } " and " { $link reduce-index } "."
1875 $nl
1876 "Combinators that produce new sequences, such as " { $link map } ", will output an array if the input is an instance of " { $link <iota> } "."
1877 $nl
1878 "More elaborate counted loops can be performed with " { $link "ranges" } "." ;
1879
1880 ARTICLE: "sequences-if" "Control flow with sequences"
1881 "To reduce the boilerplate of checking if a sequence is empty, several combinators are provided."
1882 $nl
1883 "Checking if a sequence is empty:"
1884 { $subsections if-empty when-empty unless-empty } ;
1885
1886 ARTICLE: "sequences-access" "Accessing sequence elements"
1887 "Element access by index, without raising exceptions:"
1888 { $subsections ?nth }
1889 "Concise way of extracting one of the first four elements:"
1890 { $subsections first second third fourth ?first ?second }
1891 "Extracting the last element:"
1892 { $subsections last ?last }
1893 "Unpacking sequences:"
1894 { $subsections first2 first3 first4 }
1895 { $see-also nth } ;
1896
1897 ARTICLE: "sequences-add-remove" "Adding and removing sequence elements"
1898 "Adding elements:"
1899 { $subsections prefix suffix insert-nth }
1900 "Removing elements:"
1901 { $subsections remove remove-eq remove-nth } ;
1902
1903 ARTICLE: "sequences-reshape" "Reshaping sequences"
1904 "A " { $emphasis "repetition" } " is a virtual sequence consisting of a single element repeated multiple times:"
1905 { $subsections repetition <repetition> }
1906 "Reversing a sequence:"
1907 { $subsections reverse }
1908 "A " { $emphasis "reversed" } " is a virtual sequence presenting a reversed view of an underlying sequence:"
1909 { $subsections reversed <reversed> }
1910 "Transposing a matrix:"
1911 { $subsections flip } ;
1912
1913 ARTICLE: "sequences-appending" "Appending sequences"
1914 "Basic append operations:"
1915 { $subsections
1916     append
1917     append-as
1918     prepend
1919     3append
1920     3append-as
1921     surround
1922     surround-as
1923     glue
1924     glue-as
1925 }
1926 "Collapse a sequence unto itself:"
1927 { $subsections concat concat-as join join-as }
1928 "A pair of words useful for aligning strings:"
1929 { $subsections pad-head pad-tail } ;
1930
1931 ARTICLE: "sequences-slices" "Subsequences and slices"
1932 "There are two ways to extract a subrange of elements from a sequence. The first approach creates a new sequence of the same type as the input, which does not share storage with the underlying sequence. This takes time proportional to the number of elements being extracted. The second approach creates a " { $emphasis "slice" } ", which is a virtual sequence (see " { $link "virtual-sequences" } ") sharing storage with the original sequence. Slices are constructed in constant time."
1933 $nl
1934 "Some general guidelines for choosing between the two approaches:"
1935 { $list
1936   "If you are using mutable state, the choice has to be made one way or another because of semantics; mutating a slice will change the underlying sequence."
1937   { "Using a slice can improve algorithmic complexity. For example, if each iteration of a loop decomposes a sequence using " { $link first } " and " { $link rest } ", then the loop will run in quadratic time, relative to the length of the sequence. Using " { $link rest-slice } " changes the loop to run in linear time, since " { $link rest-slice } " does not copy any elements. Taking a slice of a slice will “collapse” the slice so to avoid the double indirection, so it is safe to use slices in recursive code." }
1938   "Accessing elements from a concrete sequence (such as a string or an array) is often faster than accessing elements from a slice, because slice access entails additional indirection. However, in some cases, if the slice is immediately consumed by an iteration combinator, the compiler can eliminate the slice allocation and indirection altogether."
1939   "If the slice outlives the original sequence, the original sequence will still remain in memory, since the slice will reference it. This can increase memory consumption unnecessarily."
1940 }
1941 { $heading "Subsequence operations" }
1942 "Extracting a subsequence:"
1943 { $subsections
1944     subseq
1945     subseq-as
1946     head
1947     tail
1948     head*
1949     tail*
1950 }
1951 "Removing the first or last element:"
1952 { $subsections rest but-last }
1953 "Taking a sequence apart into a head and a tail:"
1954 { $subsections
1955     unclip
1956     unclip-last
1957     cut
1958     cut*
1959 }
1960 { $heading "Slice operations" }
1961 "The slice data type:"
1962 { $subsections slice slice? }
1963 "Extracting a slice:"
1964 { $subsections
1965     <slice>
1966     head-slice
1967     tail-slice
1968     head-slice*
1969     tail-slice*
1970 }
1971 "Removing the first or last element:"
1972 { $subsections rest-slice but-last-slice }
1973 "Taking a sequence apart into a head and a tail:"
1974 { $subsections unclip-slice unclip-last-slice cut-slice }
1975 "Replacing slices with new elements:"
1976 { $subsections replace-slice } ;
1977
1978 ARTICLE: "sequences-combinators" "Sequence combinators"
1979 "Iteration:"
1980 { $subsections
1981     each
1982     each-index
1983     reduce
1984     interleave
1985 }
1986 "Mapping:"
1987 { $subsections
1988     map
1989     map-as
1990     map-index
1991     map-index-as
1992     map-reduce
1993     accumulate
1994     accumulate-as
1995     accumulate*
1996     accumulate*-as
1997 }
1998 "Filtering:"
1999 { $subsections
2000     filter
2001     filter-as
2002     partition
2003 }
2004 "Counting:"
2005 { $subsections
2006     count
2007 }
2008 "Superlatives with " { $link min } " and " { $link max } ":"
2009 { $subsections
2010     infimum
2011     infimum-by
2012     supremum
2013     supremum-by
2014     shorter
2015     longer
2016     shorter?
2017     longer?
2018     shortest
2019     longest
2020 }
2021 "Generating:"
2022 { $subsections
2023     replicate
2024     replicate-as
2025     produce
2026     produce-as
2027 }
2028 "Math:"
2029 { $subsections
2030     sum
2031     product
2032 }
2033 "Testing if a sequence contains elements satisfying a predicate:"
2034 { $subsections
2035     any?
2036     all?
2037 }
2038 { $heading "Related Articles" }
2039 { $subsections
2040     "sequence-2combinators"
2041     "sequence-3combinators"
2042 } ;
2043
2044 ARTICLE: "sequence-2combinators" "Pair-wise sequence combinators"
2045 "There is a set of combinators which traverse two sequences pairwise. If one sequence is shorter than the other, then only the prefix having the length of the minimum of the two is examined."
2046 { $subsections
2047     2each
2048     2reduce
2049     2map
2050     2map-as
2051     2map-reduce
2052     2all?
2053 } ;
2054
2055 ARTICLE: "sequence-3combinators" "Triple-wise sequence combinators"
2056 "There is a set of combinators which traverse three sequences triple-wise. If one sequence is shorter than the others, then only the prefix having the length of the minimum of the three is examined."
2057 { $subsections 3each 3map 3map-as } ;
2058
2059 ARTICLE: "sequences-tests" "Testing sequences"
2060 "Testing for an empty sequence:"
2061 { $subsections empty? }
2062 "Testing indices:"
2063 { $subsections bounds-check? }
2064 "Testing if a sequence contains an object:"
2065 { $subsections member? member-eq? }
2066 "Testing if a sequence contains a subsequence:"
2067 { $subsections head? tail? subseq? subseq-of? } ;
2068
2069 ARTICLE: "sequences-search" "Searching sequences"
2070 "Finding the index of an element:"
2071 { $subsections
2072     index
2073     index-from
2074     last-index
2075     last-index-from
2076 }
2077 "Finding the start of a subsequence:"
2078 { $subsections
2079     subseq-start
2080     subseq-start-from
2081     subseq-index
2082     subseq-index-from
2083     subseq-starts-at?
2084 }
2085 "Finding the index of an element satisfying a predicate:"
2086 { $subsections
2087     find
2088     find-from
2089     find-last
2090     find-last-from
2091     map-find
2092     map-find-last
2093 } ;
2094
2095 ARTICLE: "sequences-trimming" "Trimming sequences"
2096 "Trimming words:"
2097 { $subsections trim trim-head trim-tail }
2098 "Potentially more efficient trim:"
2099 { $subsections trim-slice trim-head-slice trim-tail-slice } ;
2100
2101 ARTICLE: "sequences-destructive-discussion" "When to use destructive operations"
2102 "Constructive (non-destructive) operations should be preferred where possible because code without side-effects is usually more reusable and easier to reason about. There are two main reasons to use destructive operations:"
2103 { $list
2104     "For the side-effect. Some code is simpler to express with destructive operations; constructive operations return new objects, and sometimes ``threading'' the objects through the program manually complicates stack shuffling."
2105     { "As an optimization. Some code written to use constructive operations suffers from worse performance. An example is a loop which adds an element to a sequence on each iteration. Either " { $link suffix } " or " { $link suffix! } " could be used; however, the former copies the entire sequence each time, which would cause the loop to run in quadratic time." }
2106 }
2107 "The second reason is much weaker than the first one. In particular, many combinators (see " { $link map } ", " { $link produce } " and " { $link "namespaces-make" } ") as well as more advanced data structures (such as " { $vocab-link "persistent.vectors" } ") alleviate the need for explicit use of side effects." ;
2108
2109 ARTICLE: "sequences-destructive" "Destructive sequence operations"
2110 "Many operations have destructive variants that side effect an input sequence, instead of creating a new sequence:"
2111 { $table
2112     { { $strong "Constructive" } { $strong "Destructive" } }
2113     { { $link suffix } { $link suffix! } }
2114     { { $link remove } { $link remove! } }
2115     { { $link remove-eq } { $link remove-eq! } }
2116     { { $link remove-nth } { $link remove-nth! } }
2117     { { $link reverse } { $link reverse! } }
2118     { { $link append } { $link append! } }
2119     { { $link map } { $link map! } }
2120     { { $link accumulate } { $link accumulate! } }
2121     { { $link accumulate* } { $link accumulate*! } }
2122     { { $link filter } { $link filter! } }
2123 }
2124 "Changing elements:"
2125 { $subsections map! accumulate! accumulate*! change-nth }
2126 "Deleting elements:"
2127 { $subsections
2128     remove!
2129     remove-eq!
2130     remove-nth!
2131     delete-slice
2132     delete-all
2133     filter!
2134 }
2135 "Adding elements:"
2136 { $subsections
2137     suffix!
2138     append!
2139 }
2140 "Other destructive words:"
2141 { $subsections
2142     reverse!
2143     move
2144     exchange
2145     copy
2146 }
2147 { $heading "Related Articles" }
2148 { $subsections
2149     "sequences-destructive-discussion"
2150     "sequences-stacks"
2151 }
2152 { $see-also set-nth push push-all pop pop* } ;
2153
2154 ARTICLE: "sequences-stacks" "Treating sequences as stacks"
2155 "The classical stack operations, modifying a sequence in place:"
2156 { $subsections push push-all pop pop* }
2157 { $see-also empty? } ;
2158
2159 ARTICLE: "sequences-comparing" "Comparing sequences"
2160 "Element equality testing:"
2161 { $subsections
2162     sequence=
2163     mismatch
2164     drop-prefix
2165     assert-sequence=
2166 }
2167 "The " { $link <=> } " generic word performs lexicographic comparison when applied to sequences." ;
2168
2169 ARTICLE: "sequences-f" "The f object as a sequence"
2170 "The " { $link f } " object supports the sequence protocol in a trivial way. It responds with a length of zero and throws an out of bounds error when an attempt is made to access elements." ;
2171
2172 ARTICLE: "sequences-combinator-implementation" "Implementing sequence combinators"
2173 "Creating a new sequence unconditionally:"
2174 { $subsections
2175     collector
2176     collector-as
2177 }
2178 "Creating a new sequence conditionally:"
2179 { $subsections
2180     selector
2181     selector-as
2182     2selector
2183 } ;
2184
2185 ARTICLE: "sequences-cartesian" "Cartesian product operations"
2186 "The cartesian product of two sequences is a sequence of all pairs where the first element of each pair is from the first sequence, and the second element of each pair is from the second sequence. The number of elements in the cartesian product is the product of the lengths of the two sequences."
2187 $nl
2188 "Combinators which pair every element of the first sequence with every element of the second:"
2189 { $subsections
2190     cartesian-each
2191     cartesian-map
2192     cartesian-find
2193 }
2194 "Computing the cartesian product of two sequences:"
2195 { $subsections
2196     cartesian-product
2197     cartesian-product-as
2198 } ;
2199
2200 ARTICLE: "sequences" "Sequence operations"
2201 "A " { $emphasis "sequence" } " is a finite, linearly-ordered collection of elements. Words for working with sequences are in the " { $vocab-link "sequences" } " vocabulary."
2202 $nl
2203 "Sequences implement a protocol:"
2204 { $subsections
2205     "sequence-protocol"
2206     "sequences-f"
2207 }
2208 "Sequence utility words can operate on any object whose class implements the sequence protocol. Most implementations are backed by storage. Some implementations obtain their elements from an underlying sequence, or compute them on the fly. These are known as " { $link "virtual-sequences" } "."
2209 { $subsections
2210     "sequences-access"
2211     "sequences-combinators"
2212     "sequences-add-remove"
2213     "sequences-appending"
2214     "sequences-slices"
2215     "sequences-reshape"
2216     "sequences-tests"
2217     "sequences-search"
2218     "sequences-comparing"
2219     "sequences-split"
2220     "grouping"
2221     "sequences-destructive"
2222     "sequences-stacks"
2223     "sequences-sorting"
2224     "binary-search"
2225     "sets"
2226     "sequences-trimming"
2227     "sequences-cartesian"
2228     "sequences.deep"
2229 }
2230 "Using sequences for looping:"
2231 { $subsections
2232     "sequences-integers"
2233     "ranges"
2234 }
2235 "Using sequences for control flow:"
2236 { $subsections "sequences-if" }
2237 "For inner loops:"
2238 { $subsections "sequences-unsafe" }
2239 "Implementing sequence combinators:"
2240 { $subsections "sequences-combinator-implementation" } ;
2241
2242 ABOUT: "sequences"