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