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