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