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 : split-around ( seq quot -- before elem after )
68 dupd find over [ "Element not found" throw ] unless
69 >r cut-slice 1 tail r> swap ; inline
71 : (map-until) ( quot pred -- quot )
73 [ [ drop t ] [ , f ] if ] compose [ find 2drop ] curry ;
75 : map-until ( seq quot pred -- newseq )
76 (map-until) { } make ;
78 : take-while ( seq quot -- newseq )
80 [ find drop [ head-slice ] when* ] curry
81 [ dup ] swap compose keep like ;
83 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
86 : translate-string ( n alphabet out-len -- seq )
87 [ drop /mod ] curry* map nip ;
89 : map-alphabet ( alphabet seq[seq] -- seq[seq] )
90 [ [ swap nth ] curry* map ] curry* map ;
92 : exact-number-strings ( n out-len -- seqs )
93 [ ^ ] 2keep [ translate-string ] 2curry map ;
95 : number-strings ( n max-length -- seqs )
96 1+ [ exact-number-strings ] curry* map concat ;
99 : exact-strings ( alphabet length -- seqs )
100 >r dup length r> exact-number-strings map-alphabet ;
102 : strings ( alphabet length -- seqs )
103 >r dup length r> number-strings map-alphabet ;
105 : nths ( nths seq -- subseq )
106 ! nths is a sequence of ones and zeroes
107 >r [ length ] keep [ nth 1 = ] curry subset r>
108 [ nth ] curry { } map-as ;
110 : power-set ( seq -- subsets )
111 2 over length exact-number-strings swap [ nths ] curry map ;
113 : cut-find ( seq pred -- before after )
114 dupd find drop dup [ cut ] when ;
116 : cut3 ( seq pred -- first mid last )
117 [ cut-find ] keep [ not ] compose cut-find ;
119 : (cut-all) ( seq pred quot -- )
120 [ >r cut3 r> dip >r >r , r> [ , ] when* r> ] 2keep
121 pick [ (cut-all) ] [ 3drop ] if ;
123 : cut-all ( seq pred quot -- first mid last )
124 [ (cut-all) ] { } make ;
126 : human-sort ( seq -- newseq )
127 [ dup [ digit? ] [ string>number ] cut-all ] { } map>assoc