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
11 : each-withn ( seq quot n -- ) nwith each ; inline
13 : each-with ( seq quot -- ) with each ; inline
15 : each-with2 ( obj obj list quot -- ) 2 each-withn ; inline
17 : map-withn ( seq quot n -- newseq ) nwith map ; inline
19 : map-with ( seq quot -- ) with map ; inline
21 : map-with2 ( obj obj list quot -- newseq ) 2 map-withn ; inline
23 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
25 : each-percent ( seq quot -- )
33 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
35 : map-reduce ( seq map-quot reduce-quot -- result )
36 >r [ unclip ] dip [ call ] keep r> compose reduce ; inline
38 : reduce* ( seq quot -- result ) [ ] swap map-reduce ; inline
40 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
42 : higher ( a b quot -- c ) [ compare +gt+ eq? ] curry most ; inline
44 : lower ( a b quot -- c ) [ compare +lt+ eq? ] curry most ; inline
46 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
48 : longer ( a b -- c ) [ length ] higher ;
50 : shorter ( a b -- c ) [ length ] lower ;
52 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
54 : longest ( seq -- item ) [ longer ] reduce* ;
56 : shortest ( seq -- item ) [ shorter ] reduce* ;
58 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
60 : bigger ( a b -- c ) [ ] higher ;
62 : smaller ( a b -- c ) [ ] lower ;
64 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
66 : biggest ( seq -- item ) [ bigger ] reduce* ;
68 : smallest ( seq -- item ) [ smaller ] reduce* ;
70 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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 ;
76 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
78 : ,, ( obj -- ) building get peek push ;
79 : v, ( -- ) V{ } clone , ;
80 : ,v ( -- ) building get dup peek empty? [ dup pop* ] when drop ;
82 : (monotonic-split) ( seq quot -- newseq )
84 >r dup unclip suffix r>
85 v, [ pick ,, call [ v, ] unless ] curry 2each ,v
88 : monotonic-split ( seq quot -- newseq )
89 over empty? [ 2drop { } ] [ (monotonic-split) ] if ;
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
96 : (map-until) ( quot pred -- quot )
98 [ [ drop t ] [ , f ] if ] compose [ find 2drop ] curry ;
100 : map-until ( seq quot pred -- newseq )
101 (map-until) { } make ;
103 : take-while ( seq quot -- newseq )
105 [ find drop [ head-slice ] when* ] curry
106 [ dup ] prepose keep like ;
108 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
111 : translate-string ( n alphabet out-len -- seq )
112 [ drop /mod ] with map nip ;
114 : map-alphabet ( alphabet seq[seq] -- seq[seq] )
115 [ [ swap nth ] with map ] with map ;
117 : exact-number-strings ( n out-len -- seqs )
118 [ ^ ] 2keep [ translate-string ] 2curry map ;
120 : number-strings ( n max-length -- seqs )
121 1+ [ exact-number-strings ] with map concat ;
124 : exact-strings ( alphabet length -- seqs )
125 >r dup length r> exact-number-strings map-alphabet ;
127 : strings ( alphabet length -- seqs )
128 >r dup length r> number-strings map-alphabet ;
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 ;
135 : power-set ( seq -- subsets )
136 2 over length exact-number-strings swap [ switches ] curry map ;
138 : push-either ( elt quot accum1 accum2 -- )
139 >r >r keep swap r> r> ? push ; inline
141 : 2pusher ( quot -- quot accum1 accum2 )
142 V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
144 : partition ( seq quot -- trueseq falseseq )
145 over >r 2pusher >r >r each r> r> r> drop ; inline
147 : cut-find ( seq pred -- before after )
148 dupd find drop dup [ cut ] when ;
150 : cut3 ( seq pred -- first mid last )
151 [ cut-find ] keep [ not ] compose cut-find ;
153 : (cut-all) ( seq pred quot -- )
154 [ >r cut3 r> dip >r >r , r> [ , ] when* r> ] 2keep
155 pick [ (cut-all) ] [ 3drop ] if ;
157 : cut-all ( seq pred quot -- first mid last )
158 [ (cut-all) ] { } make ;
160 : human-sort ( seq -- newseq )
161 [ dup [ digit? ] [ string>number ] cut-all ] { } map>assoc
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
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
174 : ?subseq ( from to seq -- subseq )
176 [ length tuck min >r min r> ] keep subseq ;
178 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
180 ! List the positions of obj in seq
182 : indices ( seq obj -- seq )
183 >r dup length swap r>
184 [ = [ ] [ drop f ] if ] curry
189 : (attempt-each-integer) ( i n quot -- result )
192 [ 3nip ] [ iterate-next (attempt-each-integer) ] if*
193 ] [ 3drop f ] if-iterate? ; inline recursive
196 : attempt-each ( seq quot -- result )
197 (each) iterate-prep (attempt-each-integer) ; inline
199 : ?nth* ( n seq -- elt/f ? )
200 2dup bounds-check? [ nth-unsafe t ] [ 2drop f f ] if ; flushable
202 : if-seq ( seq quot1 quot2 -- ) [ f like ] 2dip if* ; inline
204 : if-empty ( seq quot1 quot2 -- ) swap if-seq ; inline
206 : when-empty ( seq quot1 -- ) [ ] if-empty ; inline
208 : unless-empty ( seq quot1 -- ) [ ] swap if-empty ; inline