]> gitweb.factorcode.org Git - factor.git/blob - core/generalizations/generalizations.factor
factor: Move math.ranges => ranges.
[factor.git] / core / generalizations / generalizations.factor
1 ! Copyright (C) 2007, 2009 Chris Double, Doug Coleman, Eduardo
2 ! Cavazos, Slava Pestov.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: arrays combinators kernel kernel.private math ranges
5 memoize.private sequences ;
6 IN: generalizations
7
8 ! These words can be inline combinators the word does no math on
9 ! the input parameters, e.g. n.
10 ! If math is done, the word needs to be a macro so the math can
11 ! be done at compile-time.
12 <<
13
14 ALIAS: n*quot (n*quot)
15
16 MACRO: call-n ( n -- quot )
17     [ call ] <repetition> '[ _ cleave ] ;
18
19 : repeat ( n obj quot -- ) swapd times ; inline
20
21 >>
22
23 MACRO: nsum ( n -- quot )
24     1 - [ + ] n*quot ;
25
26 ERROR: nonpositive-npick n ;
27
28 MACRO: npick ( n -- quot )
29     {
30         { [ dup 0 <= ] [ nonpositive-npick ] }
31         { [ dup 1 = ] [ drop [ dup ] ] }
32         [ 1 - [ dup ] [ '[ _ dip swap ] ] repeat ]
33     } cond ;
34
35 MACRO: nover ( n -- quot )
36     dup 1 + '[ _ npick ] n*quot ;
37
38 : ndup ( n -- )
39     [ '[ _ npick ] ] keep call-n ; inline
40
41 MACRO: dupn ( n -- quot )
42     [ [ drop ] ]
43     [ 1 - [ dup ] n*quot ] if-zero ;
44
45 MACRO: nrot ( n -- quot )
46     1 - [ ] [ '[ _ dip swap ] ] repeat ;
47
48 MACRO: -nrot ( n -- quot )
49     1 - [ ] [ '[ swap _ dip ] ] repeat ;
50
51 : ndrop ( n -- )
52     [ drop ] swap call-n ; inline
53
54 : nnip ( n -- )
55     '[ _ ndrop ] dip ; inline
56
57 : ndip ( n -- )
58     [ [ dip ] curry ] swap call-n call ; inline
59
60 : nkeep ( n -- )
61     dup '[ [ _ ndup ] dip _ ndip ] call ; inline
62
63 : ncurry ( n -- )
64     [ curry ] swap call-n ; inline
65
66 : nwith ( n -- )
67     [ with ] swap call-n ; inline
68
69 : nbi ( quot1 quot2 n -- )
70     [ nip nkeep ] [ drop nip call ] 3bi ; inline
71
72 MACRO: ncleave ( quots n -- quot )
73     [ '[ _ '[ _ _ nkeep ] ] map [ ] join ] [ '[ _ ndrop ] ] bi
74     compose ;
75
76 MACRO: nspread ( quots n -- quot )
77     over empty? [ 2drop [ ] ] [
78         [ [ but-last ] dip ]
79         [ [ last ] dip ] 2bi
80         swap
81         '[ [ _ _ nspread ] _ ndip @ ]
82     ] if ;
83
84 MACRO: spread* ( n -- quot )
85     [ [ ] ] [
86         [1..b) [ '[ [ [ _ ndip ] curry ] dip compose ] ] map [ ] concat-as
87         [ call ] compose
88     ] if-zero ;
89
90 MACRO: nspread* ( m n -- quot )
91     [ drop [ ] ] [
92         [ * 0 ] [ drop neg ] 2bi
93         <range> rest >array dup length <iota> <reversed>
94         [ '[ [ [ _ ndip ] curry ] _ ndip ] ] 2map
95         [ [ ] concat-as ]
96         [ length 1 - [ compose ] <array> concat append ] bi
97         [ call ] compose
98     ] if-zero ;
99
100 MACRO: cleave* ( n -- quot )
101     [ [ ] ]
102     [ 1 - [ [ [ keep ] curry ] dip compose ] n*quot [ call ] compose ]
103     if-zero ;
104
105 : napply ( quot n -- )
106     [ dupn ] [ spread* ] bi ; inline
107
108 : mnapply ( quot m n -- )
109     [ nip dupn ] [ nspread* ] 2bi ; inline
110
111 : apply-curry ( a... quot n -- )
112     [ currier ] dip napply ; inline
113
114 : cleave-curry ( a quot... n -- )
115     [ currier ] swap [ napply ] [ cleave* ] bi ; inline
116
117 : spread-curry ( a... quot... n -- )
118     [ currier ] swap [ napply ] [ spread* ] bi ; inline
119
120 MACRO: mnswap ( m n -- quot )
121     1 + '[ _ -nrot ] swap '[ _ _ napply ] ;
122
123 MACRO: nweave ( n -- quot )
124     [ dup <iota> <reversed> [ '[ _ _ mnswap ] ] with map ] keep
125     '[ _ _ ncleave ] ;
126
127 : nbi-curry ( n -- )
128     [ bi-curry ] swap call-n ; inline
129
130 MACRO: map-compose ( quots quot -- quot' )
131     '[ _ compose ] map '[ _ ] ;