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