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