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