1 ! Copyright (C) 2007 Slava Pestov, Chris Double, Doug Coleman,
2 ! Eduardo Cavazos, Daniel Ehrenberg.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: kernel combinators namespaces quotations hashtables
5 sequences assocs arrays inference effects math math.ranges
6 arrays.lib shuffle macros bake combinators.cleave
11 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
13 : generate ( generator predicate -- obj )
14 #! Call 'generator' until the result satisfies 'predicate'.
15 [ slip over slip ] 2keep
16 roll [ 2drop ] [ rot drop generate ] if ; inline
18 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
19 ! Generalized versions of core combinators
20 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
22 MACRO: ndip ( quot n -- ) dup saver -rot restorer 3append ;
24 MACRO: nslip ( n -- ) dup saver [ call ] rot restorer 3append ;
26 : 4slip ( quot a b c d -- a b c d ) 4 nslip ; inline
30 [ [ , ndup ] dip , -nrot , nslip ]
33 : 4keep ( w x y z quot -- w x y z ) 4 nkeep ; inline
35 MACRO: ncurry ( n -- ) [ curry ] n*quot ;
37 MACRO: nwith ( quot n -- )
39 [ , -nrot [ , nrot , call ] , ncurry ]
42 MACRO: napply ( n -- )
47 map concat >quotation [ call ] append ;
49 : 3apply ( obj obj obj quot -- ) 3 napply ; inline
51 : dipd ( x y quot -- y ) 2 ndip ; inline
53 : 2with ( param1 param2 obj quot -- obj curry )
56 : 3with ( param1 param2 param3 obj quot -- obj curry )
57 with with with ; inline
59 : with* ( obj assoc quot -- assoc curry )
60 swapd [ [ -rot ] dip call ] 2curry ; inline
62 : 2with* ( obj1 obj2 assoc quot -- assoc curry )
65 : 3with* ( obj1 obj2 obj3 assoc quot -- assoc curry )
66 with* with* with* ; inline
68 : assoc-each-with ( obj assoc quot -- )
69 with* assoc-each ; inline
71 : assoc-map-with ( obj assoc quot -- assoc )
72 with* assoc-map ; inline
74 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
75 ! short circuiting words
76 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
78 : short-circuit ( quots quot default -- quot )
79 1quotation -rot { } map>assoc <reversed> alist>quot ;
81 MACRO: && ( quots -- ? )
82 [ [ not ] append [ f ] ] t short-circuit ;
84 MACRO: <-&& ( quots -- )
85 [ [ dup ] swap append [ not ] append [ f ] ] t short-circuit
88 MACRO: <--&& ( quots -- )
89 [ [ 2dup ] swap append [ not ] append [ f ] ] t short-circuit
92 MACRO: || ( quots -- ? ) [ [ t ] ] f short-circuit ;
94 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
96 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
98 MACRO: ifte ( quot quot quot -- )
101 [ >r >r , nkeep , nrot r> r> if ]
104 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
106 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
108 : preserving ( predicate -- quot )
114 MACRO: switch ( quot -- )
115 [ [ preserving ] [ ] bi* ] assoc-map
119 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
121 ! Conceptual implementation:
123 ! : pcall ( seq quots -- seq ) [ call ] 2map ;
125 MACRO: parallel-call ( quots -- )
126 [ [ unclip % r> dup >r push ] bake ] map concat
127 [ V{ } clone >r % drop r> >array ] bake ;
129 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
130 ! map-call and friends
131 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
133 : (make-call-with) ( quots -- quot )
134 [ [ keep ] curry ] map concat [ drop ] append ;
136 MACRO: call-with ( quots -- )
139 MACRO: map-call-with ( quots -- )
140 [ (make-call-with) ] keep length [ narray ] curry compose ;
142 : (make-call-with2) ( quots -- quot )
143 [ [ 2dup >r >r ] swap append [ r> r> ] append ] map concat
146 MACRO: call-with2 ( quots -- )
149 MACRO: map-call-with2 ( quots -- )
150 [ (make-call-with2) ] keep length [ narray ] curry append ;
152 MACRO: map-exec-with ( words -- )
153 [ 1quotation ] map [ map-call-with ] curry ;
155 MACRO: construct-slots ( assoc tuple-class -- tuple )
156 [ construct-empty ] curry swap [
157 [ dip ] curry swap 1quotation [ keep ] curry compose
158 ] { } assoc>map concat compose ;
160 : either ( object first second -- ? )
161 >r keep swap [ r> drop ] [ r> call ] ?if ; inline
163 : 2quot-with ( obj seq quot1 quot2 -- seq quot1 quot2 )
164 >r pick >r with r> r> swapd with ;
166 : or? ( obj quot1 quot2 -- ? )
167 >r keep r> rot [ 2nip ] [ call ] if* ; inline
169 : and? ( obj quot1 quot2 -- ? )
170 >r keep r> rot [ call ] [ 2drop f ] if ; inline
172 : retry ( quot n -- )
173 [ drop ] rot compose attempt-all ; inline