1 ! Copyright (C) 2009 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays combinators fry generalizations kernel macros math
4 math.order memoize.private quotations sequences
6 IN: sequences.generalizations
8 MACRO: (nsequence) ( n -- quot )
9 <iota> reverse [ '[ [ _ swap set-nth-unsafe ] keep ] ] map concat ;
11 MACRO: nsequence ( n exemplar -- quot )
12 [ [nsequence] ] keep '[ @ _ like ] ;
14 MACRO: narray ( n -- quot )
15 '[ _ { } nsequence ] ;
17 MACRO: firstn-unsafe ( n -- quot )
20 MACRO: firstn ( n -- quot )
22 [ 1 - swap bounds-check 2drop ]
27 MACRO: set-firstn-unsafe ( n -- quot )
29 [ <iota> [ '[ _ rot [ set-nth-unsafe ] keep ] ] map ] bi
30 '[ _ -nrot _ spread drop ] ;
32 MACRO: set-firstn ( n -- quot )
34 [ 1 - swap bounds-check 2drop ]
39 MACRO: ?firstn ( n -- quot )
40 dup '[ _ f pad-tail _ firstn-unsafe ] ;
42 : nappend ( n -- seq ) narray concat ; inline
44 : nappend-as ( n exemplar -- seq )
45 [ narray ] [ concat-as ] bi* ; inline
47 MACRO: nmin-length ( n -- quot )
48 dup 1 - [ min ] n*quot
49 '[ [ length ] _ napply @ ] ;
51 : nnth ( n seq... n -- )
52 [ nth ] swap [ apply-curry ] [ cleave* ] bi ; inline
54 : nnth-unsafe ( n seq... n -- )
55 [ nth-unsafe ] swap [ apply-curry ] [ cleave* ] bi ; inline
57 MACRO: nset-nth-unsafe ( n -- quot )
59 [ '[ [ set-nth-unsafe ] _ [ apply-curry ] [ cleave-curry ] [ spread* ] tri ] ]
62 : (neach) ( seq... quot n -- len quot' )
64 '[ [ _ nmin-length ] _ nkeep [ _ nnth-unsafe ] _ ncurry ] dip compose ; inline
66 : neach ( seq... quot n -- )
67 (neach) each-integer ; inline
69 : nmap-as ( seq... quot exemplar n -- result )
70 '[ _ (neach) ] dip map-integers ; inline
72 : nmap ( seq... quot n -- result )
73 dup '[ [ _ npick ] dip swap ] dip nmap-as ; inline
75 MACRO: nnew-sequence ( n -- quot )
77 [ dup '[ [ new-sequence ] _ apply-curry _ cleave* ] ] if-zero ;
79 : nnew-like ( len exemplar... quot n -- result... )
82 [ [ _ nnew-sequence ] dip call ]
88 MACRO: (ncollect) ( n -- quot )
90 '[ [ [ keep ] _ ndip _ nset-nth-unsafe ] _ ncurry ] ;
92 : ncollect ( len quot into... n -- )
93 (ncollect) each-integer ; inline
95 : nmap-integers ( len quot exemplar... n -- result... )
97 '[ [ over ] _ ndip [ [ _ ncollect ] _ nkeep ] _ nnew-like ] call ; inline
99 : mnmap-as ( m*seq quot n*exemplar m n -- result*n )
100 dup '[ [ _ (neach) ] _ ndip _ nmap-integers ] call ; inline
102 : mnmap ( m*seq quot m n -- result*n )
103 2dup '[ [ _ npick ] dip swap _ dupn ] 2dip mnmap-as ; inline
105 : ncollector-as ( quot exemplar... n -- quot' vec... )
107 [ [ length ] keep new-resizable ] _ napply
108 [ [ [ push ] _ apply-curry _ spread* ] _ ncurry compose ] _ nkeep
111 : ncollector ( quot n -- quot' vec... )
112 [ V{ } swap dupn ] keep ncollector-as ; inline
114 : nproduce-as ( pred quot exemplar... n -- seq... )
117 [ _ ncollector-as [ while ] _ ndip ]
119 [ like ] _ apply-curry _ spread*
122 : nproduce ( pred quot n -- seq... )
123 [ { } swap dupn ] keep nproduce-as ; inline
125 MACRO: nmap-reduce ( map-quot reduce-quot n -- quot )
126 -rot dupd compose overd over '[
127 [ [ first ] _ napply @ 1 ] _ nkeep
128 _ _ (neach) (each-integer)
131 : nall? ( seqs... quot n -- ? )
132 (neach) all-integers? ; inline
134 MACRO: finish-nfind ( n -- quot )
135 [ 1 + ] keep dup dup dup f <array> >quotation '[
137 [ [ dup ] _ ndip _ nnth-unsafe ]
142 : (nfind) ( seqs... quot n quot' -- i elts... )
144 [ '[ _ _ (neach) @ ] ] dip
145 [ '[ _ finish-nfind ] ] keep
148 : nfind ( seqs... quot n -- i elts... )
149 [ find-integer ] (nfind) ; inline
151 : nany? ( seqs... quot n -- ? )
152 [ nfind ] [ ndrop ] bi >boolean ; inline