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