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