1 USING: combinators.lib kernel sequences math namespaces assocs
2 random sequences.private shuffle math.functions mirrors ;
5 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7 : map-reduce ( seq map-quot reduce-quot -- result )
8 >r [ unclip ] dip [ call ] keep r> compose reduce ; inline
10 : reduce* ( seq quot -- result ) [ ] swap map-reduce ; inline
12 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14 : higher ( a b quot -- c ) [ compare 0 > ] curry most ; inline
16 : lower ( a b quot -- c ) [ compare 0 < ] curry most ; inline
18 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
20 : longer ( a b -- c ) [ length ] higher ;
22 : shorter ( a b -- c ) [ length ] lower ;
24 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
26 : longest ( seq -- item ) [ longer ] reduce* ;
28 : shortest ( seq -- item ) [ shorter ] reduce* ;
30 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
32 : bigger ( a b -- c ) [ ] higher ;
34 : smaller ( a b -- c ) [ ] lower ;
36 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
38 : biggest ( seq -- item ) [ bigger ] reduce* ;
40 : smallest ( seq -- item ) [ smaller ] reduce* ;
42 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
44 : minmax ( seq -- min max )
45 #! find the min and max of a seq in one pass
46 1/0. -1/0. rot [ tuck max >r min r> ] each ;
48 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
50 : ,, building get peek push ;
52 : ,v building get dup peek empty? [ dup pop* ] when drop ;
54 : monotonic-split ( seq quot -- newseq )
57 v, [ pick ,, call [ v, ] unless ] curry 2each ,v
60 : singleton? ( seq -- ? )
63 : delete-random ( seq -- value )
64 [ length random ] keep [ nth ] 2keep delete-nth ;
66 : (map-until) ( quot pred -- quot )
68 [ [ drop t ] [ , f ] if ] compose [ find 2drop ] curry ;
70 : map-until ( seq quot pred -- newseq )
71 (map-until) { } make ;
73 : take-while ( seq quot -- newseq )
75 [ find drop [ head-slice ] when* ] curry
76 [ dup ] swap compose keep like ;
78 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
81 : translate-string ( n alphabet out-len -- seq )
82 [ drop /mod ] curry* map nip ;
84 : map-alphabet ( alphabet seq[seq] -- seq[seq] )
85 [ [ swap nth ] curry* map ] curry* map ;
87 : exact-number-strings ( n out-len -- seqs )
88 [ ^ ] 2keep [ translate-string ] 2curry map ;
90 : number-strings ( n max-length -- seqs )
91 1+ [ exact-number-strings ] curry* map concat ;
94 : exact-strings ( alphabet length -- seqs )
95 >r dup length r> exact-number-strings map-alphabet ;
97 : strings ( alphabet length -- seqs )
98 >r dup length r> number-strings map-alphabet ;
100 : nths ( nths seq -- subseq )
101 ! nths is a sequence of ones and zeroes
102 >r [ length ] keep [ nth 1 = ] curry subset r>
103 [ nth ] curry { } map-as ;
105 : power-set ( seq -- subsets )
106 2 over length exact-number-strings swap [ nths ] curry map ;
108 : push-either ( elt quot accum1 accum2 -- )
109 >r >r keep swap r> r> ? push ; inline
111 : 2pusher ( quot -- quot accum1 accum2 )
112 V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
114 : partition ( seq quot -- trueseq falseseq )
115 over >r 2pusher >r >r each r> r> r> drop ; inline