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