]> gitweb.factorcode.org Git - factor.git/blob - extra/combinators/extras/extras.factor
e0b81ccefabb91692a69cfb0de56604e90070793
[factor.git] / extra / combinators / extras / extras.factor
1 ! Copyright (C) 2013 Doug Coleman, John Benediktsson.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs combinators combinators.smart
4 generalizations graphs.private kernel math math.order namespaces
5 quotations sequences sequences.generalizations sequences.private
6 sets shuffle stack-checker.transforms system words ;
7 IN: combinators.extras
8
9 : callk ( ..a quot: ( ..a -- ..b ) -- ..b quot )
10     dup [ call ] dip ; inline
11
12 : once ( quot -- ) call ; inline
13 : twice ( quot -- ) callk call ; inline
14 : thrice ( quot -- ) callk callk call ; inline
15 : forever ( quot -- ) [ t ] compose loop ; inline
16
17 MACRO: cond-case ( assoc -- quot )
18     [
19         dup callable? not [
20             [ first [ dup ] prepose ]
21             [ second [ drop ] prepose ] bi 2array
22         ] when
23     ] map [ cond ] curry ;
24
25 MACRO: cleave-array ( quots -- quot )
26     [ '[ _ cleave ] ] [ length '[ _ narray ] ] bi compose ;
27
28 : 3bi* ( u v w x y z p q -- )
29     [ 3dip ] dip call ; inline
30
31 : 3bi@ ( u v w x y z quot -- )
32     dup 3bi* ; inline
33
34 : 4bi ( w x y z p q -- )
35     [ 4keep ] dip call ; inline
36
37 : 4bi* ( s t u v w x y z p q -- )
38     [ 4dip ] dip call ; inline
39
40 : 4tri* ( o p q r  s t u v  w x y z  p q r -- )
41     [ 8 ndip ] 2dip
42     [ 4dip ] dip
43     call ; inline
44
45 : 4bi@ ( s t u v  w x y z  quot -- )
46     dup 4bi* ; inline
47
48 : 4tri@ ( a b c d  e f g h  i j k l  quot -- )
49     dup dup 4tri* ; inline
50
51 : 4tri ( w x y z  p q r -- )
52     [ [ 4keep ] dip 4keep ] dip call ; inline
53
54 : 4quad ( w x y z  p q r s -- )
55     [ [ [ 4keep ] dip 4keep ] dip 4keep ] dip call ; inline
56
57 : quad* ( w x y z p q r s -- ) [ [ [ 3dip ] dip 2dip ] dip dip ] dip call ; inline
58
59 : quad@ ( w x y z quot -- ) dup dup dup quad* ; inline
60
61 MACRO: smart-plox ( true -- quot )
62     [ inputs [ 1 - [ and ] n*quot ] keep ] keep swap
63     '[ _ _ [ _ ndrop f ] smart-if ] ;
64
65 : throttle ( quot millis -- quot' )
66     1,000,000 * '[
67         _ nano-count { 0 } 2dup first-unsafe _ + >=
68         [ 0 swap set-nth-unsafe call ] [ 3drop ] if
69     ] ; inline
70
71 : swap-when ( x y quot: ( x -- n ) quot: ( n n -- ? ) -- x' y' )
72     '[ _ _ 2dup _ bi@ @ [ swap ] when ] call ; inline
73
74
75 ! ?1arg-result-falsify
76
77 : 1falsify ( obj/f -- obj/f ) ; inline
78 : 2falsify ( obj1 obj2 -- obj1/f obj2/f ) 2dup and [ 2drop f f ] unless ; inline
79 : 3falsify ( obj1 obj2 obj3 -- obj1/f obj2/f obj3/f ) 3dup and and [ 3drop f f f ] unless ; inline
80
81 MACRO: n-and ( n -- quot )
82     1 [-] [ and ] n*quot ;
83
84 MACRO: n*obj ( n obj -- quot )
85     1quotation n*quot ;
86
87 MACRO:: n-falsify ( n -- quot )
88     [ n ndup n n-and [ n ndrop n f n*obj ] unless ] ;
89
90 ! when both args are true, call quot. otherwise dont
91 : ?2res ( ..a obj1 obj2 quot: ( obj1 obj2 -- ? ) -- ..b )
92     [ 2dup and ] dip [ 2drop f ] if ; inline
93
94 ! try the quot, keep the original arg if quot is true
95 : ?1arg ( obj quot: ( obj -- ? ) -- obj/f )
96     [ ?call ] keepd '[ _ ] [ f ] if ; inline
97
98 : ?2arg ( obj1 obj2 quot: ( obj1 obj2 -- ? ) -- obj1/f obj2/f )
99     [ ?2res ] 2keepd '[ _ _ ] [ f f ] if ; inline
100
101 <<
102 : alist>quot* ( default assoc -- quot )
103     [ rot \ if* 3array [ ] append-as ] assoc-each ;
104
105 : cond*>quot ( assoc -- quot )
106     [ dup pair? [ [ drop ] prepend [ t ] swap 2array ] unless ] map
107     reverse! [ no-cond ] swap alist>quot* ;
108
109 DEFER: cond*
110 \ cond* [ cond*>quot ] 1 define-transform
111 \ cond* t "no-compile" set-word-prop
112 >>
113 : cond* ( assoc -- )
114     [ dup callable? [ drop t ] [ first call ] if ] map-find
115     [ dup callable? [ nip call ] [ second call ] if ]
116     [ no-cond ] if* ;
117
118 MACRO: chain ( quots -- quot )
119     <reversed> [ ] [ swap '[ [ @ @ ] [ f ] if* ] ] reduce ;
120
121 : with-output-variable ( value variable quot -- value )
122     over [ get ] curry compose with-variable ; inline
123
124 : loop1 ( ..a quot: ( ..a -- ..a obj ? ) -- ..a obj )
125     [ call ] keep '[ drop _ loop1 ] when ; inline recursive
126
127
128 : keep-1up ( quot -- quot ) keep 1 2 0 nrotated ; inline
129 : keep-2up ( quot -- quot ) keep 2 3 0 nrotated ; inline
130 : keep-3up ( quot -- quot ) keep 3 4 0 nrotated ; inline
131
132 : 2keep-1up ( quot -- quot ) 2keep 1 3 0 nrotated ; inline
133 : 2keep-2up ( quot -- quot ) 2keep 2 4 0 nrotated ; inline
134 : 2keep-3up ( quot -- quot ) 2keep 3 5 0 nrotated ; inline
135
136 : 3keep-1up ( quot -- quot ) 3keep 1 4 0 nrotated ; inline
137 : 3keep-2up ( quot -- quot ) 3keep 2 5 0 nrotated ; inline
138 : 3keep-3up ( quot -- quot ) 3keep 3 6 0 nrotated ; inline
139
140 ! d is dummy, o is object to save notation space
141 : dip-1up  ( ..a d quot: ( ..a -- ..b o d ) -- ..b d o )
142     dip swap ; inline
143 : dip-2up  ( ..a d quot: ( ..a -- ..b o1 o2 d ) -- ..b d o1 o2 )
144     dip rot rot ; inline
145
146 : 2dip-1up ( ..a d1 d2 quot: ( ..a -- ..b o d1 d2 ) -- ..b d1 d2 o )
147     2dip rot ; inline
148 : 2dip-2up ( ..a d1 d2 quot: ( ..a -- ..b o1 o2 d1 d2 ) -- ..b d1 d2 o1 o2 )
149     2dip roll roll ; inline
150
151 : 3dip-1up ( ..a d1 d2 d3 quot: ( ..a -- ..b o d1 d2 d3 ) -- ..b d1 d2 d3 o )
152     3dip roll ; inline
153 : 3dip-2up ( ..a d1 d2 d3 quot: ( ..a -- ..b o1 o2 d1 d2 d3 ) -- ..b d1 d2 d3 o1 o2 )
154     3dip 2 5 0 nrotated ; inline
155 : 3dip-3up ( ..a d1 d2 d3 quot: ( ..a -- ..b o1 o2 o3 d1 d2 d3 ) -- ..b d1 d2 d3 o1 o2 o3 )
156     3dip 3 6 0 nrotated ; inline
157
158
159 : 2craft-1up ( ..a quot1: ( ..a -- ..b o1 ) quot2: ( ..b -- ..c o2 ) -- ..c o1 o2 )
160     [ call ] dip [ dip-1up ] call ; inline
161
162 : 3craft-1up ( ..a quot1: ( ..a -- ..b o1 ) quot2: ( ..b -- ..c o2 ) quot3: ( ..c -- ..d o3 ) -- ..d o1 o2 o3 )
163     [ call ] 2dip [ dip-1up ] dip [ 2dip-1up ] call ; inline
164
165 : 4craft-1up ( ..a quot1: ( ..a -- ..b o1 ) quot2: ( ..b -- ..c o2 ) quot3: ( ..c -- ..d o3 ) quot4: ( ..d -- ..e o4 ) -- ..e o1 o2 o3 o4 )
166     [ call ] 3dip [ dip-1up ] 2dip [ 2dip-1up ] dip [ 3dip-1up ] call ; inline
167
168 : 3and ( a b c -- ? ) and and ; inline
169 : 4and ( a b c d -- ? ) and and and ; inline
170
171 : 3or ( a b c -- ? ) or or ; inline
172 : 4or ( a b c d -- ? ) or or or ; inline
173
174 ! The kept values are on the bottom of the stack
175 MACRO: keep-under ( quot -- quot' )
176     dup outputs 1 + '[ _ keep 1 _ 0 -nrotated ] ;
177
178 MACRO: 2keep-under ( quot -- quot' )
179     dup outputs 2 + '[ _ 2keep 2 _ 0 -nrotated ] ;
180
181 MACRO: 3keep-under ( quot -- quot' )
182     dup outputs 3 + '[ _ 3keep 3 _ 0 -nrotated ] ;
183
184 MACRO: 4keep-under ( quot -- quot' )
185     dup outputs 4 + '[ _ 4keep 4 _ 0 -nrotated ] ;
186
187 ! for use with assoc-map etc
188 : 1temp1d ( quot: ( a b c -- d e f ) -- quot ) '[ swap @ swap ] ; inline
189 : 1temp2d ( quot: ( a b c -- d e f ) -- quot ) '[ rot @ -rot ] ; inline
190 : 2temp2d ( quot: ( a b c d -- e f g h ) -- quot ) '[ 2 4 0 nrotated @ 2 4 0 -nrotated ] ; inline
191
192 : (closure-limit) ( vertex set quot: ( vertex -- edges ) i n -- )
193     2dup < [
194         [ 1 + ] dip
195         2reach ?adjoin [
196             [ [ dip ] keep ] 2dip [ (closure-limit) ] 2curry 2curry each
197         ] [ 5drop ] if
198     ] [
199         5drop
200     ] if ; inline recursive
201
202 : closure-limit-as ( vertex quot: ( vertex -- edges ) n exemplar -- set )
203     [ 0 ] 2dip
204     new-empty-set-like [ -roll (closure-limit) ] keep ; inline
205
206 : closure-limit ( vertex quot: ( vertex -- edges ) n -- set )
207     HS{ } closure-limit-as ; inline
208
209 : 1check ( obj quot -- obj ? ) over [ call ] dip swap ; inline
210 : 2check ( obj1 obj2 quot -- obj ? ) 2over [ call ] 2dip rot ; inline
211
212 : 1check-when ( ..a obj cond: ( ..a obj -- obj/f ) true: ( ..a obj cond -- ..b ) -- ..b )
213     [ 1check ] dip when ; inline
214 : 2check-when ( ..a obj1 obj2 cond: ( ..a obj1 obj2 -- obj/f ) true: ( ..a obj1 obj2 cond -- ..b ) -- ..b )
215     [ 2check ] dip when ; inline