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