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