]> gitweb.factorcode.org Git - factor.git/blob - extra/sequences/lib/lib.factor
Fixing basis -> extra dependencies
[factor.git] / extra / sequences / lib / lib.factor
1 ! Copyright (C) 2007 Slava Pestov, Chris Double, Doug Coleman,
2 !                    Eduardo Cavazos, Daniel Ehrenberg.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: combinators.lib kernel sequences math namespaces assocs 
5 random sequences.private shuffle math.functions
6 arrays math.parser math.private sorting strings ascii macros
7 assocs.lib quotations hashtables math.order locals
8 generalizations ;
9 IN: sequences.lib
10
11 : each-withn ( seq quot n -- ) nwith each ; inline
12
13 : each-with ( seq quot -- ) with each ; inline
14
15 : each-with2 ( obj obj list quot -- ) 2 each-withn ; inline
16
17 : map-withn ( seq quot n -- newseq ) nwith map ; inline
18
19 : map-with ( seq quot -- ) with map ; inline
20
21 : map-with2 ( obj obj list quot -- newseq ) 2 map-withn ; inline
22
23 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
24
25 : each-percent ( seq quot -- )
26   >r
27   dup length
28   dup [ / ] curry
29   [ 1+ ] prepose
30   r> compose
31   2each ;                       inline
32
33 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
34
35 : map-reduce ( seq map-quot reduce-quot -- result )
36     >r [ unclip ] dip [ call ] keep r> compose reduce ; inline
37
38 : reduce* ( seq quot -- result ) [ ] swap map-reduce ; inline
39
40 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
41
42 : higher ( a b quot -- c ) [ compare +gt+ eq? ] curry most ; inline
43
44 : lower  ( a b quot -- c ) [ compare +lt+ eq? ] curry most ; inline
45
46 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
47
48 : longer  ( a b -- c ) [ length ] higher ;
49
50 : shorter ( a b -- c ) [ length ] lower ;
51
52 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
53
54 : longest ( seq -- item ) [ longer ] reduce* ;
55
56 : shortest ( seq -- item ) [ shorter ] reduce* ;
57
58 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
59
60 : bigger ( a b -- c ) [ ] higher ;
61
62 : smaller ( a b -- c ) [ ] lower ;
63
64 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
65
66 : biggest ( seq -- item ) [ bigger ] reduce* ;
67
68 : smallest ( seq -- item ) [ smaller ] reduce* ;
69
70 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
71
72 : minmax ( seq -- min max )
73     #! find the min and max of a seq in one pass
74     1/0. -1/0. rot [ tuck max >r min r> ] each ;
75
76 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
77
78 : ,, ( obj -- ) building get peek push ;
79 : v, ( -- ) V{ } clone , ;
80 : ,v ( -- ) building get dup peek empty? [ dup pop* ] when drop ;
81
82 : (monotonic-split) ( seq quot -- newseq )
83     [
84         >r dup unclip suffix r>
85         v, [ pick ,, call [ v, ] unless ] curry 2each ,v
86     ] { } make ;
87
88 : monotonic-split ( seq quot -- newseq )
89     over empty? [ 2drop { } ] [ (monotonic-split) ] if ;
90
91 ERROR: element-not-found ;
92 : split-around ( seq quot -- before elem after )
93     dupd find over [ element-not-found ] unless
94     >r cut rest r> swap ; inline
95
96 : (map-until) ( quot pred -- quot )
97     [ dup ] swap 3compose
98     [ [ drop t ] [ , f ] if ] compose [ find 2drop ] curry ;
99
100 : map-until ( seq quot pred -- newseq )
101     (map-until) { } make ;
102
103 : take-while ( seq quot -- newseq )
104     [ not ] compose
105     [ find drop [ head-slice ] when* ] curry
106     [ dup ] prepose keep like ;
107
108 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
109
110 <PRIVATE
111 : translate-string ( n alphabet out-len -- seq )
112     [ drop /mod ] with map nip  ;
113
114 : map-alphabet ( alphabet seq[seq] -- seq[seq] )
115     [ [ swap nth ] with map ] with map ;
116
117 : exact-number-strings ( n out-len -- seqs )
118     [ ^ ] 2keep [ translate-string ] 2curry map ;
119
120 : number-strings ( n max-length -- seqs )
121     1+ [ exact-number-strings ] with map concat ;
122 PRIVATE>
123
124 : exact-strings ( alphabet length -- seqs )
125     >r dup length r> exact-number-strings map-alphabet ;
126
127 : strings ( alphabet length -- seqs )
128     >r dup length r> number-strings map-alphabet ;
129
130 : switches ( seq1 seq -- subseq )
131     ! seq1 is a sequence of ones and zeroes
132     >r [ length ] keep [ nth 1 = ] curry filter r>
133     [ nth ] curry { } map-as ;
134
135 : power-set ( seq -- subsets )
136     2 over length exact-number-strings swap [ switches ] curry map ;
137
138 : push-either ( elt quot accum1 accum2 -- )
139     >r >r keep swap r> r> ? push ; inline
140
141 : 2pusher ( quot -- quot accum1 accum2 )
142     V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
143
144 : partition ( seq quot -- trueseq falseseq )
145     over >r 2pusher >r >r each r> r> r> drop ; inline
146
147 : cut-find ( seq pred -- before after )
148     dupd find drop dup [ cut ] when ;
149
150 : cut3 ( seq pred -- first mid last )
151     [ cut-find ] keep [ not ] compose cut-find ;
152
153 : (cut-all) ( seq pred quot -- )
154     [ >r cut3 r> dip >r >r , r> [ , ] when* r> ] 2keep
155     pick [ (cut-all) ] [ 3drop ] if ;
156
157 : cut-all ( seq pred quot -- first mid last )
158     [ (cut-all) ] { } make ;
159
160 : human-sort ( seq -- newseq )
161     [ dup [ digit? ] [ string>number ] cut-all ] { } map>assoc
162     sort-values keys ;
163
164 : ?first ( seq -- first/f ) 0 swap ?nth ; inline
165 : ?second ( seq -- second/f ) 1 swap ?nth ; inline
166 : ?third ( seq -- third/f ) 2 swap ?nth ; inline
167 : ?fourth ( seq -- fourth/f ) 3 swap ?nth ; inline
168
169 : ?first2 ( seq -- 1st/f 2nd/f ) dup ?first swap ?second ; inline
170 : ?first3 ( seq -- 1st/f 2nd/f 3rd/f ) dup ?first2 rot ?third ; inline
171 : ?first4 ( seq -- 1st/f 2nd/f 3rd/f 4th/f ) dup ?first3 roll ?fourth ; inline
172
173 USE: continuations
174 : ?subseq ( from to seq -- subseq )
175     >r >r 0 max r> r>
176     [ length tuck min >r min r> ] keep subseq ;
177
178 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
179
180 ! List the positions of obj in seq
181
182 : indices ( seq obj -- seq )
183   >r dup length swap r>
184   [ = [ ] [ drop f ] if ] curry
185   2map
186   sift ;
187
188 <PRIVATE
189 : (attempt-each-integer) ( i n quot -- result )
190     [
191         iterate-step roll
192         [ 3nip ] [ iterate-next (attempt-each-integer) ] if*
193     ] [ 3drop f ] if-iterate? ; inline recursive
194 PRIVATE>
195
196 : attempt-each ( seq quot -- result )
197     (each) iterate-prep (attempt-each-integer) ; inline
198
199 : ?nth* ( n seq -- elt/f ? )
200     2dup bounds-check? [ nth-unsafe t ] [ 2drop f f ] if ; flushable
201
202 : if-seq ( seq quot1 quot2 -- ) [ f like ] 2dip if* ; inline
203  
204 : if-empty ( seq quot1 quot2 -- ) swap if-seq ; inline
205
206 : when-empty ( seq quot1 -- ) [ ] if-empty ; inline
207
208 : unless-empty ( seq quot1 -- ) [ ] swap if-empty ; inline
209