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