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