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