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 make
5 assocs random sequences.private shuffle math.functions arrays
6 math.parser math.private sorting strings ascii macros assocs.lib
7 quotations hashtables math.order locals generalizations ;
10 : each-withn ( seq quot n -- ) nwith each ; inline
12 : each-with ( seq quot -- ) with each ; inline
14 : each-with2 ( obj obj list quot -- ) 2 each-withn ; inline
16 : map-withn ( seq quot n -- newseq ) nwith map ; inline
18 : map-with ( seq quot -- ) with map ; inline
20 : map-with2 ( obj obj list quot -- newseq ) 2 map-withn ; inline
22 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
24 : each-percent ( seq quot -- )
32 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
34 : map-reduce ( seq map-quot reduce-quot -- result )
35 >r [ unclip ] dip [ call ] keep r> compose reduce ; inline
37 : reduce* ( seq quot -- result ) [ ] swap map-reduce ; inline
39 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
41 : higher ( a b quot -- c ) [ compare +gt+ eq? ] curry most ; inline
43 : lower ( a b quot -- c ) [ compare +lt+ eq? ] curry most ; inline
45 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
47 : longer ( a b -- c ) [ length ] higher ;
49 : shorter ( a b -- c ) [ length ] lower ;
51 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
53 : longest ( seq -- item ) [ longer ] reduce* ;
55 : shortest ( seq -- item ) [ shorter ] reduce* ;
57 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
59 : bigger ( a b -- c ) [ ] higher ;
61 : smaller ( a b -- c ) [ ] lower ;
63 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
65 : biggest ( seq -- item ) [ bigger ] reduce* ;
67 : smallest ( seq -- item ) [ smaller ] reduce* ;
69 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
71 : minmax ( seq -- min max )
72 #! find the min and max of a seq in one pass
73 1/0. -1/0. rot [ tuck max >r min r> ] each ;
75 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
77 : ,, ( obj -- ) building get peek push ;
78 : v, ( -- ) V{ } clone , ;
79 : ,v ( -- ) building get dup peek empty? [ dup pop* ] when drop ;
81 : (monotonic-split) ( seq quot -- newseq )
83 >r dup unclip suffix r>
84 v, [ pick ,, call [ v, ] unless ] curry 2each ,v
87 : monotonic-split ( seq quot -- newseq )
88 over empty? [ 2drop { } ] [ (monotonic-split) ] if ;
90 ERROR: element-not-found ;
91 : split-around ( seq quot -- before elem after )
92 dupd find over [ element-not-found ] unless
93 >r cut rest r> swap ; inline
95 : (map-until) ( quot pred -- quot )
97 [ [ drop t ] [ , f ] if ] compose [ find 2drop ] curry ;
99 : map-until ( seq quot pred -- newseq )
100 (map-until) { } make ;
102 : take-while ( seq quot -- newseq )
104 [ find drop [ head-slice ] when* ] curry
105 [ dup ] prepose keep like ;
107 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
110 : translate-string ( n alphabet out-len -- seq )
111 [ drop /mod ] with map nip ;
113 : map-alphabet ( alphabet seq[seq] -- seq[seq] )
114 [ [ swap nth ] with map ] with map ;
116 : exact-number-strings ( n out-len -- seqs )
117 [ ^ ] 2keep [ translate-string ] 2curry map ;
119 : number-strings ( n max-length -- seqs )
120 1+ [ exact-number-strings ] with map concat ;
123 : exact-strings ( alphabet length -- seqs )
124 >r dup length r> exact-number-strings map-alphabet ;
126 : strings ( alphabet length -- seqs )
127 >r dup length r> number-strings map-alphabet ;
129 : switches ( seq1 seq -- subseq )
130 ! seq1 is a sequence of ones and zeroes
131 >r [ length ] keep [ nth 1 = ] curry filter r>
132 [ nth ] curry { } map-as ;
134 : power-set ( seq -- subsets )
135 2 over length exact-number-strings swap [ switches ] curry map ;
137 : cut-find ( seq pred -- before after )
138 dupd find drop dup [ cut ] when ;
140 : cut3 ( seq pred -- first mid last )
141 [ cut-find ] keep [ not ] compose cut-find ;
143 : (cut-all) ( seq pred quot -- )
144 [ >r cut3 r> dip >r >r , r> [ , ] when* r> ] 2keep
145 pick [ (cut-all) ] [ 3drop ] if ;
147 : cut-all ( seq pred quot -- first mid last )
148 [ (cut-all) ] { } make ;
150 : human-sort ( seq -- newseq )
151 [ dup [ digit? ] [ string>number ] cut-all ] { } map>assoc
154 : ?first ( seq -- first/f ) 0 swap ?nth ; inline
155 : ?second ( seq -- second/f ) 1 swap ?nth ; inline
156 : ?third ( seq -- third/f ) 2 swap ?nth ; inline
157 : ?fourth ( seq -- fourth/f ) 3 swap ?nth ; inline
159 : ?first2 ( seq -- 1st/f 2nd/f ) dup ?first swap ?second ; inline
160 : ?first3 ( seq -- 1st/f 2nd/f 3rd/f ) dup ?first2 rot ?third ; inline
161 : ?first4 ( seq -- 1st/f 2nd/f 3rd/f 4th/f ) dup ?first3 roll ?fourth ; inline
164 : ?subseq ( from to seq -- subseq )
166 [ length tuck min >r min r> ] keep subseq ;
168 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
170 ! List the positions of obj in seq
172 : indices ( seq obj -- seq )
173 >r dup length swap r>
174 [ = [ ] [ drop f ] if ] curry
179 : (attempt-each-integer) ( i n quot -- result )
182 [ 3nip ] [ iterate-next (attempt-each-integer) ] if*
183 ] [ 3drop f ] if-iterate? ; inline recursive
186 : attempt-each ( seq quot -- result )
187 (each) iterate-prep (attempt-each-integer) ; inline
189 : ?nth* ( n seq -- elt/f ? )
190 2dup bounds-check? [ nth-unsafe t ] [ 2drop f f ] if ; flushable