]> gitweb.factorcode.org Git - factor.git/blob - basis/grouping/grouping-docs.factor
Merge branch 'master' of git://factorcode.org/git/factor
[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
27 ABOUT: "grouping"
28
29 HELP: groups
30 { $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."
31 $nl
32 "New groups are created by calling " { $link <groups> } " and " { $link <sliced-groups> } "." } ;
33
34 HELP: group
35 { $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "array" "a sequence of sequences" } }
36 { $description "Splits the sequence into disjoint groups of " { $snippet "n" } " elements and collects the groups into a new array." }
37 { $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." }
38 { $examples
39     { $example "USING: grouping prettyprint ;" "{ 3 1 3 3 7 } 2 group ." "{ { 3 1 } { 3 3 } { 7 } }" }
40 } ;
41
42 HELP: <groups>
43 { $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "groups" groups } }
44 { $description "Outputs a virtual sequence whose elements are disjoint subsequences of " { $snippet "n" } " elements from the underlying sequence." }
45 { $examples
46     { $example
47         "USING: arrays kernel prettyprint sequences grouping ;"
48         "9 >array 3 <groups> dup reverse-here concat >array ." "{ 6 7 8 3 4 5 0 1 2 }"
49     }
50     { $example
51         "USING: kernel prettyprint sequences grouping ;"
52         "{ 1 2 3 4 5 6 } 3 <groups> first ."
53         "{ 1 2 3 }"
54     }
55 } ;
56
57 HELP: <sliced-groups>
58 { $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "groups" groups } }
59 { $description "Outputs a virtual sequence whose elements are slices of disjoint subsequences of " { $snippet "n" } " elements from the underlying sequence." }
60 { $examples
61     { $example
62         "USING: arrays kernel prettyprint sequences grouping ;"
63         "9 >array 3 <sliced-groups>"
64         "dup [ reverse-here ] each concat >array ."
65         "{ 2 1 0 5 4 3 8 7 6 }"
66     }
67     { $example
68         "USING: kernel prettyprint sequences grouping ;"
69         "{ 1 2 3 4 5 6 } 3 <sliced-groups> second ."
70         "T{ slice { from 3 } { to 6 } { seq { 1 2 3 4 5 6 } } }"
71     }
72 } ;
73
74 HELP: clumps
75 { $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."
76 $nl
77 "New clumps are created by calling " { $link <clumps> } " and " { $link <sliced-clumps> } "." } ;
78
79 HELP: clump
80 { $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "array" "a sequence of sequences" } }
81 { $description "Splits the sequence into overlapping clumps of " { $snippet "n" } " elements and collects the clumps into a new array." }
82 { $errors "Throws an error if " { $snippet "n" } " is smaller than the length of the sequence." }
83 { $examples
84     { $example "USING: grouping prettyprint ;" "{ 3 1 3 3 7 } 2 clump ." "{ { 3 1 } { 1 3 } { 3 3 } { 3 7 } }" }
85 } ;
86
87 HELP: <clumps>
88 { $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "clumps" clumps } }
89 { $description "Outputs a virtual sequence whose elements are overlapping subsequences of " { $snippet "n" } " elements from the underlying sequence." }
90 { $examples
91     "Running averages:"
92     { $example
93         "USING: grouping sequences math prettyprint kernel ;"
94         "IN: scratchpad"
95         ": share-price"
96         "    { 13/50 51/100 13/50 1/10 4/5 17/20 33/50 3/25 19/100 3/100 } ;"
97         ""
98         "share-price 4 <clumps> [ [ sum ] [ length ] bi / ] map ."
99         "{ 113/400 167/400 201/400 241/400 243/400 91/200 1/4 }"
100     }
101     { $example
102         "USING: kernel sequences grouping prettyprint ;"
103         "{ 1 2 3 4 5 6 } 3 <clumps> second ."
104         "{ 2 3 4 }"
105     }
106 } ;
107
108 HELP: <sliced-clumps>
109 { $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "clumps" clumps } }
110 { $description "Outputs a virtual sequence whose elements are overlapping slices of " { $snippet "n" } " elements from the underlying sequence." }
111 { $examples
112     { $example
113         "USING: kernel sequences grouping prettyprint ;"
114         "{ 1 2 3 4 5 6 } 3 <sliced-clumps> second ."
115         "T{ slice { from 1 } { to 4 } { seq { 1 2 3 4 5 6 } } }"
116     }
117 } ;
118
119 { clumps groups } related-words
120
121 { clump group } related-words
122
123 { <clumps> <groups> } related-words
124
125 { <sliced-clumps> <sliced-groups> } related-words