1 ! Copyright (C) 2006, 2010 Slava Pestov, Daniel Ehrenberg.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs kernel kernel.private math
4 math.order math.private quotations sequences sequences.private
8 ! Most of these combinators have compile-time expansions in
9 ! the optimizing compiler. See stack-checker.transforms and
10 ! compiler.tree.propagation.call-effect
14 : call-effect-unsafe ( quot effect -- ) drop call ;
16 : execute-effect-unsafe ( word effect -- ) drop execute ;
19 ERROR-HANDLER-QUOT special-object [ die ] or
20 ( error -- * ) call-effect-unsafe ;
24 ERROR: wrong-values quot call-site ;
26 ! We can't USE: effects here so we forward reference slots instead
31 : call-effect ( quot effect -- )
32 ! Don't use fancy combinators here, since this word always
35 [ [ get-datastack ] dip dip ] dip
36 dup terminated?>> [ 2drop f ] [
37 dup in>> length swap out>> length
41 [ 2drop ] [ wrong-values ] if ;
43 : execute-effect ( word effect -- )
44 [ [ execute ] curry ] dip call-effect ;
50 : cleave>quot ( seq -- quot )
51 [ [ keep ] curry ] map concat
52 [ drop ] [ ] append-as ;
55 : 2cleave ( x y seq -- )
56 [ 2keep ] each 2drop ;
58 : 2cleave>quot ( seq -- quot )
59 [ [ 2keep ] curry ] map concat
60 [ 2drop ] [ ] append-as ;
63 : 3cleave ( x y z seq -- )
64 [ 3keep ] each 3drop ;
66 : 3cleave>quot ( seq -- quot )
67 [ [ 3keep ] curry ] map concat
68 [ 3drop ] [ ] append-as ;
71 : 4cleave ( w x y z seq -- )
72 [ 4keep ] each 4drop ;
74 : 4cleave>quot ( seq -- quot )
75 [ [ 4keep ] curry ] map concat
76 [ 4drop ] [ ] append-as ;
79 : shallow-spread>quot ( seq -- quot )
80 [ ] [ [ dup empty? [ [ dip ] curry ] unless ] dip append ] reduce ;
82 : deep-spread>quot ( seq -- quot )
83 [ ] [ [ [ dip ] curry ] dip append ] reduce ;
85 : spread ( objs... seq -- )
86 deep-spread>quot call ;
92 [ dup callable? [ drop t ] [ first call ] if ] find nip
93 [ dup callable? [ call ] [ second call ] if ]
96 : alist>quot ( default assoc -- quot )
97 [ rot \ if 3array [ ] append-as ] assoc-each ;
99 : cond>quot ( assoc -- quot )
100 [ dup pair? [ [ t ] swap 2array ] unless ] map
101 reverse! [ no-cond ] swap alist>quot ;
104 ERROR: no-case object ;
106 : case-find ( obj assoc -- obj' )
109 dupd first dup word? [
112 dup wrapper? [ wrapped>> ] when
117 \ case-find t "no-compile" set-word-prop
119 : case ( obj assoc -- )
121 { [ dup array? ] [ nip second call ] }
122 { [ dup callable? ] [ call ] }
123 { [ dup not ] [ drop no-case ] }
126 : linear-case-quot ( default assoc -- quot )
128 [ 1quotation \ dup prefix \ = suffix ]
129 [ \ drop prefix ] bi*
130 ] assoc-map reverse! alist>quot ;
134 : (distribute-buckets) ( buckets pair keys -- )
136 drop [ swap adjoin ] curry each
139 [ 2dup ] dip hashcode pick length rem rot nth adjoin
143 : <buckets> ( initial length -- array )
144 next-power-of-2 <iota> swap [ nip clone ] curry map ;
146 : distribute-buckets ( alist initial quot -- buckets )
147 swapd [ [ dup first ] dip call 2array ] curry map
148 [ length <buckets> dup ] keep
149 [ first2 (distribute-buckets) ] with each ; inline
151 : hash-case-table ( default assoc -- array )
152 V{ } [ 1array ] distribute-buckets [
153 [ [ literalize ] dip ] assoc-map linear-case-quot
156 : hash-dispatch-quot ( table -- quot )
157 [ length 1 - [ fixnum-bitand ] curry ] keep
158 [ dispatch ] curry append ;
160 : hash-case-quot ( default assoc -- quot )
161 hash-case-table hash-dispatch-quot
162 [ dup hashcode >fixnum ] prepend ;
164 : contiguous-range? ( keys -- ? )
165 dup [ fixnum? ] all? [
167 [ length ] [ supremum ] [ infimum ] tri - - 1 =
171 : dispatch-case-quot ( default assoc -- quot )
173 [ keys [ infimum ] [ supremum ] bi over ]
174 [ sort-keys values [ >quotation ] map ] bi
177 integer>fixnum-strict dup _ _ between? [
185 : case>quot ( default assoc -- quot )
187 { [ dup empty? ] [ 2drop ] }
188 { [ dup [ length 4 <= ] [ [ word? ] any? ] bi or ] [ drop linear-case-quot ] }
189 { [ dup contiguous-range? ] [ drop dispatch-case-quot ] }
190 { [ dup [ wrapper? ] none? ] [ drop hash-case-quot ] }
191 { [ dup [ wrapper? ] all? ] [ drop [ [ wrapped>> ] dip ] assoc-map hash-case-quot ] }
192 [ drop linear-case-quot ]
195 : to-fixed-point ( ... object quot: ( ... object(n) -- ... object(n+1) ) -- ... object(n) )
196 [ keep over = ] keep [ to-fixed-point ] curry unless ; inline recursive