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