]> gitweb.factorcode.org Git - factor.git/blob - extra/sequences/lib/lib.factor
XML fixes
[factor.git] / extra / sequences / lib / lib.factor
1 USING: combinators.lib kernel sequences math namespaces assocs 
2 random sequences.private shuffle math.functions mirrors ;
3 USING: arrays math.parser sorting strings ;
4 IN: sequences.lib
5
6 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7
8 : map-reduce ( seq map-quot reduce-quot -- result )
9     >r [ unclip ] dip [ call ] keep r> compose reduce ; inline
10
11 : reduce* ( seq quot -- result ) [ ] swap map-reduce ; inline
12
13 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14
15 : higher ( a b quot -- c ) [ compare 0 > ] curry most ; inline
16
17 : lower  ( a b quot -- c ) [ compare 0 < ] curry most ; inline
18
19 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
20
21 : longer  ( a b -- c ) [ length ] higher ;
22
23 : shorter ( a b -- c ) [ length ] lower ;
24
25 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
26
27 : longest ( seq -- item ) [ longer ] reduce* ;
28
29 : shortest ( seq -- item ) [ shorter ] reduce* ;
30
31 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
32
33 : bigger ( a b -- c ) [ ] higher ;
34
35 : smaller ( a b -- c ) [ ] lower ;
36
37 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
38
39 : biggest ( seq -- item ) [ bigger ] reduce* ;
40
41 : smallest ( seq -- item ) [ smaller ] reduce* ;
42
43 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
44
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 ;
48
49 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
50
51 : ,, building get peek push ;
52 : v, V{ } clone , ;
53 : ,v building get dup peek empty? [ dup pop* ] when drop ;
54
55 : monotonic-split ( seq quot -- newseq )
56     [
57         >r dup unclip add r>
58         v, [ pick ,, call [ v, ] unless ] curry 2each ,v
59     ] { } make ;
60
61 : singleton? ( seq -- ? )
62     length 1 = ;
63
64 : delete-random ( seq -- value )
65     [ length random ] keep [ nth ] 2keep delete-nth ;
66
67 : (map-until) ( quot pred -- quot )
68     [ dup ] swap 3compose
69     [ [ drop t ] [ , f ] if ] compose [ find 2drop ] curry ;
70
71 : map-until ( seq quot pred -- newseq )
72     (map-until) { } make ;
73
74 : take-while ( seq quot -- newseq )
75     [ not ] compose
76     [ find drop [ head-slice ] when* ] curry
77     [ dup ] swap compose keep like ;
78
79 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
80
81 <PRIVATE
82 : translate-string ( n alphabet out-len -- seq )
83     [ drop /mod ] curry* map nip  ;
84
85 : map-alphabet ( alphabet seq[seq] -- seq[seq] )
86     [ [ swap nth ] curry* map ] curry* map ;
87
88 : exact-number-strings ( n out-len -- seqs )
89     [ ^ ] 2keep [ translate-string ] 2curry map ;
90
91 : number-strings ( n max-length -- seqs )
92     1+ [ exact-number-strings ] curry* map concat ;
93 PRIVATE>
94
95 : exact-strings ( alphabet length -- seqs )
96     >r dup length r> exact-number-strings map-alphabet ;
97
98 : strings ( alphabet length -- seqs )
99     >r dup length r> number-strings map-alphabet ;
100
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 ;
105
106 : power-set ( seq -- subsets )
107     2 over length exact-number-strings swap [ nths ] curry map ;
108
109 : cut-find ( seq pred -- before after )
110     dupd find drop dup [ cut ] when ;
111
112 : cut3 ( seq pred -- first mid last )
113     [ cut-find ] keep [ not ] compose cut-find ;
114
115 : (cut-all) ( seq pred quot -- )
116     [ >r cut3 r> dip >r >r , r> [ , ] when* r> ] 2keep
117     pick [ (cut-all) ] [ 3drop ] if ;
118
119 : cut-all ( seq pred quot -- first mid last )
120     [ (cut-all) ] { } make ;
121
122 : human-sort ( seq -- newseq )
123     [ dup [ digit? ] [ string>number ] cut-all ] { } map>assoc
124     sort-values keys ;