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