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