2008-03-26 18:07:50 -04:00
|
|
|
! Copyright (C) 2007, 2008 Slava Pestov, Chris Double,
|
|
|
|
! Doug Coleman, Eduardo Cavazos,
|
|
|
|
! Daniel Ehrenberg.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-03-26 18:07:50 -04:00
|
|
|
USING: kernel combinators fry namespaces quotations hashtables
|
2008-02-11 01:14:40 -05:00
|
|
|
sequences assocs arrays inference effects math math.ranges
|
2008-03-29 00:00:20 -04:00
|
|
|
arrays.lib shuffle macros bake continuations ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
IN: combinators.lib
|
|
|
|
|
|
|
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
! Generalized versions of core combinators
|
|
|
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
|
|
|
|
MACRO: ndip ( quot n -- ) dup saver -rot restorer 3append ;
|
|
|
|
|
|
|
|
MACRO: nslip ( n -- ) dup saver [ call ] rot restorer 3append ;
|
|
|
|
|
|
|
|
: 4slip ( quot a b c d -- a b c d ) 4 nslip ; inline
|
|
|
|
|
|
|
|
MACRO: nkeep ( n -- )
|
|
|
|
[ ] [ 1+ ] [ ] tri
|
|
|
|
[ [ , ndup ] dip , -nrot , nslip ]
|
|
|
|
bake ;
|
|
|
|
|
|
|
|
: 4keep ( w x y z quot -- w x y z ) 4 nkeep ; inline
|
|
|
|
|
|
|
|
MACRO: ncurry ( n -- ) [ curry ] n*quot ;
|
|
|
|
|
2008-01-09 17:36:30 -05:00
|
|
|
MACRO: nwith ( quot n -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
tuck 1+ dup
|
|
|
|
[ , -nrot [ , nrot , call ] , ncurry ]
|
|
|
|
bake ;
|
|
|
|
|
|
|
|
MACRO: napply ( n -- )
|
|
|
|
2 [a,b]
|
2008-03-26 18:07:50 -04:00
|
|
|
[ [ 1- ] [ ] bi
|
|
|
|
'[ , ntuck , nslip ] ]
|
2007-09-20 18:09:08 -04:00
|
|
|
map concat >quotation [ call ] append ;
|
|
|
|
|
|
|
|
: 3apply ( obj obj obj quot -- ) 3 napply ; inline
|
|
|
|
|
|
|
|
: dipd ( x y quot -- y ) 2 ndip ; inline
|
|
|
|
|
2008-01-10 21:55:38 -05:00
|
|
|
: 2with ( param1 param2 obj quot -- obj curry )
|
|
|
|
with with ; inline
|
|
|
|
|
|
|
|
: 3with ( param1 param2 param3 obj quot -- obj curry )
|
|
|
|
with with with ; inline
|
|
|
|
|
|
|
|
: with* ( obj assoc quot -- assoc curry )
|
|
|
|
swapd [ [ -rot ] dip call ] 2curry ; inline
|
|
|
|
|
|
|
|
: 2with* ( obj1 obj2 assoc quot -- assoc curry )
|
|
|
|
with* with* ; inline
|
|
|
|
|
|
|
|
: 3with* ( obj1 obj2 obj3 assoc quot -- assoc curry )
|
|
|
|
with* with* with* ; inline
|
|
|
|
|
|
|
|
: assoc-each-with ( obj assoc quot -- )
|
|
|
|
with* assoc-each ; inline
|
|
|
|
|
|
|
|
: assoc-map-with ( obj assoc quot -- assoc )
|
|
|
|
with* assoc-map ; inline
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
! short circuiting words
|
|
|
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
|
|
|
|
: short-circuit ( quots quot default -- quot )
|
|
|
|
1quotation -rot { } map>assoc <reversed> alist>quot ;
|
|
|
|
|
2008-02-11 01:14:40 -05:00
|
|
|
MACRO: && ( quots -- ? )
|
|
|
|
[ [ not ] append [ f ] ] t short-circuit ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
MACRO: <-&& ( quots -- )
|
2008-03-19 20:15:32 -04:00
|
|
|
[ [ dup ] prepend [ not ] append [ f ] ] t short-circuit
|
2008-02-11 01:14:40 -05:00
|
|
|
[ nip ] append ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
MACRO: <--&& ( quots -- )
|
2008-03-19 20:15:32 -04:00
|
|
|
[ [ 2dup ] prepend [ not ] append [ f ] ] t short-circuit
|
2008-02-11 01:14:40 -05:00
|
|
|
[ 2nip ] append ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
MACRO: || ( quots -- ? ) [ [ t ] ] f short-circuit ;
|
|
|
|
|
|
|
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
! ifte
|
|
|
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
|
2008-03-26 18:07:50 -04:00
|
|
|
MACRO: preserving ( predicate -- quot )
|
|
|
|
dup infer effect-in
|
|
|
|
dup 1+
|
|
|
|
'[ , , nkeep , nrot ] ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
MACRO: ifte ( quot quot quot -- )
|
2008-03-26 18:07:50 -04:00
|
|
|
'[ , preserving , , if ] ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
! switch
|
|
|
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
|
|
|
|
MACRO: switch ( quot -- )
|
2008-03-26 18:07:50 -04:00
|
|
|
[ [ [ preserving ] curry ] dip ] assoc-map
|
|
|
|
[ cond ] curry ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
|
|
|
|
! Conceptual implementation:
|
|
|
|
|
|
|
|
! : pcall ( seq quots -- seq ) [ call ] 2map ;
|
|
|
|
|
|
|
|
MACRO: parallel-call ( quots -- )
|
2008-02-11 01:14:40 -05:00
|
|
|
[ [ unclip % r> dup >r push ] bake ] map concat
|
|
|
|
[ V{ } clone >r % drop r> >array ] bake ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
! map-call and friends
|
|
|
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
|
2008-01-20 13:08:36 -05:00
|
|
|
: (make-call-with) ( quots -- quot )
|
2008-02-11 01:14:40 -05:00
|
|
|
[ [ keep ] curry ] map concat [ drop ] append ;
|
2008-01-20 13:08:36 -05:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
MACRO: map-call-with ( quots -- )
|
2008-02-11 01:14:40 -05:00
|
|
|
[ (make-call-with) ] keep length [ narray ] curry compose ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-01-20 13:08:36 -05:00
|
|
|
: (make-call-with2) ( quots -- quot )
|
2008-03-19 20:15:32 -04:00
|
|
|
[ [ 2dup >r >r ] prepend [ r> r> ] append ] map concat
|
2008-02-11 01:14:40 -05:00
|
|
|
[ 2drop ] append ;
|
2008-01-20 13:08:36 -05:00
|
|
|
|
|
|
|
MACRO: map-call-with2 ( quots -- )
|
2008-03-15 20:45:05 -04:00
|
|
|
[
|
2008-03-19 20:15:32 -04:00
|
|
|
[ [ 2dup >r >r ] prepend [ r> r> ] append ] map concat
|
2008-03-15 20:45:05 -04:00
|
|
|
[ 2drop ] append
|
|
|
|
] keep length [ narray ] curry append ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-02-11 01:14:40 -05:00
|
|
|
MACRO: map-exec-with ( words -- )
|
|
|
|
[ 1quotation ] map [ map-call-with ] curry ;
|
2007-12-30 23:59:56 -05:00
|
|
|
|
|
|
|
MACRO: construct-slots ( assoc tuple-class -- tuple )
|
2008-04-13 16:06:27 -04:00
|
|
|
[ new ] curry swap [
|
2007-12-30 23:59:56 -05:00
|
|
|
[ dip ] curry swap 1quotation [ keep ] curry compose
|
|
|
|
] { } assoc>map concat compose ;
|
2008-01-09 14:44:58 -05:00
|
|
|
|
|
|
|
: either ( object first second -- ? )
|
2008-01-11 16:04:26 -05:00
|
|
|
>r keep swap [ r> drop ] [ r> call ] ?if ; inline
|
2008-01-16 15:19:50 -05:00
|
|
|
|
|
|
|
: 2quot-with ( obj seq quot1 quot2 -- seq quot1 quot2 )
|
|
|
|
>r pick >r with r> r> swapd with ;
|
|
|
|
|
|
|
|
: or? ( obj quot1 quot2 -- ? )
|
|
|
|
>r keep r> rot [ 2nip ] [ call ] if* ; inline
|
|
|
|
|
|
|
|
: and? ( obj quot1 quot2 -- ? )
|
|
|
|
>r keep r> rot [ call ] [ 2drop f ] if ; inline
|
2008-02-28 16:42:34 -05:00
|
|
|
|
2008-02-16 01:51:52 -05:00
|
|
|
MACRO: multikeep ( word out-indexes -- ... )
|
|
|
|
[
|
|
|
|
dup >r [ \ npick \ >r 3array % ] each
|
|
|
|
%
|
|
|
|
r> [ drop \ r> , ] each
|
|
|
|
] [ ] make ;
|
2008-03-12 23:52:00 -04:00
|
|
|
|
2008-02-28 16:42:34 -05:00
|
|
|
: retry ( quot n -- )
|
2008-02-29 19:04:09 -05:00
|
|
|
[ drop ] rot compose attempt-all ; inline
|
2008-03-19 17:16:35 -04:00
|
|
|
|
|
|
|
: do-while ( pred body tail -- )
|
|
|
|
>r tuck 2slip r> while ;
|
|
|
|
|
|
|
|
: generate ( generator predicate -- obj )
|
|
|
|
[ dup ] swap [ dup [ nip ] unless not ] 3compose
|
|
|
|
swap [ ] do-while ;
|