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