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