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