1 ! Copyright (C) 2013 Doug Coleman, John Benediktsson.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays combinators combinators.smart fry generalizations
4 kernel macros math quotations sequences
5 sequences.generalizations sequences.private system ;
8 : once ( quot -- ) call ; inline
9 : twice ( quot -- ) dup [ call ] dip call ; inline
10 : thrice ( quot -- ) dup dup [ call ] 2dip [ call ] dip call ; inline
11 : forever ( quot -- ) [ t ] compose loop ; inline
13 MACRO: cond-case ( assoc -- quot )
16 [ first [ dup ] prepose ]
17 [ second [ drop ] prepose ] bi 2array
19 ] map [ cond ] curry ;
21 MACRO: cleave-array ( quots -- quot )
22 [ '[ _ cleave ] ] [ length '[ _ narray ] ] bi compose ;
24 : 3bi* ( u v w x y z p q -- )
25 [ 3dip ] dip call ; inline
27 : 3bi@ ( u v w x y z quot -- )
30 : 4bi ( w x y z p q -- )
31 [ 4keep ] dip call ; inline
33 : 4bi* ( s t u v w x y z p q -- )
34 [ 4dip ] dip call ; inline
36 : 4bi@ ( s t u v w x y z quot -- )
39 : 4tri ( w x y z p q r -- )
40 [ [ 4keep ] dip 4keep ] dip call ; inline
42 : keepd ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x )
45 : plox ( ... x/f quot: ( ... x -- ... ) -- ... )
48 MACRO: smart-plox ( true -- quot )
49 [ inputs [ 1 - [ and ] n*quot ] keep ] keep swap
50 '[ _ _ [ _ ndrop f ] smart-if ] ;
52 : throttle ( quot millis -- quot' )
54 _ nano-count { 0 } 2dup first-unsafe _ + >=
55 [ 0 swap set-nth-unsafe call ] [ 3drop ] if
58 : swap-when ( x y quot: ( x -- n ) quot: ( n n -- ? ) -- x' y' )
59 '[ _ _ 2dup _ bi@ @ [ swap ] when ] call ; inline