]> gitweb.factorcode.org Git - factor.git/blob - extra/sequences/extras/extras-docs.factor
sequences.extras: simplify pad-center, add docs.
[factor.git] / extra / sequences / extras / extras-docs.factor
1 USING: arrays help.markup help.syntax kernel math multiline
2 quotations sequences ;
3 IN: sequences.extras
4
5 HELP: pad-center
6 { $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
7 { $description "Outputs a new sequence consisting of " { $snippet "seq" } " padded on the left and right with enough repetitions of " { $snippet "elt" } " to have the result be of length " { $snippet "n" } "." }
8 { $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-center print ] each" "-ab--\nquux-" } } ;
9
10 HELP: ?supremum
11 { $values
12     { "seq/f" { $maybe sequence } }
13     { "elt/f" { $maybe object } }
14 }
15 { $description "Outputs the greatest element of " { $snippet "seq" } ", ignoring any " { $link POSTPONE: f } " elements in it. If " { $snippet "seq" } " is empty or " { $link POSTPONE: f } ", returns " { $link POSTPONE: f } "." }
16 { $examples
17     { $example "USING: prettyprint sequences.extras ;"
18     "{ 1 f 3 2 } ?supremum ."
19     "3" }
20 } ;
21
22 HELP: ?infimum
23 { $values
24     { "seq/f" { $maybe sequence } }
25     { "elt/f" { $maybe object } }
26 }
27 { $description "Outputs the least element of " { $snippet "seq" } ", ignoring any " { $link POSTPONE: f } " elements in it. If " { $snippet "seq" } " is empty or " { $link POSTPONE: f } ", returns " { $link POSTPONE: f } "." }
28 { $examples
29     { $example "USING: prettyprint sequences.extras ;"
30     "{ 1 f 3 2 } ?infimum ."
31     "1" }
32 } ;
33
34 { ?supremum ?infimum } related-words
35
36 HELP: 2count
37 { $values
38     { "seq1" sequence }
39     { "seq2" sequence }
40     { "quot" { $quotation ( ... elt1 elt2 -- ... ? ) } }
41     { "n" integer } }
42 { $description "Efficiently counts how many pairwise elements of " { $snippet "seq1" } " and " { $snippet "seq2" } " that the predicate quotation matches." }
43 { $examples
44     { $example "USING: kernel prettyprint sequences.extras ;" "{ 1 2 3 } { 3 2 1 } [ = ] 2count ." "1" } }
45 { $see-also count } ;
46
47 HELP: 2each-index
48 { $values
49     { "seq1" sequence }
50     { "seq2" sequence }
51     { "quot" { $quotation ( ... elt1 elt2 index -- ... ) } } }
52 { $description "Applies " { $snippet "quot" } " to each pair of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } ", providing the index of the elements at the top of the stack." }
53 { $see-also 2each each-index } ;
54
55 HELP: 2map!
56 { $values
57     { "seq1" sequence }
58     { "seq2" sequence }
59     { "quot" { $quotation ( ... elt1 elt2 -- ... newelt ) } } }
60 { $description "Applies the quotation to each pair of elements from " { $snippet "seq1" } " and " { $snippet "seq2" } ", yielding a new element, and storing it back into " { $snippet "seq1" } ". Returns " { $snippet "seq1" } "." }
61 { $see-also 2map map! } ;
62
63 HELP: 2map-index
64 { $values
65     { "seq1" sequence }
66     { "seq2" sequence }
67     { "quot" { $quotation ( ... elt1 elt2 index -- ... newelt ) } }
68     { "newseq" sequence } }
69 { $description "Calls the quotation with each pair of elements of the two sequences and their index on the stack, with the index on the top of the stack. Collects the outputs of the quotation and outputs them into a new sequence of the same type as the first sequence." }
70 { $see-also 2map map-index } ;
71
72 HELP: count*
73 { $values
74     { "seq" sequence }
75     { "quot" { $quotation ( ... elt -- ... ? ) } }
76     { "%" rational } }
77 { $description "Outputs the fraction of elements in the sequence for which the predicate quotation matches." }
78 { $examples { $example "USING: math math.ranges prettyprint sequences.extras ;" "100 [1,b] [ even? ] count* ." "1/2" } } ;
79
80 HELP: collapse
81 { $values
82     { "seq" sequence }
83     { "quot" { $quotation ( ... elt -- ... ? ) } }
84     { "elt" object }
85     { "seq'" sequence } }
86 { $description "Generate a new sequence where all runs of elements for which the predicate returns true are replaced by a single instance of " { $snippet "elt" } "." }
87 { $see-also compact }
88 { $examples
89     "Collapse multiple spaces in a string down to a single space"
90     { $example "USING: kernel prettyprint sequences.extras ;" "\"   Hello,    crazy    world   \" [ CHAR: \\s = ] \" \" collapse ." "\" Hello, crazy world \"" } } ;
91
92 HELP: compact
93 { $values
94     { "seq" sequence }
95     { "quot" { $quotation ( ... elt -- ... ? ) } }
96     { "elt" object }
97     { "seq'" sequence } }
98 { $description "Generate a new sequence where all runs of elements for which the predicate returns true are replaced by a single instance of " { $snippet "elt" } ". Runs at the beginning or end of the sequence for which the predicate returns true are removed." }
99 { $see-also collapse }
100 { $examples
101     "Collapse multiple spaces in a string down to a single space"
102     { $example "USING: kernel prettyprint sequences.extras ;" "\"   Hello,    crazy    world   \" [ CHAR: \\s = ] \" \" compact ." "\"Hello, crazy world\"" } } ;
103
104 HELP: <evens>
105 { $values { "seq" sequence } { "evens" evens } }
106 { $description "Create a virtual sequence whose elements consist of the even-indexed elements from the original sequence." }
107 { $notes "Because sequences are zero-indexed, this collection includes the first, third, fifth, etc. elements of the actual sequence which can be counterintuitive." }
108 { $see-also <odds> } ;
109
110 HELP: find-all
111 { $values
112     { "seq" sequence }
113     { "quot" { $quotation ( ... elt -- ... ? ) } }
114     { "elts" "the indices of the matching elements" } }
115 { $description "Similar to " { $link find } ", but finds all of the indices and elements that match the provided quotation, not just the first." }
116 { $notes "The result is provided as an array of arrays, whose first value is the index and whose second value is the element." } ;
117
118 HELP: first=
119 { $values
120     { "seq" sequence }
121     { "elt" object }
122     { "?" boolean } }
123 { $description "Checks whether the first element of " { $snippet "seq" } " is equal to " { $snippet "elt" } "." } ;
124
125 HELP: first?
126 { $values
127     { "seq" sequence }
128     { "quot" { $quotation ( ... elt -- ... ? ) } }
129     { "?" boolean } }
130 { $description "Tests whether the first element of " { $snippet "seq" } " satisfies the provided predicate." } ;
131
132 HELP: fourth=
133 { $values
134     { "seq" sequence }
135     { "elt" object }
136     { "?" boolean } }
137 { $description "Checks whether the fourth element of " { $snippet "seq" } " is equal to " { $snippet "elt" } "." } ;
138
139 HELP: fourth?
140 { $values
141     { "seq" sequence }
142     { "quot" { $quotation ( ... elt -- ... ? ) } }
143     { "?" boolean } }
144 { $description "Tests whether the fourth element of " { $snippet "seq" } " satisfies the provided predicate." } ;
145
146 HELP: <odds>
147 { $values { "seq" sequence } { "odds" odds } }
148 { $description "Create a virtual sequence whose elements consist of the odd-indexed elements from the original sequence." }
149 { $notes "Because sequences are zero-indexed, this collection includes the second, fourth, sixth, etc. elements of the actual sequence which can be counterintuitive." }
150 { $see-also <evens> } ;
151
152 HELP: >resizable
153 { $values { "seq" sequence } { "accum" sequence } }
154 { $description "Converts a sequence into the nearest resizable equivalent, preserving its contents." } ;
155
156 HELP: second=
157 { $values
158     { "seq" sequence }
159     { "elt" object }
160     { "?" boolean } }
161 { $description "Checks whether the second element of " { $snippet "seq" } " is equal to " { $snippet "elt" } "." } ;
162
163 HELP: second?
164 { $values
165     { "seq" sequence }
166     { "quot" { $quotation ( ... elt -- ... ? ) } }
167     { "?" boolean } }
168 { $description "Tests whether the second element of " { $snippet "seq" } " satisfies the provided predicate." } ;
169
170 HELP: subseq*
171 { $values
172      { "from" integer } { "to" integer } { "seq" sequence } { "subseq" sequence } }
173 { $description "Outputs a new sequence using positions relative to one or both ends of the sequence. Positive values describes offsets relative to the start of the sequence, negative values relative to the end. Values of " { $link f } " for " { $snippet "from" } " indicate the beginning of the sequence, while an " { $link f } " for " { $snippet "to" } " indicates the end of the sequence." }
174 { $notes "Both " { $snippet "from" } " and " { $snippet "to" } " can be safely set to values outside the length of the sequence. Also, " { $snippet "from" } " can safely reference a smaller or greater index position than " { $snippet "to" } "." }
175 { $examples
176     "Using a negative relative index:"
177     { $example "USING: prettyprint sequences.extras ; 2 -1 \"abcdefg\" subseq* ."
178                "\"cdef\""
179     }
180     "Using optional indices:"
181     { $example "USING: prettyprint sequences.extras ; f -4 \"abcdefg\" subseq* ."
182                "\"abc\""
183     }
184     "Using larger-than-necessary indices:"
185     { $example "USING: prettyprint sequences.extras ; 0 10 \"abcdefg\" subseq* ."
186                "\"abcdefg\""
187     }
188     "Trimming from either end of the sequence."
189     { $example "USING: prettyprint sequences.extras ; 1 -1 \"abcdefg\" subseq* ."
190                "\"bcdef\""
191     }
192 } ;
193
194 HELP: third=
195 { $values
196     { "seq" sequence }
197     { "elt" object }
198     { "?" boolean } }
199 { $description "Checks whether the third element of " { $snippet "seq" } " is equal to " { $snippet "elt" } "." } ;
200
201 HELP: third?
202 { $values
203     { "seq" sequence }
204     { "quot" { $quotation ( ... elt -- ... ? ) } }
205     { "?" boolean } }
206 { $description "Tests whether the third element of " { $snippet "seq" } " satisfies the provided predicate." } ;
207
208 HELP: unsurround
209 { $values
210     { "newseq" sequence }
211     { "seq2" sequence }
212     { "seq3" sequence }
213     { "seq1" sequence } }
214 { $description "Reverses the result of a " { $link surround } " call, stripping off the prefix " { $snippet "seq2" } " and suffix " { $snippet "seq3" } " to restore the original sequence " { $snippet "seq" } "." }
215 { $see-also surround } ;
216
217 HELP: start-all
218 { $values
219      { "subseq" sequence } { "seq" sequence } { "indices" sequence } }
220 { $description "Outputs the starting indices of the non-overlapping occurrences of " { $snippet "subseq" } " in " { $snippet "seq" } "." }
221 { $examples
222     { $example "USING: prettyprint sequences.extras ; \"ABA\" \"ABABA\" start-all ."
223                "{ 0 }"
224     }
225     { $example "USING: prettyprint sequences.extras ; \"ABA\" \"ABAABA\" start-all ."
226       "{ 0 3 }"
227     }
228  } ;
229
230 HELP: start-all*
231 { $values
232     { "subseq" sequence } { "seq" sequence } { "indices" sequence } }
233 { $description "Outputs the starting indices of the possibly overlapping occurrences of " { $snippet "subseq" } " in " { $snippet "seq" } "." }
234 { $examples
235     { $example "USING: prettyprint sequences.extras ; \"ABA\" \"ABABA\" start-all* ."
236                "{ 0 2 }"
237     } } ;
238
239 HELP: arg-max
240 { $values { "seq" sequence } { "n" integer } }
241 { $description "Outputs the sequence with the largest item." } ;
242
243 HELP: arg-min
244 { $values { "seq" sequence } { "n" integer } }
245 { $description "Outputs the sequence with the smallest item." } ;
246
247 { arg-max arg-min } related-words
248
249 HELP: count-subseq
250 { $values
251     { "subseq" sequence } { "seq" sequence } { "n" integer } }
252 { $description "Outputs the number of non-overlapping occurrences of " { $snippet "subseq" } " in " { $snippet "seq" } "." }
253 { $examples
254     { $example "USING: prettyprint sequences.extras ; \"ABA\" \"ABABA\" count-subseq ."
255                "1"
256     } } ;
257
258
259 HELP: count-subseq*
260 { $values
261     { "subseq" sequence } { "seq" sequence } { "n" integer } }
262 { $description "Outputs the number of possibly overlapping occurrences of " { $snippet "subseq" } " in " { $snippet "seq" } "." }
263 { $examples
264     { $example "USING: prettyprint sequences.extras ; \"ABA\" \"ABABA\" count-subseq* ."
265                "2"
266     } } ;
267
268 { start-all start-all* count-subseq count-subseq* } related-words
269
270 HELP: loop>array
271 { $values
272     { "quot" quotation }
273     { "array" array }
274 }
275 { $description "Call the " { $snippet "quot" } ", which should output an object or " { $snippet "f" } ", and collect the objects in " { $snippet "array" } " until " { $snippet "quot" } " outputs " { $snippet "f" } "." }
276 { $examples
277     { $example "USING: sequences.extras prettyprint io.encodings.binary"
278     "io.streams.byte-array io ;"
279         "B{ 10 20 30 } binary ["
280         "   [ read1 ] loop>array"
281         "] with-byte-reader ."
282         "{ 10 20 30 }"
283     }
284 } ;
285
286 HELP: loop>array*
287 { $values
288     { "quot" quotation }
289     { "array" array }
290 }
291 { $description "Call the " { $snippet "quot" } ", which should output an object and a " { $snippet "bool" } ", and collect the objects in " { $snippet "array" } " until " { $snippet "quot" } " outputs " { $snippet "f" } ". Do collect the last object." }
292 { $examples
293     { $example "USING: sequences.extras prettyprint io.encodings.binary"
294                "random random.mersenne-twister kernel math ;"
295     "123 <mersenne-twister> ["
296     "   ["
297     "      10 random dup 5 >"
298     "   ] loop>array* ."
299     "] with-random"
300     "{ 6 7 2 }"
301     }
302 } ;
303
304 HELP: loop>array**
305 { $values
306     { "quot" quotation }
307     { "array" array }
308 }
309 { $description "Call the " { $snippet "quot" } ", which should output an object and a " { $snippet "bool" } ", and collect the objects in " { $snippet "array" } " until " { $snippet "quot" } " outputs " { $snippet "f" } ". Do not collect the last object." }
310 { $examples
311     { $example "USING: sequences.extras prettyprint io.encodings.binary"
312                "random random.mersenne-twister kernel math ;"
313     "123 <mersenne-twister> ["
314     "   ["
315     "      10 random dup 5 >"
316     "   ] loop>array** ."
317     "] with-random"
318     "{ 6 7 }"
319     }
320 } ;
321
322
323 HELP: loop>sequence
324 { $values
325     { "quot" quotation } { "exemplar" object }
326     { "seq" sequence }
327 }
328 { $description "Call " { $snippet "quot" } ", which should output an object or " { $snippet "f" } ", and collect the objects in " { $snippet "seq" } " of type " { $snippet "exemplar" } " until " { $snippet "quot" } " outputs " { $snippet "f" } "." }
329 { $examples
330     { $example "USING: sequences.extras prettyprint io.encodings.binary"
331     "io.streams.byte-array io ;"
332         "B{ 10 20 30 } binary ["
333         "   [ read1 ] V{ } loop>sequence"
334         "] with-byte-reader ."
335         "V{ 10 20 30 }"
336     }
337 } ;
338
339 HELP: loop>sequence*
340 { $values
341     { "quot" quotation } { "exemplar" object }
342     { "seq" sequence }
343 }
344 { $description "Call " { $snippet "quot" } ", which should output an object and a " { $snippet "bool" } ", and collect the objects in " { $snippet "seq" } " of type " { $snippet "exemplar" } " until " { $snippet "quot" } " outputs " { $snippet "f" } ". Do collect the last object." }
345 { $examples
346     { $example "USING: sequences.extras prettyprint io.encodings.binary"
347                "random random.mersenne-twister kernel math ;"
348     "! Get random numbers until one of them is greater than 5"
349     "! but also output the last number"
350     "123 <mersenne-twister> ["
351     "   ["
352     "      10 random dup 5 >"
353     "   ] V{ } loop>sequence*"
354     "] with-random ."
355     "V{ 6 7 2 }"
356     }
357 } ;
358
359 HELP: loop>sequence**
360 { $values
361     { "quot" quotation } { "exemplar" object }
362     { "seq" sequence }
363 }
364 { $description "Call " { $snippet "quot" } ", which should output an object and a " { $snippet "bool" } ", and collect the objects in " { $snippet "seq" } " of type " { $snippet "exemplar" } " until " { $snippet "quot" } " outputs " { $snippet "f" } ". Do not collect the last object." }
365 { $examples
366     { $example "USING: sequences.extras prettyprint io.encodings.binary"
367                "random random.mersenne-twister kernel math ;"
368     "! Get random numbers until one of them is greater than 5"
369     "! but also output the last number"
370     "123 <mersenne-twister> ["
371     "   ["
372     "      10 random dup 5 >"
373     "   ] V{ } loop>sequence**"
374     "] with-random ."
375     "V{ 6 7 }"
376     }
377 } ;
378
379 {
380     loop>array loop>array* loop>array**
381     loop>sequence loop>sequence* loop>sequence**
382     zero-loop>array zero-loop>sequence
383 } related-words
384
385 HELP: zero-loop>array
386 { $values
387     { "quot" quotation }
388     { "seq" sequence }
389 }
390 { $description "Call " { $snippet "quot" } ", which takes an integer starting from zero and incrementing on every loop, and should output an object, and collect the objects in " { $snippet "array" } " until " { $snippet "quot" } " outputs " { $snippet "f" } "." }
391 { $examples
392     "Example:"
393     { $example "USING: sequences.extras prettyprint math.text.english math kernel ;"
394         "[ dup 5 < [ number>text ] [ drop f ] if ] zero-loop>array ."
395         [[ { "zero" "one" "two" "three" "four" }]]
396     }
397 } ;
398
399 HELP: zero-loop>sequence
400 { $values
401     { "quot" quotation } { "exemplar" object }
402     { "seq" sequence }
403 }
404 { $description "Call the " { $snippet "quot" } ", which takes an integer starting from zero and incrementing on every loop, and should output an object or " { $snippet "f" } ", and collect the objects in " { $snippet "array" } " until " { $snippet "quot" } " outputs " { $snippet "f" } "." }
405 { $examples
406     "Example:"
407     { $example "USING: sequences.extras prettyprint math.text.english math kernel ;"
408         "[ dup 5 < [ number>text ] [ drop f ] if ] V{ } zero-loop>sequence ."
409         [[ V{ "zero" "one" "two" "three" "four" }]]
410     }
411 } ;