]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-docs.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / core / sequences / sequences-docs.factor
1 USING: arrays help.markup help.syntax math
2 sequences.private vectors strings kernel math.order layouts
3 quotations generic.single ;
4 IN: sequences
5
6 HELP: sequence
7 { $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:"
8     { $code "INSTANCE: my-sequence sequence" }
9 } ;
10
11 HELP: length
12 { $values { "seq" sequence } { "n" "a non-negative integer" } }
13 { $contract "Outputs the length of the sequence. All sequences support this operation." } ;
14
15 HELP: set-length
16 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
17 { $contract "Resizes a sequence. The initial contents of the new area is undefined." }
18 { $errors "Throws a " { $link no-method  } " error if the sequence is not resizable, and a " { $link bounds-error } " if the new length is negative." }
19 { $side-effects "seq" } ;
20
21 HELP: lengthen
22 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
23 { $contract "Ensures the sequence has a length of at least " { $snippet "n" } " elements. This word differs from " { $link set-length } " in two respects:"
24     { $list
25         { "This word does not shrink the sequence if " { $snippet "n" } " is less than its length." }
26         { "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." }
27     }
28 } ;
29
30 HELP: nth
31 { $values { "n" "a non-negative integer" } { "seq" sequence } { "elt" "the element at the " { $snippet "n" } "th index" } }
32 { $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." }
33 { $errors "Throws a " { $link bounds-error } " if the index is negative, or greater than or equal to the length of the sequence." } ;
34
35 HELP: set-nth
36 { $values { "elt" object } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
37 { $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." }
38 { $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."
39 $nl
40 "Throws an error if the sequence cannot hold elements of the given type." }
41 { $side-effects "seq" } ;
42
43 HELP: nths
44 { $values
45      { "indices" sequence } { "seq" sequence }
46      { "seq'" sequence } }
47 { $description "Outputs a sequence of elements from the input sequence indexed by the indices." }
48 { $examples 
49     { $example "USING: prettyprint sequences ;"
50                "{ 0 2 } { \"a\" \"b\" \"c\" } nths ."
51                "{ \"a\" \"c\" }"
52     }
53 } ;
54
55 HELP: immutable
56 { $values { "seq" sequence } }
57 { $description "Throws an " { $link immutable } " error." }
58 { $error-description "Thrown if an attempt is made to modify an immutable sequence." } ;
59
60 HELP: new-sequence
61 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a mutable sequence" } }
62 { $contract "Outputs a mutable sequence of length " { $snippet "n" } " which can hold the elements of " { $snippet "seq" } ". The initial contents of the sequence are undefined." } ;
63
64 HELP: new-resizable
65 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a resizable mutable sequence" } }
66 { $contract "Outputs a resizable mutable sequence with an initial capacity of " { $snippet "n" } " elements and zero length, which can hold the elements of " { $snippet "seq" } "." }
67 { $examples
68     { $example "USING: prettyprint sequences ;" "300 V{ } new-resizable ." "V{ }" }
69     { $example "USING: prettyprint sequences ;" "300 SBUF\" \" new-resizable ." "SBUF\" \"" }
70 } ;
71
72 HELP: like
73 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
74 { $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."
75 $nl
76 "The default implementation does nothing." }
77 { $notes "Unlike " { $link clone-like } ", the output sequence might share storage with the input sequence." } ;
78
79 HELP: empty?
80 { $values { "seq" sequence } { "?" "a boolean" } }
81 { $description "Tests if the sequence has zero length." } ;
82
83 HELP: if-empty
84 { $values { "seq" sequence } { "quot1" quotation } { "quot2" quotation } }
85 { $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." }
86 { $example
87     "USING: kernel prettyprint sequences ;"
88     "{ 1 2 3 } [ \"empty sequence\" ] [ sum ] if-empty ."
89     "6"
90 } ;
91
92 HELP: when-empty
93 { $values
94      { "seq" sequence } { "quot" "the first quotation of an " { $link if-empty } } }
95 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped and the " { $snippet "quot" } " is called." }
96 { $examples "This word is equivalent to " { $link if-empty } " with an empty second quotation:"
97     { $example
98     "USING: sequences prettyprint ;"
99     "{ } [ { 4 5 6 } ] [ ] if-empty ."
100     "{ 4 5 6 }"
101     }
102     { $example
103     "USING: sequences prettyprint ;"
104     "{ } [ { 4 5 6 } ] when-empty ."
105     "{ 4 5 6 }"
106     }
107 } ;
108
109 HELP: unless-empty
110 { $values
111      { "seq" sequence } { "quot" "the second quotation of an " { $link if-empty } } }
112 { $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped. Otherwise, the " { $snippet "quot" } " is called on the sequence." }
113 { $examples "This word is equivalent to " { $link if-empty } " with an empty first quotation:"
114     { $example
115     "USING: sequences prettyprint ;"
116     "{ 4 5 6 } [ ] [ sum ] if-empty ."
117     "15"
118     }
119     { $example
120     "USING: sequences prettyprint ;"
121     "{ 4 5 6 } [ sum ] unless-empty ."
122     "15"
123     }
124 } ;
125
126 HELP: delete-all
127 { $values { "seq" "a resizable sequence" } }
128 { $description "Resizes the sequence to zero length, removing all elements. Not all sequences are resizable." }
129 { $errors "Throws a " { $link bounds-error } " if the new length is negative, or if the sequence is not resizable." }
130 { $side-effects "seq" } ;
131
132 HELP: resize
133 { $values { "n" "a non-negative integer" } { "seq" sequence } { "newseq" "a new sequence" } }
134 { $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." }
135 { $notes "This generic word is only implemented for strings and arrays." } ;
136
137 HELP: first
138 { $values { "seq" sequence } { "first" "the first element of the sequence" } }
139 { $description "Outputs the first element of the sequence." }
140 { $errors "Throws an error if the sequence is empty." } ;
141
142 HELP: second
143 { $values { "seq" sequence } { "second" "the second element of the sequence" } }
144 { $description "Outputs the second element of the sequence." }
145 { $errors "Throws an error if the sequence contains less than two elements." } ;
146
147 HELP: third
148 { $values { "seq" sequence } { "third" "the third element of the sequence" } }
149 { $description "Outputs the third element of the sequence." }
150 { $errors "Throws an error if the sequence contains less than three elements." } ;
151
152 HELP: fourth
153 { $values { "seq" sequence } { "fourth" "the fourth element of the sequence" } }
154 { $description "Outputs the fourth element of the sequence." }
155 { $errors "Throws an error if the sequence contains less than four elements." } ;
156
157 HELP: push
158 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
159 { $description "Adds an element at the end of the sequence. The sequence length is adjusted accordingly." }
160 { $errors "Throws an error if " { $snippet "seq" } " is not resizable, or if the type of " { $snippet "elt" } " is not permitted in " { $snippet "seq" } "." }
161 { $side-effects "seq" } ;
162
163 HELP: bounds-check?
164 { $values { "n" "an integer" } { "seq" sequence } { "?" "a boolean" } }
165 { $description "Tests if the index is within the bounds of the sequence." } ;
166
167 HELP: bounds-error
168 { $values { "n" "a positive integer" } { "seq" sequence } }
169 { $description "Throws a " { $link bounds-error } "." }
170 { $error-description "Thrown by " { $link nth } ", " { $link set-nth } " and " { $link set-length } " if the given index lies beyond the bounds of the sequence." } ;
171
172 HELP: bounds-check
173 { $values { "n" "a positive integer" } { "seq" sequence } }
174 { $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." } ;
175
176 HELP: ?nth
177 { $values { "n" "an integer" } { "seq" sequence } { "elt/f" "an object or " { $link f } } }
178 { $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 } "." } ;
179
180 HELP: nth-unsafe
181 { $values { "n" "an integer" } { "seq" sequence } { "elt" object } }
182 { $contract "Unsafe variant of " { $link nth } " that does not perform bounds checks." } ;
183
184 HELP: set-nth-unsafe
185 { $values { "elt" object } { "n" "an integer" } { "seq" sequence } }
186 { $contract "Unsafe variant of " { $link set-nth } " that does not perform bounds checks." } ;
187
188 HELP: exchange-unsafe
189 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
190 { $description "Unsafe variant of " { $link exchange } " that does not perform bounds checks." } ;
191
192 HELP: first-unsafe
193 { $values { "seq" sequence } { "first" "the first element" } }
194 { $contract "Unsafe variant of " { $link first } " that does not perform bounds checks." } ;
195
196 HELP: first2-unsafe
197 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
198 { $contract "Unsafe variant of " { $link first2 } " that does not perform bounds checks." } ;
199
200 HELP: first3-unsafe
201 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
202 { $contract "Unsafe variant of " { $link first3 } " that does not perform bounds checks." } ;
203
204 HELP: first4-unsafe
205 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
206 { $contract "Unsafe variant of " { $link first4 } " that does not perform bounds checks." } ;
207
208 HELP: 1sequence
209 { $values { "obj" object } { "exemplar" sequence } { "seq" sequence } }
210 { $description "Creates a one-element sequence of the same type as " { $snippet "exemplar" } "." } ;
211
212 HELP: 2sequence
213 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "seq" sequence } }
214 { $description "Creates a two-element sequence of the same type as " { $snippet "exemplar" } "." } ;
215
216 HELP: 3sequence
217 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "exemplar" sequence } { "seq" sequence } }
218 { $description "Creates a three-element sequence of the same type as " { $snippet "exemplar" } "." } ;
219
220 HELP: 4sequence
221 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "obj4" object } { "exemplar" sequence } { "seq" sequence } }
222 { $description "Creates a four-element sequence of the same type as " { $snippet "exemplar" } "." } ;
223
224 HELP: first2
225 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
226 { $description "Pushes the first two elements of a sequence." }
227 { $errors "Throws an error if the sequence has less than two elements." } ;
228
229 HELP: first3
230 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
231 { $description "Pushes the first three elements of a sequence." }
232 { $errors "Throws an error if the sequence has less than three elements." } ;
233
234 HELP: first4
235 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
236 { $description "Pushes the first four elements of a sequence." }
237 { $errors "Throws an error if the sequence has less than four elements." } ;
238
239 HELP: array-capacity
240 { $values { "array" "an array" } { "n" "a non-negative fixnum" } }
241 { $class-description "A predicate class whose instances are valid array sizes for the current architecture. The minimum value is zero and the maximum value is " { $link max-array-capacity } "." }
242 { $description "Low-level array length accessor." }
243 { $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." } ;
244
245 HELP: array-nth
246 { $values { "n" "a non-negative fixnum" } { "array" "an array" }  { "elt" object } }
247 { $description "Low-level array element accessor." }
248 { $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." } ;
249
250 HELP: set-array-nth
251 { $values { "elt" object } { "n" "a non-negative fixnum" } { "array" "an array" }  }
252 { $description "Low-level array element mutator." }
253 { $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." } ;
254
255 HELP: collect
256 { $values { "n" "a non-negative integer" } { "quot" { $quotation "( n -- value )" } } { "into" "a sequence of length at least " { $snippet "n" } } }
257 { $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." } ;
258
259 HELP: each
260 { $values { "seq" sequence } { "quot" { $quotation "( elt -- )" } } }
261 { $description "Applies the quotation to each element of the sequence in order." } ;
262
263 HELP: reduce
264 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation "( prev elt -- next )" } } { "result" "the final result" } }
265 { $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." }
266 { $examples
267     { $example "USING: math prettyprint sequences ;" "{ 1 5 3 } 0 [ + ] reduce ." "9" }
268 } ;
269
270 HELP: reduce-index
271 { $values
272      { "seq" sequence } { "identity" object } { "quot" quotation } }
273 { $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." }
274 { $examples { $example "USING: sequences prettyprint math ;"
275     "{ 10 50 90 } 0 [ + + ] reduce-index ."
276     "153"
277 } } ;
278
279 HELP: accumulate-as
280 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation "( prev elt -- next )" } } { "exemplar" sequence } { "final" "the final result" } { "newseq" "a new sequence" } }
281 { $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."
282 $nl
283 "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."
284 $nl
285 "When given the empty sequence, outputs an empty sequence together with the " { $snippet "identity" } "." } ;
286
287 HELP: accumulate
288 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation "( prev elt -- next )" } } { "final" "the final result" } { "newseq" "a new array" } }
289 { $description "Combines successive elements of the sequence using a binary operation, and outputs an array of intermediate results, together with the final result."
290 $nl
291 "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."
292 $nl
293 "When given the empty sequence, outputs an empty sequence together with the " { $snippet "identity" } "." }
294 { $examples
295     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate . ." "{ 0 2 4 6 8 }\n10" }
296 } ;
297
298 HELP: accumulate!
299 { $values { "seq" sequence } { "identity" object } { "quot" { $quotation "( prev elt -- next )" } } { "final" "the final result" } { "seq" sequence } }
300 { $description "Combines successive elements of the sequence using a binary operation, and outputs the original sequence of intermediate results, together with the final result."
301 $nl
302 "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."
303 $nl
304 "When given the empty sequence, outputs an empty sequence together with the " { $snippet "identity" } "." }
305 { $examples
306     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate! . ." "{ 0 2 4 6 8 }\n10" }
307 } ;
308
309 HELP: map
310 { $values { "seq" sequence } { "quot" { $quotation "( old -- new )" } } { "newseq" "a new sequence" } }
311 { $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." } ;
312
313 HELP: map-as
314 { $values { "seq" sequence } { "quot" { $quotation "( old -- new )" } } { "exemplar" sequence } { "newseq" "a new sequence" } }
315 { $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" } "." }
316 { $examples
317     "The following example converts a string into an array of one-element strings:"
318     { $example "USING: prettyprint strings sequences ;" "\"Hello\" [ 1string ] { } map-as ." "{ \"H\" \"e\" \"l\" \"l\" \"o\" }" }
319     "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."
320 } ;
321
322 HELP: each-index
323 { $values
324      { "seq" sequence } { "quot" quotation } }
325 { $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." }
326 { $examples { $example "USING: sequences prettyprint math ;"
327 "{ 10 20 30 } [ + . ] each-index"
328 "10\n21\n32"
329 } } ;
330
331 HELP: map-index
332 { $values
333   { "seq" sequence } { "quot" quotation } { "newseq" sequence } }
334 { $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." }
335 { $examples { $example "USING: sequences prettyprint math ;"
336 "{ 10 20 30 } [ + ] map-index ."
337 "{ 10 21 32 }"
338 } } ;
339
340 HELP: change-nth
341 { $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" { $quotation "( elt -- newelt )" } } }
342 { $description "Applies the quotation to the " { $snippet "i" } "th element of the sequence, storing the result back into the sequence." }
343 { $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" } "." }
344 { $side-effects "seq" } ;
345
346 HELP: map!
347 { $values { "seq" "a mutable sequence" } { "quot" { $quotation "( old -- new )" } } { "seq" "a mutable sequence" } }
348 { $description "Applies the quotation to each element yielding a new element, storing the new elements back in the original sequence. Returns the original sequence." }
349 { $errors "Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
350 { $side-effects "seq" } ;
351
352 HELP: min-length
353 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
354 { $description "Outputs the minimum of the lengths of the two sequences." } ;
355
356 HELP: max-length
357 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
358 { $description "Outputs the maximum of the lengths of the two sequences." } ;
359
360 HELP: 2each
361 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- )" } } }
362 { $description "Applies the quotation to pairs of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } "." } ;
363
364 HELP: 3each
365 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation "( elt1 elt2 elt3 -- )" } } }
366 { $description "Applies the quotation to triples of elements from " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } "." } ;
367
368 HELP: 2reduce
369 { $values { "seq1" sequence }
370           { "seq2" sequence }
371           { "identity" object }
372           { "quot" { $quotation "( prev elt1 elt2 -- next )" } }
373           { "result" "the final result" } }
374 { $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" } "." } ;
375
376 HELP: 2map
377 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- new )" } } { "newseq" "a new sequence" } }
378 { $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" } "." } ;
379
380 HELP: 3map
381 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation "( elt1 elt2 elt3 -- new )" } } { "newseq" "a new sequence" } }
382 { $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" } "." } ;
383
384 HELP: 2map-as
385 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- new )" } } { "exemplar" sequence } { "newseq" "a new sequence" } }
386 { $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" } "." } ;
387
388 HELP: 3map-as
389 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "quot" { $quotation "( elt1 elt2 elt3 -- new )" } } { "exemplar" sequence } { "newseq" "a new sequence" } }
390 { $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" } "." } ;
391
392 HELP: 2all?
393 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation "( elt1 elt2 -- ? )" } } { "?" "a boolean" } }
394 { $description "Tests the predicate pairwise against elements of " { $snippet "seq1" } " and " { $snippet "seq2" } "." } ;
395
396 HELP: find
397 { $values { "seq" sequence }
398           { "quot" { $quotation "( elt -- ? )" } }
399           { "i" "the index of the first match, or " { $link f } }
400           { "elt" "the first matching element, or " { $link f } } }
401 { $description "A simpler variant of " { $link find-from } " where the starting index is 0." } ;
402
403 HELP: find-from
404 { $values { "n" "a starting index" }
405           { "seq" sequence }
406           { "quot" { $quotation "( elt -- ? )" } }
407           { "i" "the index of the first match, or " { $link f } }
408           { "elt" "the first matching element, or " { $link f } } }
409 { $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 f and " { $link f } " as the element." } ;
410
411 HELP: find-last
412 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "i" "the index of the first match, or f" } { "elt" "the first matching element, or " { $link f } } }
413 { $description "A simpler variant of " { $link find-last-from } " where the starting index is one less than the length of the sequence." } ;
414
415 HELP: find-last-from
416 { $values { "n" "a starting index" } { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "i" "the index of the first match, or f" } { "elt" "the first matching element, or " { $link f } } }
417 { $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 f and " { $link f } " as the element." } ;
418
419 HELP: map-find
420 { $values { "seq" sequence } { "quot" { $quotation "( elt -- result/f )" } } { "result" "the first non-false result of the quotation" } { "elt" "the first matching element, or " { $link f } } }
421 { $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." } ;
422
423 HELP: any?
424 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "?" "a boolean" } }
425 { $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 } "." } ;
426
427 HELP: all?
428 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "?" "a boolean" } }
429 { $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 } "." } ;
430
431 HELP: push-if
432 { $values { "elt" object } { "quot" { $quotation "( elt -- ? )" } } { "accum" "a resizable mutable sequence" } }
433 { $description "Adds the element at the end of the sequence if the quotation yields a true value." } 
434 { $notes "This word is a factor of " { $link filter } "." } ;
435
436 HELP: filter
437 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "subseq" "a new sequence" } }
438 { $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." } ;
439
440 HELP: filter-as
441 { $values { "seq" sequence } { "quot" { $quotation "( elt -- ? )" } } { "exemplar" sequence } { "subseq" "a new sequence" } }
442 { $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." } ;
443
444 HELP: filter!
445 { $values { "seq" "a resizable mutable sequence" } { "quot" { $quotation "( elt -- ? )" } } { "seq" "a resizable mutable sequence" } }
446 { $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a false value." }
447 { $side-effects "seq" } ;
448
449 HELP: interleave
450 { $values { "seq" sequence } { "between" "a quotation" } { "quot" { $quotation "( elt -- )" } } }
451 { $description "Applies " { $snippet "quot" } " to each element in turn, also invoking " { $snippet "between" } " in-between each pair of elements." }
452 { $example "USING: io sequences ;" "{ \"a\" \"b\" \"c\" } [ \"X\" write ] [ write ] interleave" "aXbXc" } ;
453
454 HELP: index
455 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
456 { $description "Outputs the index of the first element in the sequence equal to " { $snippet "obj" } ". If no element is found, outputs " { $link f } "." } ;
457
458 HELP: index-from
459 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
460 { $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 } "." } ;
461
462 HELP: last-index
463 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
464 { $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 } "." } ;
465
466 HELP: last-index-from
467 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
468 { $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 } "." } ;
469
470 HELP: member?
471 { $values { "elt" object } { "seq" sequence } { "?" "a boolean" } }
472 { $description "Tests if the sequence contains an element equal to the object." }
473 { $notes "This word uses equality comparison (" { $link = } ")." } ;
474
475 HELP: member-eq?
476 { $values { "elt" object } { "seq" sequence } { "?" "a boolean" } }
477 { $description "Tests if the sequence contains the object." }
478 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
479
480 HELP: remove
481 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
482 { $description "Outputs a new sequence containing all elements of the input sequence except for given element." }
483 { $notes "This word uses equality comparison (" { $link = } ")." } ;
484
485 HELP: remove-eq
486 { $values { "elt" object } { "seq" sequence } { "newseq" "a new sequence" } }
487 { $description "Outputs a new sequence containing all elements of the input sequence except those equal to the given element." }
488 { $notes "This word uses identity comparison (" { $link eq? } ")." } ;
489
490 HELP: remove-nth
491 { $values
492      { "n" integer } { "seq" sequence }
493      { "seq'" sequence } }
494 { $description "Creates a new sequence without the element at index " { $snippet "n" } "." }
495 { $examples "Notice that the original sequence is left intact:" { $example "USING: sequences prettyprint kernel ;"
496     "{ 1 2 3 } 1 over remove-nth . ."
497     "{ 1 3 }\n{ 1 2 3 }"
498 } } ;
499
500 HELP: move
501 { $values { "to" "an index in " { $snippet "seq" } } { "from" "an index in " { $snippet "seq" } } { "seq" "a mutable sequence" } }
502 { $description "Sets the element with index " { $snippet "m" } " to the element with index " { $snippet "n" } "." }
503 { $side-effects "seq" } ;
504
505 HELP: remove!
506 { $values { "elt" object } { "seq" "a resizable mutable sequence" } { "elt" object } }
507 { $description "Removes all elements equal to " { $snippet "elt" } " from " { $snippet "seq" } " and returns " { $snippet "seq" } "." }
508 { $notes "This word uses equality comparison (" { $link = } ")." }
509 { $side-effects "seq" } ;
510
511 HELP: remove-eq!
512 { $values { "elt" object } { "seq" "a resizable mutable sequence" } { "seq" "a resizable mutable sequence" } }
513 { $description "Outputs a new sequence containing all elements of the input sequence except the given element." }
514 { $notes "This word uses identity comparison (" { $link eq? } ")." }
515 { $side-effects "seq" } ;
516
517 HELP: remove-nth!
518 { $values { "n" "a non-negative integer" } { "seq" "a resizable mutable sequence" } { "seq" "a resizable mutable sequence" } }
519 { $description "Removes the " { $snippet "n" } "th element from the sequence, shifting all other elements down and reducing its length by one." }
520 { $side-effects "seq" } ;
521
522 HELP: delete-slice
523 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
524 { $description "Removes a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } "." }
525 { $side-effects "seq" } ;
526
527 HELP: replace-slice
528 { $values { "new" sequence } { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "seq'" sequence } }
529 { $description "Replaces a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } " with a new sequence." }
530 { $errors "Throws an error if " { $snippet "new" } " contains elements whose types are not permissible in " { $snippet "seq" } "." } ;
531
532 { push prefix suffix } related-words
533
534 HELP: suffix
535 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
536 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the end of " { $snippet "seq" } "." }
537 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
538 { $examples
539     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 4 suffix ." "{ 1 2 3 4 }" }
540 } ;
541
542 HELP: suffix!
543 { $values { "seq" sequence } { "elt" object } { "seq" sequence } }
544 { $description "Modifiers a sequence in-place by adding " { $snippet "elt" } " to the end of " { $snippet "seq" } ". Outputs " { $snippet "seq" } "." }
545 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq" } "." }
546 { $examples
547     { $example "USING: prettyprint sequences ;" "V{ 1 2 3 } 4 suffix! ." "V{ 1 2 3 4 }" }
548 } ;
549
550 HELP: append!
551 { $values { "seq1" sequence } { "seq2" sequence } { "seq1" sequence } }
552 { $description "Modifiers " { $snippet "seq1" } " in-place by adding the elements from " { $snippet "seq2" } " to the end and outputs " { $snippet "seq1" } "." }
553 { $examples
554     { $example "USING: prettyprint sequences ;" "V{ 1 2 3 } { 4 5 6 } append! ." "V{ 1 2 3 4 5 6 }" }
555 } ;
556
557 HELP: prefix
558 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
559 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the beginning of " { $snippet "seq" } "." }
560 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." } 
561 { $examples
562 { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 0 prefix ." "{ 0 1 2 3 }" }
563 } ;
564
565 HELP: sum-lengths
566 { $values { "seq" "a sequence of sequences" } { "n" integer } }
567 { $description "Outputs the sum of the lengths of all sequences in " { $snippet "seq" } "." } ;
568
569 HELP: concat
570 { $values { "seq" sequence } { "newseq" sequence } }
571 { $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" } "." }
572 { $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" } "." } ;
573
574 HELP: concat-as
575 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" sequence } }
576 { $description "Concatenates a sequence of sequences together into one sequence with the same type as " { $snippet "exemplar" } "." }
577 { $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" } "." } ;
578
579 HELP: join
580 { $values { "seq" sequence } { "glue" sequence } { "newseq" sequence } }
581 { $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" } "." }
582 { $notes "If the " { $snippet "glue" } " sequence is empty, this word calls " { $link concat-as } "." }
583 { $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" } "." } ;
584
585 { join concat concat-as } related-words
586
587 HELP: last
588 { $values { "seq" sequence } { "elt" object } }
589 { $description "Outputs the last element of a sequence." }
590 { $errors "Throws an error if the sequence is empty." } ;
591
592 { pop pop* } related-words
593
594 HELP: pop*
595 { $values { "seq" "a resizable mutable sequence" } }
596 { $description "Removes the last element and shortens the sequence." }
597 { $side-effects "seq" }
598 { $errors "Throws an error if the sequence is empty." } ;
599
600 HELP: pop
601 { $values { "seq" "a resizable mutable sequence" } { "elt" object } }
602 { $description "Outputs the last element after removing it and shortening the sequence." }
603 { $side-effects "seq" }
604 { $errors "Throws an error if the sequence is empty." } ;
605
606 HELP: mismatch
607 { $values { "seq1" sequence } { "seq2" sequence } { "i" "an index" } }
608 { $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." } ;
609
610 HELP: flip
611 { $values { "matrix" "a sequence of equal-length sequences" } { "newmatrix" "a sequence of equal-length sequences" } }
612 { $description "Transposes the matrix; that is, rows become columns and columns become rows." }
613 { $examples { $example "USING: prettyprint sequences ;" "{ { 1 2 3 } { 4 5 6 } } flip ." "{ { 1 4 } { 2 5 } { 3 6 } }" } } ;
614
615 HELP: exchange
616 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
617 { $description "Exchanges the " { $snippet "m" } "th and " { $snippet "n" } "th elements of " { $snippet "seq" } "." } ;
618
619 HELP: reverse!
620 { $values { "seq" "a mutable sequence" } }
621 { $description "Reverses a sequence in-place and outputs that sequence." }
622 { $side-effects "seq" } ;
623
624 HELP: padding
625 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "quot" { $quotation "( seq1 seq2 -- newseq )" } } { "newseq" "a new sequence" } }
626 { $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." } ;
627
628 HELP: pad-head
629 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
630 { $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" } "." }
631 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-head print ] each" "---ab\n-quux" } } ;
632
633 HELP: pad-tail
634 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
635 { $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" } "." }
636 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-tail print ] each" "ab---\nquux-" } } ;
637
638 HELP: sequence=
639 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
640 { $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." } ;
641
642 HELP: reversed
643 { $class-description "A virtual sequence which presents a reversed view of an underlying sequence. New instances can be created by calling " { $link <reversed> } "." } ;
644
645 HELP: reverse
646 { $values { "seq" sequence } { "newseq" "a new sequence" } }
647 { $description "Outputs a new sequence having the same elements as " { $snippet "seq" } " but in reverse order." } ;
648
649 { reverse <reversed> reverse! } related-words
650
651 HELP: <reversed>
652 { $values { "seq" sequence } { "reversed" "a new sequence" } }
653 { $description "Creates an instance of the " { $link reversed } " class." }
654 { $see-also "virtual-sequences" } ;
655
656 HELP: slice-error
657 { $values { "str" "a reason" } }
658 { $description "Throws a " { $link slice-error } "." }
659 { $error-description "Thrown by " { $link <slice> } " if one of the following invalid conditions holds:"
660     { $list
661         "The start index is negative"
662         "The end index is greater than the length of the sequence"
663         "The start index is greater than the end index"
664     }
665 } ;
666
667 HELP: slice
668 { $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."
669 $nl
670 "Slices are mutable if the underlying sequence is mutable, and mutating a slice changes the underlying sequence. However, slices cannot be resized after creation." } ;
671
672 HELP: check-slice
673 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } }
674 { $description "Ensures that " { $snippet "m" } " is less than or equal to " { $snippet "m" } ", and that both indices are within bounds for " { $snippet "seq" } "." }
675 { $errors "Throws a " { $link slice-error } " if the preconditions are not met." } ;
676
677 HELP: collapse-slice
678 { $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 } }
679 { $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." }
680 ;
681
682 HELP: <flat-slice>
683 { $values { "seq" sequence } { "slice" slice } }
684 { $description "Outputs a slice with the same elements as " { $snippet "seq" } ", and " { $snippet "from" } " equal to 0 and " { $snippet "to" } " equal to the length of " { $snippet "seq" } "." }
685 { $notes "Some words create slices then proceed to read the " { $snippet "to" } " and " { $snippet "from" } " slots of the slice. To behave predictably when they are themselves given a slice as input, they apply this word first to get a canonical slice." } ;
686
687 HELP: <slice>
688 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "slice" slice } }
689 { $description "Outputs a new virtual sequence sharing storage with the subrange of elements in " { $snippet "seq" } " with indices starting from and including " { $snippet "m" } ", and up to but not including " { $snippet "n" } "." }
690 { $errors "Throws an error if " { $snippet "m" } " or " { $snippet "n" } " is out of bounds." }
691 { $notes "Taking the slice of a slice outputs a slice of the underlying sequence of the original slice. Keep this in mind when writing code which depends on the values of " { $snippet "from" } " and " { $snippet "to" } " being equal to the inputs to this word. The " { $link <flat-slice> } " word might be helpful in such situations." } ;
692
693 { <slice> subseq } related-words
694
695 HELP: repetition
696 { $class-description "A virtual sequence consisting of " { $snippet "elt" } " repeated " { $snippet "len" } " times. Repetitions are created by calling " { $link <repetition> } "." } ;
697
698 HELP: <repetition>
699 { $values { "len" "a non-negative integer" } { "elt" object } { "repetition" repetition } }
700 { $description "Creates a new " { $link repetition } "." }
701 { $examples
702     { $example "USING: arrays prettyprint sequences ;" "10 \"X\" <repetition> >array ." "{ \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" }" }
703     { $example "USING: prettyprint sequences ;" "10 \"X\" <repetition> concat ." "\"XXXXXXXXXX\"" }
704 } ;
705 HELP: copy
706 { $values { "src" sequence } { "i" "an index in " { $snippet "dest" } } { "dst" "a mutable sequence" } }
707 { $description "Copies all elements of " { $snippet "src" } " to " { $snippet "dest" } ", with destination indices starting from " { $snippet "i" } ". Grows " { $snippet "to" } " first if necessary." }
708 { $side-effects "dest" }
709 { $errors "An error is thrown if " { $snippet "to" } " is not resizable, and not large enough to hold the copied elements." } ;
710
711 HELP: push-all
712 { $values { "src" sequence } { "dest" "a resizable mutable sequence" } }
713 { $description "Appends " { $snippet "src" } " to the end of " { $snippet "dest" } "." }
714 { $side-effects "dest" }
715 { $errors "Throws an error if " { $snippet "src" } " contains elements not permitted in " { $snippet "dest" } "." } ;
716
717 HELP: append
718 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
719 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
720 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
721 { $examples 
722     { $example "USING: prettyprint sequences ;"
723         "{ 1 2 } B{ 3 4 } append ."
724         "{ 1 2 3 4 }"
725     }
726     { $example "USING: prettyprint sequences strings ;"
727         "\"go\" \"ing\" append ."
728         "\"going\""
729     }
730 } ;
731
732 HELP: append-as
733 { $values { "seq1" sequence } { "seq2" sequence } { "exemplar" sequence } { "newseq" sequence } }
734 { $description "Outputs a new sequence of the same type as " { $snippet "exemplar" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
735 { $errors "Throws an error if " { $snippet "seq1" } " or " { $snippet "seq2" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." }
736 { $examples 
737     { $example "USING: prettyprint sequences ;"
738         "{ 1 2 } B{ 3 4 } B{ } append-as ."
739         "B{ 1 2 3 4 }"
740     }
741     { $example "USING: prettyprint sequences strings ;"
742         "\"go\" \"ing\" SBUF\" \" append-as ."
743         "SBUF\" going\""
744     }
745 } ;
746
747 { append append-as } related-words
748
749 HELP: prepend
750 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
751 { $description "Outputs a new sequence of the same type as " { $snippet "seq2" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." }
752 { $errors "Throws an error if " { $snippet "seq1" } " contains elements not permitted in sequences of the same class as " { $snippet "seq2" } "." }
753 { $examples { $example "USING: prettyprint sequences ;"
754         "{ 1 2 } B{ 3 4 } prepend ."
755         "B{ 3 4 1 2 }"
756     }
757     { $example "USING: prettyprint sequences strings ;"
758         "\"go\" \"car\" prepend ."
759         "\"cargo\""
760     }
761 } ;
762
763 HELP: 3append
764 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
765 { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn." }
766 { $errors "Throws an error if " { $snippet "seq2" } " or " { $snippet "seq3" } " contain elements not permitted in sequences of the same class as " { $snippet "seq1" } "." }
767 { $examples
768     { $example "USING: prettyprint sequences ;"
769         "\"a\" \"b\" \"c\" 3append ."
770         "\"abc\""
771     }
772 } ;
773
774 HELP: 3append-as
775 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "exemplar" sequence } { "newseq" sequence } }
776 { $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" } "." }
777 { $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" } "." }
778 { $examples
779     { $example "USING: prettyprint sequences ;"
780         "\"a\" \"b\" \"c\" SBUF\" \" 3append-as ."
781         "SBUF\" abc\""
782     }
783 } ;
784
785 { 3append 3append-as } related-words
786
787 HELP: surround
788 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
789 { $description "Outputs a new sequence with " { $snippet "seq1" } " inserted between " { $snippet "seq2" } " and " { $snippet "seq3" } "." }
790 { $examples
791     { $example "USING: sequences prettyprint ;"
792                "\"sssssh\" \"(\" \")\" surround ."
793                "\"(sssssh)\""
794     }
795 } ;
796
797 HELP: glue
798 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
799 { $description "Outputs a new sequence with " { $snippet "seq3" } " inserted between " { $snippet "seq1" } " and " { $snippet "seq2" } "." }
800 { $examples
801     { $example "USING: sequences prettyprint ;"
802                "\"a\" \"b\" \",\" glue ."
803                "\"a,b\""
804     }
805 } ;
806
807 HELP: subseq
808 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "subseq" "a new sequence" } }
809 { $description "Outputs a new sequence consisting of all elements starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } "." }
810 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
811
812 HELP: clone-like
813 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
814 { $description "Outputs a newly-allocated sequence with the same elements as " { $snippet "seq" } " but of the same type as " { $snippet "exemplar" } "." }
815 { $notes "Unlike " { $link like } ", this word always creates a new sequence which never shares storage with the original." } ;
816
817 HELP: head-slice
818 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
819 { $description "Outputs a virtual sequence sharing storage with the first " { $snippet "n" } " elements of the input sequence." }
820 { $errors "Throws an error if the index is out of bounds." } ;
821
822 HELP: tail-slice
823 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
824 { $description "Outputs a virtual sequence sharing storage with all elements from the " { $snippet "n" } "th index until the end of the input sequence." }
825 { $errors "Throws an error if the index is out of bounds." } ;
826
827 HELP: but-last-slice
828 { $values { "seq" sequence } { "slice" "a slice" } }
829 { $description "Outputs a virtual sequence sharing storage with all but the last element of the input sequence." }
830 { $errors "Throws an error on an empty sequence." } ;
831
832 HELP: rest-slice
833 { $values { "seq" sequence } { "slice" "a slice" } }
834 { $description "Outputs a virtual sequence sharing storage with all elements from the 1st index until the end of the input sequence." }
835 { $notes "Equivalent to " { $snippet "1 tail" } }
836 { $errors "Throws an error on an empty sequence." } ;
837
838 HELP: head-slice*
839 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
840 { $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." }
841 { $errors "Throws an error if the index is out of bounds." } ;
842
843 HELP: tail-slice*
844 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
845 { $description "Outputs a virtual sequence sharing storage with the last " { $snippet "n" } " elements of the input sequence." }
846 { $errors "Throws an error if the index is out of bounds." } ;
847
848 HELP: head
849 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
850 { $description "Outputs a new sequence consisting of the first " { $snippet "n" } " elements of the input sequence." }
851 { $errors "Throws an error if the index is out of bounds." } ;
852
853 HELP: tail
854 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
855 { $description "Outputs a new sequence consisting of the input sequence with the first n items removed." }
856 { $errors "Throws an error if the index is out of bounds." } ;
857
858 HELP: but-last
859 { $values { "seq" sequence } { "headseq" "a new sequence" } }
860 { $description "Outputs a new sequence consisting of the input sequence with the last item removed." }
861 { $errors "Throws an error on an empty sequence." } ;
862
863 HELP: rest
864 { $values { "seq" sequence } { "tailseq" "a new sequence" } }
865 { $description "Outputs a new sequence consisting of the input sequence with the first item removed." }
866 { $errors "Throws an error on an empty sequence." } ;
867
868 HELP: head*
869 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
870 { $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 outputs a sequence of the first " { $snippet "l-n" } " elements of the input sequence, where " { $snippet "l" } " is its length." }
871 { $errors "Throws an error if the index is out of bounds." } ;
872
873 HELP: tail*
874 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
875 { $description "Outputs a new sequence consisting of the last " { $snippet "n" } " elements of the input sequence." }
876 { $errors "Throws an error if the index is out of bounds." } ;
877
878 HELP: shorter?
879 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
880 { $description "Tets if the length of " { $snippet "seq1" } " is smaller than the length of " { $snippet "seq2" } "." } ;
881
882 HELP: head?
883 { $values { "seq" sequence } { "begin" sequence } { "?" "a boolean" } }
884 { $description "Tests if " { $snippet "seq" } " starts with " { $snippet "begin" } ". If " { $snippet "begin" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
885
886 HELP: tail?
887 { $values { "seq" sequence } { "end" sequence } { "?" "a boolean" } }
888 { $description "Tests if " { $snippet "seq" } " ends with " { $snippet "end" } ". If " { $snippet "end" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
889
890 { remove remove-nth remove-eq remove-eq! remove! remove-nth! } related-words
891
892 HELP: cut-slice
893 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before-slice" sequence } { "after-slice" "a slice" } }
894 { $description "Outputs a pair of sequences, where " { $snippet "before" } " consists of the first " { $snippet "n" } " elements of " { $snippet "seq" } " and has the same type, while " { $snippet "after" } " is a slice of the remaining elements." }
895 { $notes "Unlike " { $link cut } ", the run time of this word is proportional to the length of " { $snippet "before" } ", not " { $snippet "after" } ", so it is suitable for use in an iterative algorithm which cuts successive pieces off a sequence." } ;
896
897 HELP: cut
898 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
899 { $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" } "." }
900 { $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." } ;
901
902 HELP: cut*
903 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
904 { $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" } "." } ;
905
906 HELP: start*
907 { $values { "subseq" sequence } { "seq" sequence } { "n" "a start index" } { "i" "a start index" } }
908 { $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 } "." } ;
909
910 HELP: start
911 { $values { "subseq" sequence } { "seq" sequence } { "i" "a start index" } }
912 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", or " { $link f } " if no matching subsequence is found." } ;
913
914 HELP: subseq?
915 { $values { "subseq" sequence } { "seq" sequence } { "?" "a boolean" } }
916 { $description "Tests if " { $snippet "seq" } " contains the elements of " { $snippet "subseq" } " as a contiguous subsequence." } ;
917
918 HELP: drop-prefix
919 { $values { "seq1" sequence } { "seq2" sequence } { "slice1" "a slice" } { "slice2" "a slice" } }
920 { $description "Outputs a pair of virtual sequences with the common prefix of " { $snippet "seq1" } " and " { $snippet "seq2" } " removed." } ;
921
922 HELP: unclip
923 { $values { "seq" sequence } { "rest" sequence } { "first" object } }
924 { $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." }
925 { $examples
926     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip suffix ." "{ 2 3 1 }" }
927 } ;
928
929 HELP: unclip-slice
930 { $values { "seq" sequence } { "rest-slice" slice } { "first" object } }
931 { $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." }
932 { $examples { $example "USING: math.order prettyprint sequences ;" "{ 3 -1 -10 5 7 } unclip-slice [ min ] reduce ." "-10" } } ;
933
934 HELP: unclip-last
935 { $values { "seq" sequence } { "butlast" sequence } { "last" object } }
936 { $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." }
937 { $examples
938     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip-last prefix ." "{ 3 1 2 }" }
939 } ;
940
941 HELP: unclip-last-slice
942 { $values { "seq" sequence } { "butlast-slice" slice } { "last" object } }
943 { $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." } ;
944
945 HELP: sum
946 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
947 { $description "Outputs the sum of all elements of " { $snippet "seq" } ". Outputs zero given an empty sequence." } ;
948
949 HELP: product
950 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
951 { $description "Outputs the product of all elements of " { $snippet "seq" } ". Outputs one given an empty sequence." } ;
952
953 HELP: infimum
954 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
955 { $description "Outputs the least element of " { $snippet "seq" } "." }
956 { $errors "Throws an error if the sequence is empty." } ;
957
958 HELP: supremum
959 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
960 { $description "Outputs the greatest element of " { $snippet "seq" } "." }
961 { $errors "Throws an error if the sequence is empty." } ;
962
963 HELP: produce
964 { $values { "pred" { $quotation "( -- ? )" } } { "quot" { $quotation "( -- obj )" } } { "seq" "a sequence" } }
965 { $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." }
966 { $examples
967     "The following example divides a number by two until we reach zero, and accumulates intermediate results:"
968     { $example "USING: kernel math prettyprint sequences ;" "1337 [ dup 0 > ] [ 2/ dup ] produce nip ." "{ 668 334 167 83 41 20 10 5 2 1 0 }" }
969     "The following example collects random numbers as long as they are greater than 1:"
970     { $unchecked-example "USING: kernel prettyprint random sequences ;" "[ 10 random dup 1 > ] [ ] produce nip ." "{ 8 2 2 9 }" }
971 } ;
972
973 HELP: produce-as
974 { $values { "pred" { $quotation "( -- ? )" } } { "quot" { $quotation "( -- obj )" } } { "exemplar" sequence } { "seq" "a sequence" } }
975 { $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." }
976 { $examples "See " { $link produce } " for examples." } ;
977
978 HELP: map-sum
979 { $values { "seq" sequence } { "quot" quotation } { "n" number } }
980 { $description "Like map sum, but without creating an intermediate sequence." }
981 { $example
982     "USING: math math.ranges sequences prettyprint ;"
983     "100 [1,b] [ sq ] map-sum ."
984     "338350"
985 } ;
986
987 HELP: count
988 { $values { "seq" sequence } { "quot" quotation } { "n" integer } }
989 { $description "Efficiently returns the number of elements that the predicate quotation matches." }
990 { $example
991     "USING: math math.ranges sequences prettyprint ;"
992     "100 [1,b] [ even? ] count ."
993     "50"
994 } ;
995
996 HELP: pusher
997 { $values
998      { "quot" "a predicate quotation" }
999      { "quot" quotation } { "accum" vector } }
1000 { $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 accumulator if the test yields true. The accumulator is left on the stack for convenience." }
1001 { $example "! Find all the even numbers:" "USING: prettyprint sequences math kernel ;"
1002            "10 iota [ even? ] pusher [ each ] dip ."
1003            "V{ 0 2 4 6 8 }"
1004 }
1005 { $notes "Used to implement the " { $link filter } " word. Compare this word with " { $link accumulator } ", which is an unfiltering version." } ;
1006
1007 HELP: trim-head
1008 { $values
1009      { "seq" sequence } { "quot" quotation }
1010      { "newseq" sequence } }
1011 { $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." }
1012 { $example "" "USING: prettyprint math sequences ;"
1013            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head ."
1014            "{ 1 2 3 0 0 }"
1015 } ;
1016
1017 HELP: trim-head-slice
1018 { $values
1019      { "seq" sequence } { "quot" quotation }
1020      { "slice" slice } }
1021 { $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" }
1022 { $example "" "USING: prettyprint math sequences ;"
1023            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-head-slice ."
1024            "T{ slice { from 2 } { to 7 } { seq { 0 0 1 2 3 0 0 } } }"
1025 } ;
1026
1027 HELP: trim-tail
1028 { $values
1029      { "seq" sequence } { "quot" quotation }
1030      { "newseq" sequence } }
1031 { $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." }
1032 { $example "" "USING: prettyprint math sequences ;"
1033            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail ."
1034            "{ 0 0 1 2 3 }"
1035 } ;
1036
1037 HELP: trim-tail-slice
1038 { $values
1039      { "seq" sequence } { "quot" quotation }
1040      { "slice" slice } }
1041 { $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." }
1042 { $example "" "USING: prettyprint math sequences ;"
1043            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-tail-slice ."
1044            "T{ slice { from 0 } { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1045 } ;
1046
1047 HELP: trim
1048 { $values
1049      { "seq" sequence } { "quot" quotation }
1050      { "newseq" sequence } }
1051 { $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." }
1052 { $example "" "USING: prettyprint math sequences ;"
1053            "{ 0 0 1 2 3 0 0 } [ zero? ] trim ."
1054            "{ 1 2 3 }"
1055 } ;
1056
1057 HELP: trim-slice
1058 { $values
1059      { "seq" sequence } { "quot" quotation }
1060      { "slice" slice } }
1061 { $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." }
1062 { $example "" "USING: prettyprint math sequences ;"
1063            "{ 0 0 1 2 3 0 0 } [ zero? ] trim-slice ."
1064            "T{ slice { from 2 } { to 5 } { seq { 0 0 1 2 3 0 0 } } }"
1065 } ;
1066
1067 { trim trim-slice trim-head trim-head-slice trim-tail trim-tail-slice } related-words
1068
1069 HELP: sift
1070 { $values
1071      { "seq" sequence }
1072      { "newseq" sequence } }
1073  { $description "Outputs a new sequence with all instance of " { $link f  } " removed." }
1074  { $examples 
1075     { $example "USING: prettyprint sequences ;"
1076         "{ \"a\" 3 { } f } sift ."
1077         "{ \"a\" 3 { } }"
1078     }
1079 } ;
1080
1081 HELP: harvest
1082 { $values
1083      { "seq" sequence }
1084      { "newseq" sequence } }
1085 { $description "Outputs a new sequence with all empty sequences removed." }
1086 { $examples 
1087     { $example "USING: prettyprint sequences ;"
1088                "{ { } { 2 3 } { 5 } { } } harvest ."
1089                "{ { 2 3 } { 5 } }"
1090     }
1091 } ;
1092
1093 { filter filter! sift harvest } related-words
1094
1095 HELP: set-first
1096 { $values
1097      { "first" object } { "seq" sequence } }
1098 { $description "Sets the first element of a sequence." }
1099 { $examples 
1100     { $example "USING: prettyprint kernel sequences ;"
1101         "{ 1 2 3 4  } 5 over set-first ."
1102         "{ 5 2 3 4 }"
1103     }
1104 } ;
1105
1106 HELP: set-second
1107 { $values
1108      { "second" object } { "seq" sequence } }
1109 { $description "Sets the second element of a sequence." }
1110 { $examples 
1111     { $example "USING: prettyprint kernel sequences ;"
1112         "{ 1 2 3 4  } 5 over set-second ."
1113         "{ 1 5 3 4 }"
1114     }
1115 } ;
1116
1117 HELP: set-third
1118 { $values
1119      { "third" object } { "seq" sequence } }
1120 { $description "Sets the third element of a sequence." }
1121 { $examples 
1122     { $example "USING: prettyprint kernel sequences ;"
1123         "{ 1 2 3 4  } 5 over set-third ."
1124         "{ 1 2 5 4 }"
1125     }
1126 } ;
1127
1128 HELP: set-fourth
1129 { $values
1130      { "fourth" object } { "seq" sequence } }
1131 { $description "Sets the fourth element of a sequence." }
1132 { $examples 
1133     { $example "USING: prettyprint kernel sequences ;"
1134         "{ 1 2 3 4  } 5 over set-fourth ."
1135         "{ 1 2 3 5 }"
1136     }
1137 } ;
1138
1139 { set-first set-second set-third set-fourth } related-words
1140
1141 HELP: replicate
1142 { $values
1143      { "len" integer } { "quot" { $quotation "( -- elt )" } }
1144      { "newseq" sequence } }
1145      { $description "Calls the quotation " { $snippet "len" } " times, collecting results into a new array." }
1146 { $examples 
1147     { $unchecked-example "USING: kernel prettyprint random sequences ;"
1148         "5 [ 100 random ] replicate ."
1149         "{ 52 10 45 81 30 }"
1150     }
1151 } ;
1152
1153 HELP: replicate-as
1154 { $values
1155      { "len" integer } { "quot" quotation } { "exemplar" sequence }
1156      { "newseq" sequence } }
1157  { $description "Calls the quotation " { $snippet "len" } " times, collecting results into a new sequence of the same type as the exemplar sequence." }
1158 { $examples 
1159     { $unchecked-example "USING: prettyprint kernel sequences ;"
1160         "5 [ 100 random ] B{ } replicate-as ."
1161         "B{ 44 8 2 33 18 }"
1162     }
1163 } ;
1164
1165 { replicate replicate-as } related-words
1166
1167 HELP: partition
1168 { $values
1169      { "seq" sequence } { "quot" quotation }
1170      { "trueseq" sequence } { "falseseq" sequence } }
1171      { $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" } "." }
1172 { $examples 
1173     { $example "USING: prettyprint kernel math sequences ;"
1174         "{ 1 2 3 4 5 } [ even? ] partition [ . ] bi@"
1175         "{ 2 4 }\n{ 1 3 5 }"
1176     }
1177 } ;
1178
1179 HELP: virtual-exemplar
1180 { $values
1181      { "seq" sequence }
1182      { "seq'" sequence } }
1183 { $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 } "." } ;
1184
1185 HELP: virtual@
1186 { $values
1187      { "n" integer } { "seq" sequence }
1188      { "n'" integer } { "seq'" sequence } }
1189 { $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." } ;
1190
1191 HELP: 2map-reduce
1192 { $values
1193      { "seq1" sequence } { "seq2" sequence } { "map-quot" quotation } { "reduce-quot" quotation }
1194      { "result" object } }
1195  { $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." }
1196 { $errors "Throws an error if the sequence is empty." }
1197 { $examples { $example "USING: sequences prettyprint math ;"
1198     "{ 10 30 50 } { 200 400 600 } [ + ] [ + ] 2map-reduce ."
1199     "1290"
1200 } } ;
1201
1202 HELP: 2pusher
1203 { $values
1204      { "quot" quotation }
1205      { "quot" quotation } { "accum1" vector } { "accum2" vector } }
1206 { $description "Creates two new vectors to accumultate values based on a predicate. The first vector accumulates values for which the predicate yields true; the second for false." } ;
1207
1208 HELP: 2reverse-each
1209 { $values
1210      { "seq1" sequence } { "seq2" sequence } { "quot" quotation } }
1211 { $description "Reverse the sequences using the " { $link <reversed> } " word and calls " { $link 2each } " on the reversed sequences." }
1212 { $examples { $example "USING: sequences math prettyprint ;"
1213     "{ 10 20 30 } { 1 2 3 } [ + . ] 2reverse-each"
1214     "33\n22\n11"
1215 } } ;
1216
1217 HELP: 2unclip-slice
1218 { $values
1219      { "seq1" sequence } { "seq2" sequence }
1220      { "rest-slice1" sequence } { "rest-slice2" sequence } { "first1" object } { "first2" object } }
1221 { $description "Unclips the first element of each sequence and leaves two slice elements and the two unclipped objects on the stack." }
1222 { $examples { $example "USING: sequences prettyprint kernel arrays ;"
1223     "{ 1 2 } { 3 4 } 2unclip-slice 4array [ . ] each"
1224     "T{ slice { from 1 } { to 2 } { seq { 1 2 } } }\nT{ slice { from 1 } { to 2 } { seq { 3 4 } } }\n1\n3"
1225 } } ;
1226
1227 HELP: accumulator
1228 { $values
1229      { "quot" quotation }
1230      { "quot'" quotation } { "vec" vector } }
1231 { $description "Creates a new quotation that pushes its result to a vector and outputs that vector on the stack." }
1232 { $examples { $example "USING: sequences prettyprint kernel math ;"
1233     "{ 1 2 } [ 30 + ] accumulator [ each ] dip ."
1234     "V{ 31 32 }"
1235 } } ;
1236
1237 HELP: binary-reduce
1238 { $values
1239      { "seq" sequence } { "start" integer } { "quot" quotation }
1240      { "value" object } }
1241 { $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 } "." }
1242 { $examples "Computing factorial:"
1243     { $example "USING: prettyprint sequences math ;"
1244     "40 iota rest-slice 1 [ * ] binary-reduce ."
1245     "20397882081197443358640281739902897356800000000" }
1246 } ;
1247
1248 HELP: follow
1249 { $values
1250      { "obj" object } { "quot" quotation }
1251      { "seq" sequence } }
1252 { $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 recursuively. Objects yielded by the quotation are added to the output sequence until the quotation yields " { $link f } ", at which point the recursion terminates." }
1253 { $examples "Get random numbers until zero is reached:"
1254     { $unchecked-example
1255     "USING: random sequences prettyprint math ;"
1256     "100 [ random [ f ] when-zero ] follow ."
1257     "{ 100 86 34 32 24 11 7 2 }"
1258 } } ;
1259
1260 HELP: halves
1261 { $values
1262      { "seq" sequence }
1263      { "first-slice" slice } { "second-slice" slice } }
1264 { $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." }
1265 { $examples { $example "USING: arrays sequences prettyprint kernel ;"
1266     "{ 1 2 3 4 5 } halves [ >array . ] bi@"
1267     "{ 1 2 }\n{ 3 4 5 }"
1268 } } ;
1269
1270 HELP: indices
1271 { $values
1272      { "obj" object } { "seq" sequence }
1273      { "indices" sequence } }
1274 { $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." }
1275 { $examples { $example "USING: sequences prettyprint ;"
1276     "2 { 2 4 2 6 2 8 2 10 } indices ."
1277     "V{ 0 2 4 6 }"
1278 } } ;
1279
1280 HELP: insert-nth
1281 { $values
1282      { "elt" object } { "n" integer } { "seq" sequence }
1283      { "seq'" sequence } }
1284 { $description "Creates a new sequence where the " { $snippet "n" } "th index is set to the input object." }
1285 { $examples { $example "USING: prettyprint sequences ;"
1286     "40 3 { 10 20 30 50 } insert-nth ."
1287     "{ 10 20 30 40 50 }"
1288 } } ;
1289
1290 HELP: map-reduce
1291 { $values
1292      { "seq" sequence } { "map-quot" quotation } { "reduce-quot" quotation }
1293      { "result" object } }
1294 { $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." }
1295 { $errors "Throws an error if the sequence is empty." }
1296 { $examples { $example "USING: sequences prettyprint math ;"
1297     "{ 1 3 5 } [ sq ] [ + ] map-reduce ."
1298     "35"
1299 } } ;
1300
1301 HELP: new-like
1302 { $values
1303      { "len" integer } { "exemplar" "an exemplar sequence" } { "quot" quotation }
1304      { "seq" sequence } }
1305 { $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." } ;
1306
1307 HELP: push-either
1308 { $values
1309      { "elt" object } { "quot" quotation } { "accum1" vector } { "accum2" vector } }
1310 { $description "Pushes the input object onto one of the accumualators; the first if the quotation yields true, the second if false." } ;
1311
1312 HELP: sequence-hashcode
1313 { $values
1314      { "n" integer } { "seq" sequence }
1315      { "x" integer } }
1316 { $description "Iterates over a sequence, computes a hashcode with " { $link hashcode* } " for each element, and combines them using " { $link sequence-hashcode-step } "." } ;
1317
1318 HELP: sequence-hashcode-step
1319 { $values
1320      { "oldhash" integer } { "newpart" integer }
1321      { "newhash" integer } }
1322 { $description "An implementation word that computes a running hashcode of a sequence using some bit-twiddling. The resulting hashcode is always a fixnum." } ;
1323
1324 HELP: short
1325 { $values
1326      { "seq" sequence } { "n" integer }
1327      { "seq" sequence } { "n'" integer } }
1328 { $description "Returns the input sequence and its length or " { $snippet "n" } ", whichever is less." }
1329 { $examples { $example "USING: sequences kernel prettyprint ;"
1330     "\"abcd\" 3 short [ . ] bi@"
1331     "\"abcd\"\n3"
1332 } } ;
1333
1334 HELP: shorten
1335 { $values
1336      { "n" integer } { "seq" sequence } }
1337 { $description "Shortens a " { $link "growable" } " sequence to by " { $snippet "n" } " elements long." }
1338 { $examples { $example "USING: sequences prettyprint kernel ;"
1339     "V{ 1 2 3 4 5 } 3 over shorten ."
1340     "V{ 1 2 3 }"
1341 } } ;
1342
1343 HELP: iota
1344 { $values { "n" integer } { "iota" iota } }
1345 { $description "Creates an immutable virtual sequence containing the integers from 0 to " { $snippet "n-1" } "." }
1346 { $examples
1347   { $example
1348     "USING: math sequences prettyprint ;"
1349     "3 iota [ sq ] map ."
1350     "{ 0 1 4 }"
1351   }
1352 } ;
1353
1354 HELP: assert-sequence=
1355 { $values
1356     { "a" sequence } { "b" sequence }
1357 }
1358 { $description "Throws an error if all the elements of two sequences, taken pairwise, are not equal." }
1359 { $notes "The sequences need not be of the same type." }
1360 { $examples
1361   { $example
1362     "USING: prettyprint sequences ;"
1363     "{ 1 2 3 } V{ 1 2 3 } assert-sequence="
1364     ""
1365   }
1366 } ;
1367
1368 ARTICLE: "sequences-unsafe" "Unsafe sequence operations"
1369 "The " { $link nth-unsafe } " and " { $link set-nth-unsafe } " sequence protocol bypasses bounds checks for increased performance."
1370 $nl
1371 "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."
1372 $nl
1373 "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."
1374 $nl
1375 "The justification for this is that the VM should not crash if a resizable sequence is resized during the execution of an iteration combinator."
1376 $nl
1377 "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 } "." ;
1378
1379 ARTICLE: "sequence-protocol" "Sequence protocol"
1380 "All sequences must be instances of a mixin class:"
1381 { $subsections sequence sequence? }
1382 "All sequences must know their length:"
1383 { $subsections length }
1384 "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:"
1385 { $subsections nth nth-unsafe }
1386 "Note that sequences are always indexed starting from zero."
1387 $nl
1388 "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:"
1389 { $subsections set-nth set-nth-unsafe }
1390 "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."
1391 $nl
1392 "The following two generic words are optional, as not all sequences are resizable:"
1393 { $subsections set-length lengthen }
1394 "An optional generic word for creating sequences of the same class as a given sequence:"
1395 { $subsections like }
1396 "Optional generic words for optimization purposes:"
1397 { $subsections new-sequence new-resizable }
1398 { $see-also "sequences-unsafe" } ;
1399
1400 ARTICLE: "virtual-sequences-protocol" "Virtual sequence protocol"
1401 "Virtual sequences must know their length:"
1402 { $subsections length }
1403 "An exemplar of the underlying storage:"
1404 { $subsections virtual-exemplar }
1405 "The index and the underlying storage where the value is located:"
1406 { $subsections virtual@ } ;
1407
1408 ARTICLE: "virtual-sequences" "Virtual sequences"
1409 "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."
1410 $nl
1411 "Implementations include the following:"
1412 { $subsections reversed slice }
1413 "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."
1414 { $see-also "sequences-integers" } ;
1415
1416 ARTICLE: "sequences-integers" "Counted loops"
1417 "A virtual sequence is defined for iterating over integers from zero."
1418 { $subsection iota }
1419 "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."
1420 $nl
1421 "This means the " { $link each } " combinator, given an integer, simply calls a quotation that number of times, pushing a counter on each iteration that ranges from 0 up to that integer:"
1422 { $example "3 iota [ . ] each" "0\n1\n2" }
1423 "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 } "."
1424 $nl
1425 "Combinators that produce new sequences, such as " { $link map } ", will output an array if the input is an instance of " { $link iota } "."
1426 $nl
1427 "More elaborate counted loops can be performed with " { $link "math.ranges" } "." ;
1428
1429 ARTICLE: "sequences-if" "Control flow with sequences"
1430 "To reduce the boilerplate of checking if a sequence is empty, several combinators are provided."
1431 $nl
1432 "Checking if a sequence is empty:"
1433 { $subsections if-empty when-empty unless-empty } ;
1434
1435 ARTICLE: "sequences-access" "Accessing sequence elements"
1436 "Element access by index, without raising exceptions:"
1437 { $subsections ?nth }
1438 "Concise way of extracting one of the first four elements:"
1439 { $subsections first second third fourth }
1440 "Extracting the last element:"
1441 { $subsections last }
1442 "Unpacking sequences:"
1443 { $subsections first2 first3 first4 }
1444 { $see-also nth } ;
1445
1446 ARTICLE: "sequences-add-remove" "Adding and removing sequence elements"
1447 "Adding elements:"
1448 { $subsections prefix suffix insert-nth }
1449 "Removing elements:"
1450 { $subsections remove remove-eq remove-nth } ;
1451
1452 ARTICLE: "sequences-reshape" "Reshaping sequences"
1453 "A " { $emphasis "repetition" } " is a virtual sequence consisting of a single element repeated multiple times:"
1454 { $subsections repetition <repetition> }
1455 "Reversing a sequence:"
1456 { $subsections reverse }
1457 "A " { $emphasis "reversal" } " presents a reversed view of an underlying sequence:"
1458 { $subsections reversed <reversed> }
1459 "Transposing a matrix:"
1460 { $subsections flip } ;
1461
1462 ARTICLE: "sequences-appending" "Appending sequences"
1463 "Basic append operations:"
1464 { $subsections
1465     append
1466     append-as
1467     prepend
1468     3append
1469     3append-as
1470     surround
1471     glue
1472 }
1473 "Collapse a sequence unto itself:"
1474 { $subsections concat join }
1475 "A pair of words useful for aligning strings:"
1476 { $subsections pad-head pad-tail } ;
1477
1478 ARTICLE: "sequences-slices" "Subsequences and slices"
1479 "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."
1480 $nl
1481 "Some general guidelines for choosing between the two approaches:"
1482 { $list
1483   "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."
1484   { "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." }
1485   "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 indirect altogether."
1486   "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."
1487 }
1488 { $heading "Subsequence operations" }
1489 "Extracting a subsequence:"
1490 { $subsections
1491     subseq
1492     head
1493     tail
1494     head*
1495     tail*
1496 }
1497 "Removing the first or last element:"
1498 { $subsections rest but-last }
1499 "Taking a sequence apart into a head and a tail:"
1500 { $subsections
1501     unclip
1502     unclip-last
1503     cut
1504     cut*
1505 }
1506 { $heading "Slice operations" }
1507 "The slice data type:"
1508 { $subsections slice slice? }
1509 "Extracting a slice:"
1510 { $subsections
1511     <slice>
1512     head-slice
1513     tail-slice
1514     head-slice*
1515     tail-slice*
1516 }
1517 "Removing the first or last element:"
1518 { $subsections rest-slice but-last-slice }
1519 "Taking a sequence apart into a head and a tail:"
1520 { $subsections unclip-slice unclip-last-slice cut-slice }
1521 "A utility for words which use slices as iterators:"
1522 { $subsections <flat-slice> }
1523 "Replacing slices with new elements:"
1524 { $subsections replace-slice } ;
1525
1526 ARTICLE: "sequences-combinators" "Sequence combinators"
1527 "Iteration:"
1528 { $subsections
1529     each
1530     each-index
1531     reduce
1532     interleave
1533     replicate
1534     replicate-as
1535 }
1536 "Mapping:"
1537 { $subsections
1538     map
1539     map-as
1540     map-index
1541     map-reduce
1542     accumulate
1543     accumulate-as
1544     accumulate!
1545     produce
1546     produce-as
1547 }
1548 "Filtering:"
1549 { $subsections
1550     filter
1551     filter-as
1552     partition
1553 }
1554 "Testing if a sequence contains elements satisfying a predicate:"
1555 { $subsections
1556     any?
1557     all?
1558 }
1559 { $heading "Related Articles" }
1560 { $subsections
1561     "sequence-2combinators"
1562     "sequence-3combinators"
1563 } ;
1564
1565 ARTICLE: "sequence-2combinators" "Pair-wise sequence combinators"
1566 "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."
1567 { $subsections
1568     2each
1569     2reduce
1570     2map
1571     2map-as
1572     2map-reduce
1573     2all?
1574 } ;
1575
1576 ARTICLE: "sequence-3combinators" "Triple-wise sequence combinators"
1577 "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."
1578 { $subsections 3each 3map 3map-as } ;
1579
1580 ARTICLE: "sequences-tests" "Testing sequences"
1581 "Testing for an empty sequence:"
1582 { $subsections empty? }
1583 "Testing indices:"
1584 { $subsections bounds-check? }
1585 "Testing if a sequence contains an object:"
1586 { $subsections member? member-eq? }
1587 "Testing if a sequence contains a subsequence:"
1588 { $subsections head? tail? subseq? } ;
1589
1590 ARTICLE: "sequences-search" "Searching sequences"
1591 "Finding the index of an element:"
1592 { $subsections
1593     index
1594     index-from
1595     last-index
1596     last-index-from
1597 }
1598 "Finding the start of a subsequence:"
1599 { $subsections start start* }
1600 "Finding the index of an element satisfying a predicate:"
1601 { $subsections
1602     find
1603     find-from
1604     find-last
1605     find-last-from
1606     map-find
1607 } ;
1608
1609 ARTICLE: "sequences-trimming" "Trimming sequences"
1610 "Trimming words:"
1611 { $subsections trim trim-head trim-tail }
1612 "Potentially more efficient trim:"
1613 { $subsections trim-slice trim-head-slice trim-tail-slice } ;
1614
1615 ARTICLE: "sequences-destructive-discussion" "When to use destructive operations"
1616 "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:"
1617 { $list
1618     "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."
1619     { "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." }
1620 }
1621 "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." ;
1622
1623 ARTICLE: "sequences-destructive" "Destructive operations"
1624 "Many operations have constructive and destructive variants:"
1625 { $table
1626     { "Constructive" "Destructive" }
1627     { { $link suffix } { $link suffix! } }
1628     { { $link remove } { $link remove! } }
1629     { { $link remove-eq } { $link remove-eq! } }
1630     { { $link remove-nth } { $link remove-nth! } }
1631     { { $link reverse } { $link reverse! } }
1632     { { $link append } { $link append! } }
1633     { { $link map } { $link map! } }
1634     { { $link filter } { $link filter! } }
1635 }
1636 "Changing elements:"
1637 { $subsections map! change-nth }
1638 "Deleting elements:"
1639 { $subsections
1640     remove!
1641     remove-eq!
1642     remove-nth!
1643     delete-slice
1644     delete-all
1645     filter!
1646 }
1647 "Other destructive words:"
1648 { $subsections
1649     reverse!
1650     append!
1651     move
1652     exchange
1653     copy
1654 }
1655 { $heading "Related Articles" }
1656 { $subsections
1657     "sequences-destructive-discussion"
1658     "sequences-stacks"
1659 }
1660 { $see-also set-nth push push-all pop pop* } ;
1661
1662 ARTICLE: "sequences-stacks" "Treating sequences as stacks"
1663 "The classical stack operations, modifying a sequence in place:"
1664 { $subsections push push-all pop pop* }
1665 { $see-also empty? } ;
1666
1667 ARTICLE: "sequences-comparing" "Comparing sequences"
1668 "Element equality testing:"
1669 { $subsections
1670     sequence=
1671     mismatch
1672     drop-prefix
1673     assert-sequence=
1674 }
1675 "The " { $link <=> } " generic word performs lexicographic comparison when applied to sequences." ;
1676
1677 ARTICLE: "sequences-f" "The f object as a sequence"
1678 "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." ;
1679
1680 ARTICLE: "sequences-combinator-implementation" "Implementing sequence combinators"
1681 "Creating a new sequence unconditionally:"
1682 { $subsections
1683     accumulator
1684     accumulator-for
1685 }
1686 "Creating a new sequence conditionally:"
1687 { $subsections
1688     pusher
1689     pusher-for
1690     2pusher
1691 } ;
1692
1693 ARTICLE: "sequences" "Sequence operations"
1694 "A " { $emphasis "sequence" } " is a finite, linearly-ordered collection of elements. Words for working with sequences are in the " { $vocab-link "sequences" } " vocabulary."
1695 $nl
1696 "Sequences implement a protocol:"
1697 { $subsections
1698     "sequence-protocol"
1699     "sequences-f"
1700 }
1701 "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" } "."
1702 { $subsections
1703     "sequences-access"
1704     "sequences-combinators"
1705     "sequences-add-remove"
1706     "sequences-appending"
1707     "sequences-slices"
1708     "sequences-reshape"
1709     "sequences-tests"
1710     "sequences-search"
1711     "sequences-comparing"
1712     "sequences-split"
1713     "grouping"
1714     "sequences-destructive"
1715     "sequences-stacks"
1716     "sequences-sorting"
1717     "binary-search"
1718     "sets"
1719     "sequences-trimming"
1720     "sequences.deep"
1721 }
1722 "Using sequences for looping:"
1723 { $subsections
1724     "sequences-integers"
1725     "math.ranges"
1726 }
1727 "Using sequences for control flow:"
1728 { $subsections "sequences-if" }
1729 "For inner loops:"
1730 { $subsections "sequences-unsafe" }
1731 "Implemeting sequence combinators:"
1732 { $subsections "sequences-combinator-implementation" } ;
1733
1734 ABOUT: "sequences"