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