1 ! Copyright (C) 2007, 2008 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 math.ranges
5 combinators macros quotations fry macros locals ;
10 : n*quot ( n seq -- seq' ) <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 ] ;
26 MACRO: firstn ( n -- )
27 dup zero? [ drop [ drop ] ] [
28 [ [ '[ [ _ ] dip nth-unsafe ] ] map ]
29 [ 1- '[ [ _ ] dip bounds-check 2drop ] ]
30 bi prefix '[ _ cleave ]
34 1- [ dup ] [ '[ _ dip swap ] ] repeat ;
37 dup '[ _ npick ] n*quot ;
40 1- [ ] [ '[ _ dip swap ] ] repeat ;
43 1- [ ] [ '[ swap _ dip ] ] repeat ;
49 '[ [ _ ndrop ] dip ] ;
52 2 + '[ dup _ -nrot ] ;
55 1 [a,b] [ ] [ '[ @ _ -nrot ] ] reduce ;
57 MACRO: ndip ( quot n -- )
58 [ '[ _ dip ] ] times ;
61 '[ [ call ] _ ndip ] ;
63 MACRO: nkeep ( quot n -- )
64 tuck '[ _ ndup _ _ ndip ] ;
66 MACRO: ncurry ( n -- )
72 MACRO: ncleave ( quots n -- )
73 [ '[ _ '[ _ _ nkeep ] ] map [ ] join ] [ '[ _ ndrop ] ] bi
76 MACRO: napply ( quot n -- )
77 swap <repetition> spread>quot ;
79 MACRO: mnswap ( m n -- )
80 1+ '[ _ -nrot ] <repetition> spread>quot ;
82 : nappend-as ( n exemplar -- seq )
83 [ narray concat ] dip like ; inline
85 : nappend ( n -- seq ) narray concat ; inline