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