]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-docs.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / core / sequences / sequences-docs.factor
1 USING: arrays bit-arrays help.markup help.syntax math
2 sequences.private vectors strings sbufs kernel math.order ;
3 IN: sequences
4
5 ARTICLE: "sequences-unsafe" "Unsafe sequence operations"
6 "The " { $link nth-unsafe } " and " { $link set-nth-unsafe } " sequence protocol bypasses bounds checks for increased performance."
7 $nl
8 "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."
9 $nl
10 "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."
11 $nl
12 "The justification for this is that the VM should not crash if a resizable sequence is resized during the execution of an iteration combinator."
13 $nl
14 "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 } "." ;
15
16 ARTICLE: "sequence-protocol" "Sequence protocol"
17 "All sequences must be instances of a mixin class:"
18 { $subsection sequence }
19 { $subsection sequence? }
20 "All sequences must know their length:"
21 { $subsection length }
22 "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:"
23 { $subsection nth }
24 { $subsection nth-unsafe }
25 "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:"
26 { $subsection set-nth }
27 { $subsection set-nth-unsafe }
28 "Note that even if the sequence is immutable, at least one of the generic words must be specialized, otherwise calling them will result in an infinite recursion. There is a standard word which throws an error indicating a sequence is immutable:"
29 { $subsection immutable }
30 "The following two generic words are optional, as not all sequences are resizable:"
31 { $subsection set-length }
32 { $subsection lengthen }
33 "An optional generic word for creating sequences of the same class as a given sequence:"
34 { $subsection like }
35 "Optional generic words for optimization purposes:"
36 { $subsection new-sequence }
37 { $subsection new-resizable }
38 { $see-also "sequences-unsafe" } ;
39
40 ARTICLE: "sequences-integers" "Integer sequences and counted loops"
41 "Integers support the sequence protocol in a trivial fashion; a non-negative integer presents its non-negative predecessors as elements. For example, the integer 3, when viewed as a sequence, contains the elements 0, 1, and 2. This is very useful for performing counted loops."
42 $nl
43 "For example, the " { $link each } " combinator, given an integer, simply calls a quotation that number of times, pushing a counter on each iteration that ranges from 0 up to that integer:"
44 { $example "3 [ . ] each" "0\n1\n2" }
45 "A common idiom is to iterate over a sequence, while also maintaining a loop counter. This can be done using " { $link 2each } ":"
46 { $example "{ \"a\" \"b\" \"c\" } dup length [\n    \"Index: \" write . \"Element: \" write .\n] 2each" "Index: 0\nElement: \"a\"\nIndex: 1\nElement: \"b\"\nIndex: 2\nElement: \"c\"" }
47 "Combinators that produce new sequences, such as " { $link map } ", will output an array if the input is an integer." ;
48
49 ARTICLE: "sequences-access" "Accessing sequence elements"
50 { $subsection ?nth }
51 "Concise way of extracting one of the first four elements:"
52 { $subsection first }
53 { $subsection second }
54 { $subsection third }
55 { $subsection fourth }
56 "Unpacking sequences:"
57 { $subsection first2 }
58 { $subsection first3 }
59 { $subsection first4 }
60 { $see-also nth peek } ;
61
62 ARTICLE: "sequences-add-remove" "Adding and removing sequence elements"
63 "Adding elements:"
64 { $subsection prefix }
65 { $subsection suffix }
66 "Removing elements:"
67 { $subsection remove } ;
68
69 ARTICLE: "sequences-reshape" "Reshaping sequences"
70 "A " { $emphasis "repetition" } " is a virtual sequence consisting of a single element repeated multiple times:"
71 { $subsection repetition }
72 { $subsection <repetition> }
73 "Reversing a sequence:"
74 { $subsection reverse }
75 "A " { $emphasis "reversal" } " presents a reversed view of an underlying sequence:"
76 { $subsection reversed }
77 { $subsection <reversed> }
78 "Transposing a matrix:"
79 { $subsection flip } ;
80
81 ARTICLE: "sequences-appending" "Appending sequences"
82 { $subsection append }
83 { $subsection 3append }
84 { $subsection concat }
85 { $subsection join }
86 "A pair of words useful for aligning strings:"
87 { $subsection pad-left }
88 { $subsection pad-right } ;
89
90 ARTICLE: "sequences-slices" "Subsequences and slices"
91 "Extracting a subsequence:"
92 { $subsection subseq }
93 { $subsection head }
94 { $subsection tail }
95 { $subsection head* }
96 { $subsection tail* }
97 "Removing the first or last element:"
98 { $subsection rest }
99 { $subsection but-last }
100 "Taking a sequence apart into a head and a tail:"
101 { $subsection unclip }
102 { $subsection cut }
103 { $subsection cut* }
104 "A " { $emphasis "slice" } " is a virtual sequence which presents as view of a subsequence of an underlying sequence:"
105 { $subsection slice }
106 { $subsection slice? }
107 "Creating slices:"
108 { $subsection <slice> }
109 { $subsection head-slice }
110 { $subsection tail-slice }
111 { $subsection but-last-slice }
112 { $subsection rest-slice }
113 { $subsection head-slice* }
114 { $subsection tail-slice* }
115 "Taking a sequence apart into a head and a tail:"
116 { $subsection unclip-slice }
117 { $subsection cut-slice }
118 "A utility for words which use slices as mutable iterators:"
119 { $subsection <flat-slice> } ;
120
121 ARTICLE: "sequences-combinators" "Sequence combinators"
122 "Iteration:"
123 { $subsection each }
124 { $subsection reduce }
125 { $subsection interleave }
126 { $subsection 2each }
127 { $subsection 2reduce }
128 "Mapping:"
129 { $subsection map }
130 { $subsection 2map }
131 { $subsection accumulate }
132 { $subsection unfold }
133 "Filtering:"
134 { $subsection push-if }
135 { $subsection filter } ;
136
137 ARTICLE: "sequences-tests" "Testing sequences"
138 "Testing for an empty sequence:"
139 { $subsection empty? }
140 "Testing indices:"
141 { $subsection bounds-check? }
142 "Testing if a sequence contains an object:"
143 { $subsection member? }
144 { $subsection memq? }
145 "Testing if a sequence contains a subsequence:"
146 { $subsection head? }
147 { $subsection tail? }
148 { $subsection subseq? }
149 "Testing if a sequence contains elements satisfying a predicate:"
150 { $subsection contains? }
151 { $subsection all? }
152 { $subsection 2all? }
153 "Testing how elements are related:"
154 { $subsection monotonic? }
155 { $subsection all-eq? }
156 { $subsection all-equal? } ;
157
158 ARTICLE: "sequences-search" "Searching sequences"
159 "Finding the index of an element:"
160 { $subsection index }
161 { $subsection index-from }
162 { $subsection last-index }
163 { $subsection last-index-from }
164 "Finding the start of a subsequence:"
165 { $subsection start }
166 { $subsection start* }
167 "Finding the index of an element satisfying a predicate:"
168 { $subsection find }
169 { $subsection find-from }
170 { $subsection find-last }
171 { $subsection find-last-from } ;
172
173 ARTICLE: "sequences-destructive" "Destructive operations"
174 "These words modify their input, instead of creating a new sequence."
175 $nl
176 "In-place variant of " { $link reverse } ":"
177 { $subsection reverse-here }
178 "In-place variant of " { $link append } ":"
179 { $subsection push-all }
180 "In-place variant of " { $link remove } ":"
181 { $subsection delete }
182 "In-place variant of " { $link map } ":"
183 { $subsection change-each }
184 "Changing elements:"
185 { $subsection change-nth }
186 { $subsection cache-nth }
187 "Deleting elements:"
188 { $subsection delete-nth }
189 { $subsection delete-slice }
190 { $subsection delete-all }
191 "Other destructive words:"
192 { $subsection move }
193 { $subsection exchange }
194 { $subsection copy }
195 { $subsection replace-slice }
196 { $see-also set-nth push pop "sequences-stacks" } ;
197
198 ARTICLE: "sequences-stacks" "Treating sequences as stacks"
199 "The classical stack operations, modifying a sequence in place:"
200 { $subsection peek }
201 { $subsection push }
202 { $subsection pop }
203 { $subsection pop* }
204 { $see-also empty? } ;
205
206 ARTICLE: "sequences-comparing" "Comparing sequences"
207 "Element equality testing:"
208 { $subsection sequence= }
209 { $subsection mismatch }
210 { $subsection drop-prefix }
211 "The " { $link <=> } " generic word performs lexicographic comparison when applied to sequences." ;
212
213 ARTICLE: "sequences-f" "The f object as a sequence"
214 "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." ;
215
216 ARTICLE: "sequences" "Sequence operations"
217 "A " { $emphasis "sequence" } " is a finite, linearly-ordered collection of elements. Words for working with sequences are in the " { $vocab-link "sequences" } " vocabulary."
218 $nl
219 "Sequences implement a protocol:"
220 { $subsection "sequence-protocol" }
221 { $subsection "sequences-f" }
222 { $subsection "sequences-integers" }
223 "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 " { $emphasis "virtual sequences" } "."
224 { $subsection "sequences-access" }
225 { $subsection "sequences-combinators" }
226 { $subsection "sequences-add-remove" }
227 { $subsection "sequences-appending" }
228 { $subsection "sequences-slices" }
229 { $subsection "sequences-reshape" }
230 { $subsection "sequences-tests" }
231 { $subsection "sequences-search" }
232 { $subsection "sequences-comparing" }
233 { $subsection "sequences-split" }
234 { $subsection "grouping" }
235 { $subsection "sequences-destructive" }
236 { $subsection "sequences-stacks" }
237 { $subsection "sequences-sorting" }
238 { $subsection "sets" }
239 "For inner loops:"
240 { $subsection "sequences-unsafe" } ;
241
242 ABOUT: "sequences"
243
244 HELP: sequence
245 { $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:"
246     { $code "INSTANCE: my-sequence sequence" }
247 } ;
248
249 HELP: length
250 { $values { "seq" sequence } { "n" "a non-negative integer" } }
251 { $contract "Outputs the length of the sequence. All sequences support this operation." } ;
252
253 HELP: set-length
254 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
255 { $contract "Resizes the sequence. Not all sequences are resizable." }
256 { $errors "Throws a " { $link bounds-error } " if the new length is negative." }
257 { $side-effects "seq" } ;
258
259 HELP: lengthen
260 { $values { "n" "a non-negative integer" } { "seq" "a resizable sequence" } }
261 { $contract "Ensures the sequence has a length of at least " { $snippet "n" } " elements. This word differs from " { $link set-length } " in two respects:"
262     { $list
263         { "This word does not shrink the sequence if " { $snippet "n" } " is less than its length." }
264         { "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." }
265     }
266 } ;
267
268 HELP: nth
269 { $values { "n" "a non-negative integer" } { "seq" sequence } { "elt" "the element at the " { $snippet "n" } "th index" } }
270 { $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." }
271 { $errors "Throws a " { $link bounds-error } " if the index is negative, or greater than or equal to the length of the sequence." } ;
272
273 HELP: set-nth
274 { $values { "elt" object } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
275 { $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." }
276 { $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."
277 $nl
278 "Throws an error if the sequence cannot hold elements of the given type." }
279 { $side-effects "seq" } ;
280
281 HELP: immutable
282 { $values { "seq" sequence } }
283 { $description "Throws an " { $link immutable } " error." }
284 { $error-description "Thrown if an attempt is made to modify an immutable sequence." } ;
285
286 HELP: new-sequence
287 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a mutable sequence" } }
288 { $contract "Outputs a mutable sequence of length " { $snippet "n" } " which can hold the elements of " { $snippet "seq" } "." } ;
289
290 HELP: new-resizable
291 { $values { "len" "a non-negative integer" } { "seq" sequence } { "newseq" "a resizable mutable sequence" } }
292 { $contract "Outputs a resizable mutable sequence with an initial capacity of " { $snippet "n" } " elements and zero length, which can hold the elements of " { $snippet "seq" } "." }
293 { $examples
294     { $example "USING: prettyprint sequences ;" "300 V{ } new-resizable ." "V{ }" }
295     { $example "USING: prettyprint sequences ;" "300 SBUF\" \" new-resizable ." "SBUF\" \"" }
296 } ;
297
298 HELP: like
299 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
300 { $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."
301 $nl
302 "The default implementation does nothing." }
303 { $notes "Unlike " { $link clone-like } ", the output sequence might share storage with the input sequence." } ;
304
305 HELP: empty?
306 { $values { "seq" sequence } { "?" "a boolean" } }
307 { $description "Tests if the sequence has zero length." } ;
308
309 HELP: delete-all
310 { $values { "seq" "a resizable sequence" } }
311 { $description "Resizes the sequence to zero length, removing all elements. Not all sequences are resizable." }
312 { $errors "Throws a " { $link bounds-error } " if the new length is negative, or if the sequence is not resizable." }
313 { $side-effects "seq" } ;
314
315 HELP: resize
316 { $values { "n" "a non-negative integer" } { "seq" sequence } { "newseq" "a new sequence" } }
317 { $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." }
318 { $notes "This generic word is only implemented for strings and arrays." } ;
319
320 HELP: first
321 { $values { "seq" sequence } { "first" "the first element of the sequence" } }
322 { $description "Outputs the first element of the sequence." }
323 { $errors "Throws an error if the sequence is empty." } ;
324
325 HELP: second
326 { $values { "seq" sequence } { "second" "the second element of the sequence" } }
327 { $description "Outputs the second element of the sequence." }
328 { $errors "Throws an error if the sequence contains less than two elements." } ;
329
330 HELP: third
331 { $values { "seq" sequence } { "third" "the third element of the sequence" } }
332 { $description "Outputs the third element of the sequence." }
333 { $errors "Throws an error if the sequence contains less than three elements." } ;
334
335 HELP: fourth
336 { $values { "seq" sequence } { "fourth" "the fourth element of the sequence" } }
337 { $description "Outputs the fourth element of the sequence." }
338 { $errors "Throws an error if the sequence contains less than four elements." } ;
339
340 HELP: push
341 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
342 { $description "Adds an element at the end of the sequence. The sequence length is adjusted accordingly." }
343 { $errors "Throws an error if " { $snippet "seq" } " is not resizable, or if the type of " { $snippet "elt" } " is not permitted in " { $snippet "seq" } "." }
344 { $side-effects "seq" } ;
345
346 HELP: bounds-check?
347 { $values { "n" "an integer" } { "seq" sequence } { "?" "a boolean" } }
348 { $description "Tests if the index is within the bounds of the sequence." } ;
349
350 HELP: bounds-error
351 { $values { "n" "a positive integer" } { "seq" sequence } }
352 { $description "Throws a " { $link bounds-error } "." }
353 { $error-description "Thrown by " { $link nth } ", " { $link set-nth } " and " { $link set-length } " if the given index lies beyond the bounds of the sequence." } ;
354
355 HELP: bounds-check
356 { $values { "n" "a positive integer" } { "seq" sequence } }
357 { $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." } ;
358
359 HELP: ?nth
360 { $values { "n" "an integer" } { "seq" sequence } { "elt/f" "an object or " { $link f } } }
361 { $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 } "." } ;
362
363 HELP: nth-unsafe
364 { $values { "n" "an integer" } { "seq" sequence } { "elt" object } }
365 { $contract "Unsafe variant of " { $link nth } " that does not perform bounds checks." } ;
366
367 HELP: set-nth-unsafe
368 { $values { "elt" object } { "n" "an integer" } { "seq" sequence } }
369 { $contract "Unsafe variant of " { $link set-nth } " that does not perform bounds checks." } ;
370
371 HELP: exchange-unsafe
372 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
373 { $description "Unsafe variant of " { $link exchange } " that does not perform bounds checks." } ;
374
375 HELP: first2-unsafe
376 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
377 { $contract "Unsafe variant of " { $link first2 } " that does not perform bounds checks." } ;
378
379 HELP: first3-unsafe
380 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
381 { $contract "Unsafe variant of " { $link first3 } " that does not perform bounds checks." } ;
382
383 HELP: first4-unsafe
384 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
385 { $contract "Unsafe variant of " { $link first4 } " that does not perform bounds checks." } ;
386
387 HELP: 2sequence
388 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "seq" sequence } }
389 { $description "Creates a two-element sequence of the same type as " { $snippet "exemplar" } "." } ;
390
391 HELP: 3sequence
392 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "exemplar" sequence } { "seq" sequence } }
393 { $description "Creates a three-element sequence of the same type as " { $snippet "exemplar" } "." } ;
394
395 HELP: 4sequence
396 { $values { "obj1" object } { "obj2" object } { "exemplar" sequence } { "obj3" object } { "obj4" object } { "seq" sequence } }
397 { $description "Creates a four-element sequence of the same type as " { $snippet "exemplar" } "." } ;
398
399 HELP: first2
400 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } }
401 { $description "Pushes the first two elements of a sequence." }
402 { $errors "Throws an error if the sequence has less than two elements." } ;
403
404 HELP: first3
405 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } }
406 { $description "Pushes the first three elements of a sequence." }
407 { $errors "Throws an error if the sequence has less than three elements." } ;
408
409 HELP: first4
410 { $values { "seq" sequence } { "first" "the first element" } { "second" "the second element" } { "third" "the third element" } { "fourth" "the fourth element" } }
411 { $description "Pushes the first four elements of a sequence." }
412 { $errors "Throws an error if the sequence has less than four elements." } ;
413
414 HELP: array-capacity
415 { $values { "array" "an array" } { "n" "a non-negative fixnum" } }
416 { $description "Low-level array length accessor." }
417 { $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." } ;
418
419 HELP: array-nth
420 { $values { "n" "a non-negative fixnum" } { "array" "an array" }  { "elt" object } }
421 { $description "Low-level array element accessor." }
422 { $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." } ;
423
424 HELP: set-array-nth
425 { $values { "elt" object } { "n" "a non-negative fixnum" } { "array" "an array" }  }
426 { $description "Low-level array element mutator." }
427 { $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." } ;
428
429 HELP: collect
430 { $values { "n" "a non-negative integer" } { "quot" "a quotation with stack effect " { $snippet "( n -- value )" } } { "into" "a sequence of length at least " { $snippet "n" } } }
431 { $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." } ;
432
433 HELP: each
434 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- )" } } }
435 { $description "Applies the quotation to each element of the sequence in order." } ;
436
437 HELP: reduce
438 { $values { "seq" sequence } { "identity" object } { "quot" "a quotation with stack effect " { $snippet "( prev elt -- next )" } } { "result" "the final result" } }
439 { $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." }
440 { $examples
441     { $example "USING: math prettyprint sequences ;" "{ 1 5 3 } 0 [ + ] reduce ." "9" }
442 } ;
443
444 HELP: accumulate
445 { $values { "identity" object } { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( prev elt -- next )" } } { "final" "the final result" } { "newseq" "a new sequence" } }
446 { $description "Combines successive elements of the sequence using a binary operation, and outputs a sequence of intermediate results together with the final result. On the first iteration, the two inputs to the quotation are " { $snippet "identity" } ", and the first element of the sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the corresponding element of the sequence."
447 $nl
448 "When given the empty sequence, outputs an empty sequence together with the " { $snippet "identity" } "." }
449 { $examples
450     { $example "USING: math prettyprint sequences ;" "{ 2 2 2 2 2 } 0 [ + ] accumulate . ." "{ 0 2 4 6 8 }\n10" }
451 } ;
452
453 HELP: map
454 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( old -- new )" } } { "newseq" "a new sequence" } }
455 { $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." } ;
456
457 HELP: change-nth
458 { $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" "a quotation with stack effect " { $snippet "( elt -- newelt )" } } }
459 { $description "Applies the quotation to the " { $snippet "i" } "th element of the sequence, storing the result back into the sequence." }
460 { $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" } "." }
461 { $side-effects "seq" } ;
462
463 HELP: change-each
464 { $values { "seq" "a mutable sequence" } { "quot" "a quotation with stack effect " { $snippet "( old -- new )" } } }
465 { $description "Applies the quotation to each element yielding a new element, storing the new elements back in the original sequence." }
466 { $errors "Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by " { $snippet "quot" } "." }
467 { $side-effects "seq" } ;
468
469 HELP: min-length
470 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
471 { $description "Outputs the minimum of the lengths of the two sequences." } ;
472
473 HELP: max-length
474 { $values { "seq1" sequence } { "seq2" sequence } { "n" "a non-negative integer" } }
475 { $description "Outputs the maximum of the lengths of the two sequences." } ;
476
477 HELP: 2each
478 { $values { "seq1" sequence } { "seq2" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt1 elt2 -- )" } } }
479 { $description "Applies the quotation to pairs of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } "." }
480 { $notes "If one sequence is shorter than the other, than only the prefix having the length of the minimum of the two is examined." } ;
481
482 HELP: 2reduce
483 { $values { "seq1" sequence }
484           { "seq2" sequence }
485           { "identity" object }
486           { "quot" "a quotation with stack effect "
487                    { $snippet "( prev elt1 elt2 -- next )" } }
488           { "result" "the final result" } }
489 { $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" } "." }
490 { $notes "If one sequence is shorter than the other, then only the prefix having the length of the minimum of the two is examined." } ;
491
492 HELP: 2map
493 { $values { "seq1" sequence } { "seq2" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt1 elt2 -- new )" } } { "newseq" "a new sequence" } }
494 { $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" } "." }
495 { $notes "If one sequence is shorter than the other, than only the prefix having the length of the minimum of the two is examined." } ;
496
497 HELP: 2all?
498 { $values { "seq1" sequence } { "seq2" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt1 elt2 -- ? )" } } { "?" "a boolean" } }
499 { $description "Tests the predicate pairwise against elements of " { $snippet "seq1" } " and " { $snippet "seq2" } "." }
500 { $notes "If one sequence is shorter than the other, than only the prefix having the length of the minimum of the two is examined." } ;
501
502 HELP: find
503 { $values { "seq" sequence }
504           { "quot" "a quotation with stack effect "
505                    { $snippet "( elt -- ? )" } }
506           { "i" "the index of the first match, or f" }
507           { "elt" "the first matching element, or " { $link f } } }
508 { $description "A simpler variant of " { $link find-from } " where the starting index is 0." } ;
509
510 HELP: find-from
511 { $values { "n" "a starting index" }
512           { "seq" sequence }
513           { "quot" "a quotation with stack effect "
514                    { $snippet "( elt -- ? )" } }
515           { "i" "the index of the first match, or f" }
516           { "elt" "the first matching element, or " { $link f } } }
517 { $description "Applies the quotation to each element of the sequence in turn, until it outputs a true value or the end of the sequence is reached. If the quotation yields a true value for some sequence element, the word outputs the element index and the element itself. Otherwise, the word outputs an index of f and " { $link f } " as the element." } ;
518
519 HELP: find-last
520 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "i" "the index of the first match, or f" } { "elt" "the first matching element, or " { $link f } } }
521 { $description "A simpler variant of " { $link find-last-from } " where the starting index is one less than the length of the sequence." } ;
522
523 HELP: find-last-from
524 { $values { "n" "a starting index" } { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "i" "the index of the first match, or f" } { "elt" "the first matching element, or " { $link f } } }
525 { $description "Applies the quotation to each element of the sequence in reverse order, until it outputs a true value or the start of the sequence is reached. If the quotation yields a true value for some sequence element, the word outputs the element index and the element itself. Otherwise, the word outputs an index of f and " { $link f } " as the element." } ;
526
527 HELP: contains?
528 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "?" "a boolean" } }
529 { $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 } "." } ;
530
531 HELP: all?
532 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "?" "a boolean" } }
533 { $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 } "." } ;
534
535 HELP: push-if
536 { $values { "elt" object } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "accum" "a resizable mutable sequence" } }
537 { $description "Adds the element at the end of the sequence if the quotation yields a true value." } 
538 { $notes "This word is a factor of " { $link filter } "." } ;
539
540 HELP: filter
541 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "subseq" "a new sequence" } }
542 { $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." } ;
543
544 HELP: monotonic?
545 { $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt elt -- ? )" } } { "?" "a boolean" } }
546 { $description "Applies the relation to successive pairs of elements in the sequence, testing for a truth value. The relation should be a transitive relation, such as a total order or an equality relation." }
547 { $examples
548     "Testing if a sequence is non-decreasing:"
549     { $example "USING: math prettyprint sequences ;" "{ 1 1 2 } [ <= ] monotonic? ." "t" }
550     "Testing if a sequence is decreasing:"
551     { $example "USING: math prettyprint sequences ;" "{ 9 8 6 7 } [ < ] monotonic? ." "f" }
552 } ;
553
554 { monotonic? all-eq? all-equal? } related-words
555
556 HELP: interleave
557 { $values { "seq" sequence } { "between" "a quotation" } { "quot" "a quotation with stack effect " { $snippet "( elt -- )" } } }
558 { $description "Applies " { $snippet "quot" } " to each element in turn, also invoking " { $snippet "between" } " in-between each pair of elements." }
559 { $example "USING: io sequences ;" "{ \"a\" \"b\" \"c\" } [ \"X\" write ] [ write ] interleave" "aXbXc" } ;
560
561 HELP: cache-nth
562 { $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" "a quotation with stack effect " { $snippet "( i -- elt )" } } { "elt" object } }
563 { $description "If the sequence does not contain at least " { $snippet "i" } " elements or if the " { $snippet "i" } "th element of the sequence is " { $link f } ", calls the quotation to produce a new value, and stores it back into the sequence. Otherwise, this word outputs the " { $snippet "i" } "th element of the sequence." }
564 { $side-effects "seq" } ;
565
566 HELP: index
567 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
568 { $description "Outputs the index of the first element in the sequence equal to " { $snippet "obj" } ". If no element is found, outputs " { $link f } "." } ;
569
570 { index index-from last-index last-index-from member? memq? } related-words
571
572 HELP: index-from
573 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
574 { $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 } "." } ;
575
576 HELP: last-index
577 { $values { "obj" object } { "seq" sequence } { "n" "an index" } }
578 { $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 } "." } ;
579
580 HELP: last-index-from
581 { $values { "obj" object } { "i" "a start index" } { "seq" sequence } { "n" "an index" } }
582 { $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 } "." } ;
583
584 HELP: member?
585 { $values { "obj" object } { "seq" sequence } { "?" "a boolean" } }
586 { $description "Tests if the sequence contains an element equal to the object." } ;
587
588 HELP: memq?
589 { $values { "obj" object } { "seq" sequence } { "?" "a boolean" } }
590 { $description "Tests if the sequence contains the object." }
591 { $examples
592     "This word uses identity comparison, so the following will most likely print " { $link f } ":"
593     { $example "USING: prettyprint sequences ;" "\"hello\" { \"hello\" } memq? ." "f" }
594 } ;
595
596 HELP: remove
597 { $values { "obj" object } { "seq" sequence } { "newseq" "a new sequence" } }
598 { $description "Outputs a new sequence containing all elements of the input sequence except those equal to the given element." } ;
599
600 HELP: move
601 { $values { "from" "an index in " { $snippet "seq" } } { "to" "an index in " { $snippet "seq" } } { "seq" "a mutable sequence" } }
602 { $description "Sets the element with index " { $snippet "m" } " to the element with index " { $snippet "n" } "." }
603 { $side-effects "seq" } ;
604
605 HELP: delete
606 { $values { "elt" object } { "seq" "a resizable mutable sequence" } }
607 { $description "Removes all elements equal to " { $snippet "elt" } " from " { $snippet "seq" } "." }
608 { $side-effects "seq" } ;
609
610 HELP: delete-nth
611 { $values { "n" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
612 { $description "Removes the " { $snippet "n" } "th element from the sequence, shifting all other elements down and reducing its length by one." }
613 { $side-effects "seq" } ;
614
615 HELP: delete-slice
616 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" "a resizable mutable sequence" } }
617 { $description "Removes a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } "." }
618 { $side-effects "seq" } ;
619
620 HELP: replace-slice
621 { $values { "new" sequence } { "seq" "a mutable sequence" } { "from" "a non-negative integer" } { "to" "a non-negative integer" } }
622 { $description "Replaces a range of elements beginning at index " { $snippet "from" } " and ending before index " { $snippet "to" } " with a new sequence." }
623 { $notes "If the " { $snippet "to - from" } " is equal to the length of " { $snippet "new" } ", the sequence remains the same size, and does not have to support resizing. However, if " { $snippet "to - from" } " is not equal to the length of " { $snippet "new" } ", the " { $link set-length } " word is called on " { $snippet "seq" } ", so fixed-size sequences should not be passed in this case." }
624 { $errors "Throws an error if " { $snippet "new" } " contains elements whose types are not permissible in " { $snippet "seq" } "." }
625 { $side-effects "seq" } ;
626
627 { push prefix suffix } related-words
628
629 HELP: suffix
630 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
631 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the end of " { $snippet "seq" } "." }
632 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." }
633 { $examples
634     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 4 suffix ." "{ 1 2 3 4 }" }
635 } ;
636
637 HELP: prefix
638 { $values { "seq" sequence } { "elt" object } { "newseq" sequence } }
639 { $description "Outputs a new sequence obtained by adding " { $snippet "elt" } " at the beginning of " { $snippet "seq" } "." }
640 { $errors "Throws an error if the type of " { $snippet "elt" } " is not permitted in sequences of the same class as " { $snippet "seq1" } "." } 
641 { $examples
642 { $example "USING: prettyprint sequences ;" "{ 1 2 3 } 0 prefix ." "{ 0 1 2 3 }" }
643 } ;
644
645 HELP: sum-lengths
646 { $values { "seq" "a sequence of sequences" } { "n" integer } }
647 { $description "Outputs the sum of the lengths of all sequences in " { $snippet "seq" } "." } ;
648
649 HELP: concat
650 { $values { "seq" sequence } { "newseq" sequence } }
651 { $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" } "." }
652 { $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" } "." } ;
653
654 HELP: join
655 { $values { "seq" sequence } { "glue" sequence } { "newseq" sequence } }
656 { $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" } "." }
657 { $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" } "." } ;
658
659 { join concat } related-words
660
661 HELP: peek
662 { $values { "seq" sequence } { "elt" object } }
663 { $description "Outputs the last element of a sequence." }
664 { $errors "Throws an error if the sequence is empty." } ;
665
666 { peek pop pop* } related-words
667
668 HELP: pop*
669 { $values { "seq" "a resizable mutable sequence" } }
670 { $description "Removes the last element and shortens the sequence." }
671 { $side-effects "seq" }
672 { $errors "Throws an error if the sequence is empty." } ;
673
674 HELP: pop
675 { $values { "seq" "a resizable mutable sequence" } { "elt" object } }
676 { $description "Outputs the last element after removing it and shortening the sequence." }
677 { $side-effects "seq" }
678 { $errors "Throws an error if the sequence is empty." } ;
679
680 HELP: all-equal?
681 { $values { "seq" sequence } { "?" "a boolean" } }
682 { $description "Tests if all elements in the sequence are equal. Yields true with an empty sequence." } ;
683
684 HELP: all-eq?
685 { $values { "seq" sequence } { "?" "a boolean" } }
686 { $description "Tests if all elements in the sequence are the same identical object. Yields true with an empty sequence." } ;
687
688 HELP: mismatch
689 { $values { "seq1" sequence } { "seq2" sequence } { "i" "an index" } }
690 { $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." } ;
691
692 HELP: flip
693 { $values { "matrix" "a sequence of equal-length sequences" } { "newmatrix" "a sequence of equal-length sequences" } }
694 { $description "Transposes the matrix; that is, rows become columns and columns become rows." }
695 { $examples { $example "USING: prettyprint sequences ;" "{ { 1 2 3 } { 4 5 6 } } flip ." "{ { 1 4 } { 2 5 } { 3 6 } }" } } ;
696
697 HELP: exchange
698 { $values { "m" "a non-negative integer" } { "n" "a non-negative integer" } { "seq" "a mutable sequence" } }
699 { $description "Exchanges the " { $snippet "m" } "th and " { $snippet "n" } "th elements of " { $snippet "seq" } "." } ;
700
701 HELP: reverse-here
702 { $values { "seq" "a mutable sequence" } }
703 { $description "Reverses a sequence in-place." }
704 { $side-effects "seq" } ;
705
706 HELP: padding
707 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "quot" "a quotation with stack effect " { $snippet "( seq1 seq2 -- newseq )" } } { "newseq" "a new sequence" } }
708 { $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." } ;
709
710 HELP: pad-left
711 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
712 { $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" } "." }
713 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-left print ] each" "---ab\n-quux" } } ;
714
715 HELP: pad-right
716 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
717 { $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" } "." }
718 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-right print ] each" "ab---\nquux-" } } ;
719
720 HELP: sequence=
721 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
722 { $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." } ;
723
724 HELP: reversed
725 { $class-description "A virtual sequence which presents a reversed view of an underlying sequence. New instances can be created by calling " { $link <reversed> } "." } ;
726
727 HELP: reverse
728 { $values { "seq" sequence } { "newseq" "a new sequence" } }
729 { $description "Outputs a new sequence having the same elements as " { $snippet "seq" } " but in reverse order." } ;
730
731 { reverse <reversed> } related-words
732
733 HELP: <reversed> ( seq -- reversed )
734 { $values { "seq" sequence } { "reversed" "a new sequence" } }
735 { $description "Creates an instance of the " { $link reversed } " virtual sequence." } ;
736
737 HELP: slice-error
738 { $values { "str" "a reason" } }
739 { $description "Throws a " { $link slice-error } "." }
740 { $error-description "Thrown by " { $link <slice> } " if one of the following invalid conditions holds:"
741     { $list
742         "The start index is negative"
743         "The end index is greater than the length of the sequence"
744         "The start index is greater than the end index"
745     }
746 } ;
747
748 HELP: slice
749 { $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> } ". Slices are mutable if the underlying sequence is mutable, and mutating a slice changes the underlying sequence." }
750 { $notes "The slots of a slice should not be changed after the slice has been created, because this can break invariants." } ;
751
752 HELP: check-slice
753 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } }
754 { $description "Ensures that " { $snippet "m" } " is less than or equal to " { $snippet "m" } ", and that both indices are within bounds for " { $snippet "seq" } "." }
755 { $errors "Throws a " { $link slice-error } " if the preconditions are not met." } ;
756
757 HELP: collapse-slice
758 { $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 } }
759 { $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." }
760 ;
761
762 HELP: <flat-slice>
763 { $values { "seq" sequence } { "slice" slice } }
764 { $description "Outputs a slice with the same elements as " { $snippet "seq" } ", and " { $link slice-from } " equal to 0 and " { $link slice-to } " equal to the length of " { $snippet "seq" } "." }
765 { $notes "Some words create slices then proceed to read and write the " { $link slice-from } " and " { $link slice-to } " slots of the slice. To behave predictably when they are themselves given a slice as input, they apply this word first to get a canonical slice." } ;
766
767 HELP: <slice>
768 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "slice" "a slice" } }
769 { $description "Outputs a new virtual sequence sharing storage with the subrange of elements in " { $snippet "seq" } " with indices starting from and including " { $snippet "m" } ", and up to but not including " { $snippet "n" } "." }
770 { $errors "Throws an error if " { $snippet "m" } " or " { $snippet "n" } " is out of bounds." }
771 { $notes "Taking the slice of a slice outputs a slice of the underlying sequence of the original slice. Keep this in mind when writing code which depends on the values of " { $link slice-from } " and " { $link slice-to } " being equal to the inputs to this word. The " { $link <flat-slice> } " word might be helpful in such situations." } ;
772
773 { <slice> subseq } related-words
774
775 HELP: repetition
776 { $class-description "A virtual sequence consisting of " { $link repetition-elt } " repeated " { $link repetition-len } " times. Repetitions are created by calling " { $link <repetition> } "." } ;
777
778 HELP: <repetition> ( len elt -- repetition )
779 { $values { "len" "a non-negative integer" } { "elt" object } { "repetition" repetition } }
780 { $description "Creates a new " { $link repetition } "." }
781 { $examples
782     { $example "USING: arrays prettyprint sequences ;" "10 \"X\" <repetition> >array ." "{ \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" \"X\" }" }
783     { $example "USING: prettyprint sequences ;" "10 \"X\" <repetition> concat ." "\"XXXXXXXXXX\"" }
784 } ;
785 HELP: copy
786 { $values { "src" sequence } { "i" "an index in " { $snippet "dest" } } { "dst" "a mutable sequence" } }
787 { $description "Copies all elements of " { $snippet "src" } " to " { $snippet "dest" } ", with destination indices starting from " { $snippet "i" } ". Grows " { $snippet "to" } " first if necessary." }
788 { $side-effects "dest" }
789 { $errors "An error is thrown if " { $snippet "to" } " is not resizable, and not large enough to hold the copied elements." } ;
790
791 HELP: push-all
792 { $values { "src" sequence } { "dest" "a resizable mutable sequence" } }
793 { $description "Appends " { $snippet "src" } " to the end of " { $snippet "dest" } "." }
794 { $side-effects "dest" }
795 { $errors "Throws an error if " { $snippet "src" } " contains elements not permitted in " { $snippet "dest" } "." } ;
796
797 HELP: append
798 { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } }
799 { $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq1" } " followed by " { $snippet "seq2" } "." }
800 { $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." } ;
801
802 HELP: 3append
803 { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } }
804 { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn." }
805 { $errors "Throws an error if " { $snippet "seq2" } " or " { $snippet "seq3" } " contain elements not permitted in sequences of the same class as " { $snippet "seq1" } "." } ;
806
807 HELP: subseq
808 { $values { "from" "a non-negative integer" } { "to" "a non-negative integer" } { "seq" sequence } { "subseq" "a new sequence" } }
809 { $description "Outputs a new sequence consisting of all elements starting from and including " { $snippet "from" } ", and up to but not including " { $snippet "to" } "." }
810 { $errors "Throws an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
811
812 HELP: clone-like
813 { $values { "seq" sequence } { "exemplar" sequence } { "newseq" "a new sequence" } }
814 { $description "Outputs a newly-allocated sequence with the same elements as " { $snippet "seq" } " but of the same type as " { $snippet "exemplar" } "." }
815 { $notes "Unlike " { $link like } ", this word always creates a new sequence which never shares storage with the original." } ;
816
817 HELP: head-slice
818 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
819 { $description "Outputs a virtual sequence sharing storage with the first " { $snippet "n" } " elements of the input sequence." }
820 { $errors "Throws an error if the index is out of bounds." } ;
821
822 HELP: tail-slice
823 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
824 { $description "Outputs a virtual sequence sharing storage with all elements from the " { $snippet "n" } "th index until the end of the input sequence." }
825 { $errors "Throws an error if the index is out of bounds." } ;
826
827 HELP: but-last-slice
828 { $values { "seq" sequence } { "slice" "a slice" } }
829 { $description "Outputs a virtual sequence sharing storage with all but the last element of the input sequence." }
830 { $errors "Throws an error on an empty sequence." } ;
831
832 HELP: rest-slice
833 { $values { "seq" sequence } { "slice" "a slice" } }
834 { $description "Outputs a virtual sequence sharing storage with all elements from the 1st index until the end of the input sequence." }
835 { $notes "Equivalent to " { $snippet "1 tail" } }
836 { $errors "Throws an error on an empty sequence." } ;
837
838 HELP: head-slice*
839 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
840 { $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." }
841 { $errors "Throws an error if the index is out of bounds." } ;
842
843 HELP: tail-slice*
844 { $values { "seq" sequence } { "n" "a non-negative integer" } { "slice" "a slice" } }
845 { $description "Outputs a virtual sequence sharing storage with the last " { $snippet "n" } " elements of the input sequence." }
846 { $errors "Throws an error if the index is out of bounds." } ;
847
848 HELP: head
849 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
850 { $description "Outputs a new sequence consisting of the first " { $snippet "n" } " elements of the input sequence." }
851 { $errors "Throws an error if the index is out of bounds." } ;
852
853 HELP: tail
854 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
855 { $description "Outputs a new sequence consisting of the input sequence with the first n items removed." }
856 { $errors "Throws an error if the index is out of bounds." } ;
857
858 HELP: but-last
859 { $values { "seq" sequence } { "headseq" "a new sequence" } }
860 { $description "Outputs a new sequence consisting of the input sequence with the last item removed." }
861 { $errors "Throws an error on an empty sequence." } ;
862
863 HELP: rest
864 { $values { "seq" sequence } { "tailseq" "a new sequence" } }
865 { $description "Outputs a new sequence consisting of the input sequence with the first item removed." }
866 { $errors "Throws an error on an empty sequence." } ;
867
868 HELP: head*
869 { $values { "seq" sequence } { "n" "a non-negative integer" } { "headseq" "a new sequence" } }
870 { $description "Outputs a new sequence consisting of all elements of " { $snippet "seq" } " until the " { $snippet "n" } "th element from the end. In other words, it outputs a sequence of the first " { $snippet "l-n" } " elements of the input sequence, where " { $snippet "l" } " is its length." }
871 { $errors "Throws an error if the index is out of bounds." } ;
872
873 HELP: tail*
874 { $values { "seq" sequence } { "n" "a non-negative integer" } { "tailseq" "a new sequence" } }
875 { $description "Outputs a new sequence consisting of the last " { $snippet "n" } " elements of the input sequence." }
876 { $errors "Throws an error if the index is out of bounds." } ;
877
878 HELP: shorter?
879 { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
880 { $description "Tets if the length of " { $snippet "seq1" } " is smaller than the length of " { $snippet "seq2" } "." } ;
881
882 HELP: head?
883 { $values { "seq" sequence } { "begin" sequence } { "?" "a boolean" } }
884 { $description "Tests if " { $snippet "seq" } " starts with " { $snippet "begin" } ". If " { $snippet "begin" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
885
886 HELP: tail?
887 { $values { "seq" sequence } { "end" sequence } { "?" "a boolean" } }
888 { $description "Tests if " { $snippet "seq" } " ends with " { $snippet "end" } ". If " { $snippet "end" } " is longer than " { $snippet "seq" } ", this word outputs " { $link f } "." } ;
889
890 { delete-nth remove delete } related-words
891
892 HELP: cut-slice
893 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" "a slice" } }
894 { $description "Outputs a pair of sequences, where " { $snippet "before" } " consists of the first " { $snippet "n" } " elements of " { $snippet "seq" } " and has the same type, while " { $snippet "after" } " is a slice of the remaining elements." }
895 { $notes "Unlike " { $link cut } ", the run time of this word is proportional to the length of " { $snippet "before" } ", not " { $snippet "after" } ", so it is suitable for use in an iterative algorithm which cuts successive pieces off a sequence." } ;
896
897 HELP: cut
898 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
899 { $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" } "." }
900 { $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." } ;
901
902 HELP: cut*
903 { $values { "seq" sequence } { "n" "a non-negative integer" } { "before" sequence } { "after" sequence } }
904 { $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" } "." } ;
905
906 HELP: start*
907 { $values { "subseq" sequence } { "seq" sequence } { "n" "a start index" } { "i" "a start index" } }
908 { $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 } "." } ;
909
910 HELP: start
911 { $values { "subseq" sequence } { "seq" sequence } { "i" "a start index" } }
912 { $description "Outputs the start index of the first contiguous subsequence equal to " { $snippet "subseq" } ", or " { $link f } " if no matching subsequence is found." } ;
913
914 HELP: subseq?
915 { $values { "subseq" sequence } { "seq" sequence } { "?" "a boolean" } }
916 { $description "Tests if " { $snippet "seq" } " contains the elements of " { $snippet "subseq" } " as a contiguous subsequence." } ;
917
918 HELP: drop-prefix
919 { $values { "seq1" sequence } { "seq2" sequence } { "slice1" "a slice" } { "slice2" "a slice" } }
920 { $description "Outputs a pair of virtual sequences with the common prefix of " { $snippet "seq1" } " and " { $snippet "seq2" } " removed." } ;
921
922 HELP: unclip
923 { $values { "seq" sequence } { "rest" sequence } { "first" object } }
924 { $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." }
925 { $examples
926     { $example "USING: prettyprint sequences ;" "{ 1 2 3 } unclip suffix ." "{ 2 3 1 }" }
927 } ;
928
929 HELP: unclip-slice
930 { $values { "seq" sequence } { "rest" slice } { "first" object } }
931 { $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." } ;
932
933 HELP: sum
934 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
935 { $description "Outputs the sum of all elements of " { $snippet "seq" } ". Outputs zero given an empty sequence." } ;
936
937 HELP: product
938 { $values { "seq" "a sequence of numbers" } { "n" "a number" } }
939 { $description "Outputs the product of all elements of " { $snippet "seq" } ". Outputs one given an empty sequence." } ;
940
941 HELP: infimum
942 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
943 { $description "Outputs the least element of " { $snippet "seq" } "." }
944 { $errors "Throws an error if the sequence is empty." } ;
945
946 HELP: supremum
947 { $values { "seq" "a sequence of real numbers" } { "n" "a number" } }
948 { $description "Outputs the greatest element of " { $snippet "seq" } "." }
949 { $errors "Throws an error if the sequence is empty." } ;
950
951 HELP: unfold
952 { $values { "pred" "a quotation with stack effect " { $snippet "( -- ? )" } } { "quot" "a quotation with stack effect " { $snippet "( -- obj )" } } { "tail" "a quotation" } { "seq" "a sequence" } }
953 { $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." }
954 { $examples
955     "The following example divides a number by two until we reach zero, and accumulates intermediate results:"
956     { $example "USING: kernel math prettyprint sequences ;" "1337 [ dup 0 > ] [ 2/ dup ] [ ] unfold nip ." "{ 668 334 167 83 41 20 10 5 2 1 0 }" }
957     "The " { $snippet "tail" } " quotation is used when the predicate produces more than one output value. In this case, we have to drop this value even if the predicate fails in order for stack inference to calculate a stack effect for the " { $link unfold } " call:"
958     { $unchecked-example "USING: kernel prettyprint random sequences ;" "[ 10 random dup 1 > ] [ ] [ drop ] unfold ." "{ 8 2 2 9 }" }
959 } ;