1 USING: combinators.lib kernel sequences math namespaces assocs
2 random sequences.private shuffle math.functions mirrors ;
3 USING: arrays math.parser sorting strings ;
6 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
8 : map-reduce ( seq map-quot reduce-quot -- result )
9 >r [ unclip ] dip [ call ] keep r> compose reduce ; inline
11 : reduce* ( seq quot -- result ) [ ] swap map-reduce ; inline
13 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
15 : higher ( a b quot -- c ) [ compare 0 > ] curry most ; inline
17 : lower ( a b quot -- c ) [ compare 0 < ] curry most ; inline
19 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21 : longer ( a b -- c ) [ length ] higher ;
23 : shorter ( a b -- c ) [ length ] lower ;
25 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
27 : longest ( seq -- item ) [ longer ] reduce* ;
29 : shortest ( seq -- item ) [ shorter ] reduce* ;
31 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
33 : bigger ( a b -- c ) [ ] higher ;
35 : smaller ( a b -- c ) [ ] lower ;
37 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
39 : biggest ( seq -- item ) [ bigger ] reduce* ;
41 : smallest ( seq -- item ) [ smaller ] reduce* ;
43 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
45 : minmax ( seq -- min max )
46 #! find the min and max of a seq in one pass
47 1/0. -1/0. rot [ tuck max >r min r> ] each ;
49 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
51 : ,, building get peek push ;
53 : ,v building get dup peek empty? [ dup pop* ] when drop ;
55 : monotonic-split ( seq quot -- newseq )
58 v, [ pick ,, call [ v, ] unless ] curry 2each ,v
61 : singleton? ( seq -- ? )
64 : delete-random ( seq -- value )
65 [ length random ] keep [ nth ] 2keep delete-nth ;
67 : (map-until) ( quot pred -- quot )
69 [ [ drop t ] [ , f ] if ] compose [ find 2drop ] curry ;
71 : map-until ( seq quot pred -- newseq )
72 (map-until) { } make ;
74 : take-while ( seq quot -- newseq )
76 [ find drop [ head-slice ] when* ] curry
77 [ dup ] swap compose keep like ;
79 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
82 : translate-string ( n alphabet out-len -- seq )
83 [ drop /mod ] curry* map nip ;
85 : map-alphabet ( alphabet seq[seq] -- seq[seq] )
86 [ [ swap nth ] curry* map ] curry* map ;
88 : exact-number-strings ( n out-len -- seqs )
89 [ ^ ] 2keep [ translate-string ] 2curry map ;
91 : number-strings ( n max-length -- seqs )
92 1+ [ exact-number-strings ] curry* map concat ;
95 : exact-strings ( alphabet length -- seqs )
96 >r dup length r> exact-number-strings map-alphabet ;
98 : strings ( alphabet length -- seqs )
99 >r dup length r> number-strings map-alphabet ;
101 : nths ( nths seq -- subseq )
102 ! nths is a sequence of ones and zeroes
103 >r [ length ] keep [ nth 1 = ] curry subset r>
104 [ nth ] curry { } map-as ;
106 : power-set ( seq -- subsets )
107 2 over length exact-number-strings swap [ nths ] curry map ;
109 : cut-find ( seq pred -- before after )
110 dupd find drop dup [ cut ] when ;
112 : cut3 ( seq pred -- first mid last )
113 [ cut-find ] keep [ not ] compose cut-find ;
115 : (cut-all) ( seq pred quot -- )
116 [ >r cut3 r> dip >r >r , r> [ , ] when* r> ] 2keep
117 pick [ (cut-all) ] [ 3drop ] if ;
119 : cut-all ( seq pred quot -- first mid last )
120 [ (cut-all) ] { } make ;
122 : human-sort ( seq -- newseq )
123 [ dup [ digit? ] [ string>number ] cut-all ] { } map>assoc