1 ! Copyright (C) 2007, 2009 Chris Double, Doug Coleman, Eduardo
2 ! Cavazos, Slava Pestov.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: kernel sequences sequences.private math combinators
5 macros quotations fry effects ;
10 : n*quot ( n quot -- quot' ) <repetition> concat >quotation ;
12 : repeat ( n obj quot -- ) swapd times ; inline
16 MACRO: nsequence ( n seq -- )
18 [ drop <reversed> ] [ '[ _ _ new-sequence ] ] 2bi
19 [ '[ @ [ _ swap set-nth-unsafe ] keep ] ] reduce
23 MACRO: narray ( n -- )
24 '[ _ { } nsequence ] ;
29 MACRO: firstn-unsafe ( n -- )
30 [ '[ [ _ ] dip nth-unsafe ] ] map '[ _ cleave ] ;
32 MACRO: firstn ( n -- )
33 dup zero? [ drop [ drop ] ] [
34 [ 1 - swap bounds-check 2drop ]
40 1 - [ dup ] [ '[ _ dip swap ] ] repeat ;
43 dup 1 + '[ _ npick ] n*quot ;
46 dup '[ _ npick ] n*quot ;
49 1 - [ ] [ '[ _ dip swap ] ] repeat ;
52 1 - [ ] [ '[ swap _ dip ] ] repeat ;
58 '[ [ _ ndrop ] dip ] ;
61 2 + '[ dup _ -nrot ] ;
63 MACRO: ndip ( quot n -- )
64 [ '[ _ dip ] ] times ;
66 MACRO: nkeep ( quot n -- )
67 tuck '[ _ ndup _ _ ndip ] ;
69 MACRO: ncurry ( n -- )
76 '[ [ _ nkeep ] dip call ] ;
78 MACRO: ncleave ( quots n -- )
79 [ '[ _ '[ _ _ nkeep ] ] map [ ] join ] [ '[ _ ndrop ] ] bi
82 MACRO: nspread ( quots n -- )
83 over empty? [ 2drop [ ] ] [
87 '[ [ _ _ nspread ] _ ndip @ ]
90 MACRO: napply ( quot n -- )
91 swap <repetition> spread>quot ;
93 MACRO: mnswap ( m n -- )
94 1 + '[ _ -nrot ] swap '[ _ _ napply ] ;
96 MACRO: nweave ( n -- )
97 [ dup <reversed> [ '[ _ _ mnswap ] ] with map ] keep
100 MACRO: nbi-curry ( n -- )
101 [ bi-curry ] n*quot ;
103 : nappend-as ( n exemplar -- seq )
104 [ narray concat ] dip like ; inline
106 : nappend ( n -- seq ) narray concat ; inline