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