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