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