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