]> gitweb.factorcode.org Git - factor.git/blob - basis/grouping/grouping-docs.factor
More doc fixes
[factor.git] / basis / grouping / grouping-docs.factor
1 USING: help.markup help.syntax sequences strings ;
2 IN: grouping
3
4 ARTICLE: "grouping" "Groups and clumps"
5 "Splitting a sequence into disjoint, fixed-length subsequences:"
6 { $subsection group }
7 "A virtual sequence for splitting a sequence into disjoint, fixed-length subsequences:"
8 { $subsection groups }
9 { $subsection <groups> }
10 { $subsection <sliced-groups> }
11 "Splitting a sequence into overlapping, fixed-length subsequences:"
12 { $subsection clump }
13 "A virtual sequence for splitting a sequence into overlapping, fixed-length subsequences:"
14 { $subsection clumps }
15 { $subsection <clumps> }
16 { $subsection <sliced-clumps> }
17 "The difference can be summarized as the following:"
18 { $list
19     { "With groups, the subsequences form the original sequence when concatenated:"
20         { $unchecked-example "dup n groups concat sequence= ." "t" }
21     }
22     { "With clumps, collecting the first element of each subsequence but the last one, together with the last subseqence, yields the original sequence:"
23         { $unchecked-example "dup n clumps unclip-last [ [ first ] map ] dip append sequence= ." "t" }
24     }
25 }
26 "A combinator built using clumps:"
27 { $subsection monotonic? }
28 "Testing how elements are related:"
29 { $subsection all-eq? }
30 { $subsection all-equal? } ;
31
32 ABOUT: "grouping"
33
34 HELP: groups
35 { $class-description "Instances are virtual sequences whose elements are disjoint fixed-length subsequences of an underlying sequence. Groups are mutable and resizable if the underlying sequence is mutable and resizable, respectively."
36 $nl
37 "New groups are created by calling " { $link <groups> } " and " { $link <sliced-groups> } "." } ;
38
39 HELP: group
40 { $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "array" "a sequence of sequences" } }
41 { $description "Splits the sequence into disjoint groups of " { $snippet "n" } " elements and collects the groups into a new array." }
42 { $notes "If the sequence length is not a multiple of " { $snippet "n" } ", the final subsequence in the list will be shorter than " { $snippet "n" } " elements." }
43 { $examples
44     { $example "USING: grouping prettyprint ;" "{ 3 1 3 3 7 } 2 group ." "{ { 3 1 } { 3 3 } { 7 } }" }
45 } ;
46
47 HELP: <groups>
48 { $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "groups" groups } }
49 { $description "Outputs a virtual sequence whose elements are disjoint subsequences of " { $snippet "n" } " elements from the underlying sequence." }
50 { $examples
51     { $example
52         "USING: arrays kernel prettyprint sequences grouping ;"
53         "9 >array 3 <groups> dup reverse-here concat >array ." "{ 6 7 8 3 4 5 0 1 2 }"
54     }
55     { $example
56         "USING: kernel prettyprint sequences grouping ;"
57         "{ 1 2 3 4 5 6 } 3 <groups> first ."
58         "{ 1 2 3 }"
59     }
60 } ;
61
62 HELP: <sliced-groups>
63 { $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "groups" groups } }
64 { $description "Outputs a virtual sequence whose elements are slices of disjoint subsequences of " { $snippet "n" } " elements from the underlying sequence." }
65 { $examples
66     { $example
67         "USING: arrays kernel prettyprint sequences grouping ;"
68         "9 >array 3 <sliced-groups>"
69         "dup [ reverse-here ] each concat >array ."
70         "{ 2 1 0 5 4 3 8 7 6 }"
71     }
72     { $example
73         "USING: kernel prettyprint sequences grouping ;"
74         "{ 1 2 3 4 5 6 } 3 <sliced-groups> second ."
75         "T{ slice { from 3 } { to 6 } { seq { 1 2 3 4 5 6 } } }"
76     }
77 } ;
78
79 HELP: clumps
80 { $class-description "Instances are virtual sequences whose elements are overlapping fixed-length subsequences o an underlying sequence. Clumps are mutable and resizable if the underlying sequence is mutable and resizable, respectively."
81 $nl
82 "New clumps are created by calling " { $link <clumps> } " and " { $link <sliced-clumps> } "." } ;
83
84 HELP: clump
85 { $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "array" "a sequence of sequences" } }
86 { $description "Splits the sequence into overlapping clumps of " { $snippet "n" } " elements and collects the clumps into a new array." }
87 { $errors "Throws an error if " { $snippet "n" } " is smaller than the length of the sequence." }
88 { $examples
89     { $example "USING: grouping prettyprint ;" "{ 3 1 3 3 7 } 2 clump ." "{ { 3 1 } { 1 3 } { 3 3 } { 3 7 } }" }
90 } ;
91
92 HELP: <clumps>
93 { $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "clumps" clumps } }
94 { $description "Outputs a virtual sequence whose elements are overlapping subsequences of " { $snippet "n" } " elements from the underlying sequence." }
95 { $examples
96     "Running averages:"
97     { $example
98         "USING: grouping sequences math prettyprint kernel ;"
99         "IN: scratchpad"
100         ": share-price"
101         "    { 13/50 51/100 13/50 1/10 4/5 17/20 33/50 3/25 19/100 3/100 } ;"
102         ""
103         "share-price 4 <clumps> [ [ sum ] [ length ] bi / ] map ."
104         "{ 113/400 167/400 201/400 241/400 243/400 91/200 1/4 }"
105     }
106     { $example
107         "USING: kernel sequences grouping prettyprint ;"
108         "{ 1 2 3 4 5 6 } 3 <clumps> second ."
109         "{ 2 3 4 }"
110     }
111 } ;
112
113 HELP: <sliced-clumps>
114 { $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "clumps" clumps } }
115 { $description "Outputs a virtual sequence whose elements are overlapping slices of " { $snippet "n" } " elements from the underlying sequence." }
116 { $examples
117     { $example
118         "USING: kernel sequences grouping prettyprint ;"
119         "{ 1 2 3 4 5 6 } 3 <sliced-clumps> second ."
120         "T{ slice { from 1 } { to 4 } { seq { 1 2 3 4 5 6 } } }"
121     }
122 } ;
123
124 { clumps groups } related-words
125
126 { clump group } related-words
127
128 { <clumps> <groups> } related-words
129
130 { <sliced-clumps> <sliced-groups> } related-words
131
132 HELP: monotonic?
133 { $values { "seq" sequence } { "quot" { $quotation "( elt elt -- ? )" } } { "?" "a boolean" } }
134 { $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." }
135 { $examples
136     "Testing if a sequence is non-decreasing:"
137     { $example "USING: grouping math prettyprint ;" "{ 1 1 2 } [ <= ] monotonic? ." "t" }
138     "Testing if a sequence is decreasing:"
139     { $example "USING: grouping math prettyprint ;" "{ 9 8 6 7 } [ < ] monotonic? ." "f" }
140 } ;
141
142 HELP: all-equal?
143 { $values { "seq" sequence } { "?" "a boolean" } }
144 { $description "Tests if all elements in the sequence are equal. Yields true with an empty sequence." } ;
145
146 HELP: all-eq?
147 { $values { "seq" sequence } { "?" "a boolean" } }
148 { $description "Tests if all elements in the sequence are the same identical object. Yields true with an empty sequence." } ;
149
150 { monotonic? all-eq? all-equal? } related-words