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