]> gitweb.factorcode.org Git - factor.git/blob - unmaintained/adsoda/combinators/combinators.factor
tools.test: Make the flag public. Finish porting tester changes to fuzzer.
[factor.git] / unmaintained / adsoda / combinators / combinators.factor
1 ! Copyright (C) 2008 Jeff Bigot
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel arrays sequences fry math combinators ;
4
5 IN: adsoda.combinators
6
7 ! : (combinations) ( seq -- seq ) [ 1 tail ] dip combinations ;
8
9 ! : prefix-each [ prefix ] curry map ; inline
10
11 ! : combinations ( seq n -- seqs )
12 !    {
13 !        { [ dup 0 = ] [ 2drop { { } } ] }
14 !        { [ over empty? ] [ 2drop { } ] }
15 !        { [ t ] [ 
16 !            [ [ 1 - (combinations) ] [ drop first ] 2bi prefix-each ]
17 !            [ (combinations) ] 2bi append
18 !        ] }
19 !    } cond ;
20
21 : columnize ( array -- array ) [ 1array ] map ; inline
22
23 : among ( array n -- array )
24     2dup swap length 
25     {
26         { [ over 1 = ] [ 3drop columnize ] }
27         { [ over 0 = ] [ 4drop { } ] }
28         { [ 2dup < ] [ 2drop [ 1 cut ] dip
29                          [ 1 - among [ append ] with map ]
30                          [ among append ] 2bi
31                        ] }
32         { [ 2dup = ] [ 3drop 1array ] }
33         { [ 2dup > ] [ 4drop { } ] }
34     } cond
35 ;
36
37 : concat-nth ( seq1 seq2 -- seq )
38     [ nth append ] curry map-index ;
39
40 : do-cycle   ( array -- array )   dup first suffix ;
41
42 : map-but ( seq i quot -- seq )
43     ! quot : ( seq x -- seq )
44     '[ _ = [ @ ] unless ] map-index ; inline
45