1 ! Copyright (C) 2009 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences sequences.private math
4 combinators macros math.order math.ranges quotations fry effects
5 memoize.private generalizations ;
6 IN: sequences.generalizations
8 MACRO: nsequence ( n seq -- quot )
9 [ [nsequence] ] keep '[ @ _ like ] ;
11 MACRO: narray ( n -- quot )
12 '[ _ { } nsequence ] ;
14 MACRO: firstn-unsafe ( n -- quot )
17 MACRO: firstn ( n -- quot )
19 [ 1 - swap bounds-check 2drop ]
24 MACRO: set-firstn-unsafe ( n -- quot )
26 [ <iota> [ '[ _ rot [ set-nth-unsafe ] keep ] ] map ] bi
27 '[ _ -nrot _ spread drop ] ;
29 MACRO: set-firstn ( n -- quot )
31 [ 1 - swap bounds-check 2drop ]
36 : nappend ( n -- seq ) narray concat ; inline
38 : nappend-as ( n exemplar -- seq )
39 [ narray ] [ concat-as ] bi* ; inline
41 MACRO: nmin-length ( n -- quot )
42 dup 1 - [ min ] n*quot
43 '[ [ length ] _ napply @ ] ;
45 : nnth ( n seq... n -- )
46 [ nth ] swap [ apply-curry ] [ cleave* ] bi ; inline
48 : nnth-unsafe ( n seq... n -- )
49 [ nth-unsafe ] swap [ apply-curry ] [ cleave* ] bi ; inline
51 MACRO: nset-nth-unsafe ( n -- quot )
53 [ '[ [ set-nth-unsafe ] _ [ apply-curry ] [ cleave-curry ] [ spread* ] tri ] ]
56 : (neach) ( seq... quot n -- len quot' )
58 '[ [ _ nmin-length ] _ nkeep [ _ nnth-unsafe ] _ ncurry ] dip compose ; inline
60 : neach ( seq... quot n -- )
61 (neach) each-integer ; inline
63 : nmap-as ( seq... quot exemplar n -- result )
64 '[ _ (neach) ] dip map-integers ; inline
66 : nmap ( seq... quot n -- result )
67 dup '[ [ _ npick ] dip swap ] dip nmap-as ; inline
69 MACRO: nnew-sequence ( n -- quot )
71 [ dup '[ [ new-sequence ] _ apply-curry _ cleave* ] ] if-zero ;
73 : nnew-like ( len exemplar... quot n -- result... )
76 [ [ _ nnew-sequence ] dip call ]
82 MACRO: (ncollect) ( n -- quot )
84 '[ [ [ keep ] _ ndip _ nset-nth-unsafe ] _ ncurry ] ;
86 : ncollect ( len quot into... n -- )
87 (ncollect) each-integer ; inline
89 : nmap-integers ( len quot exemplar... n -- result... )
91 '[ [ over ] _ ndip [ [ _ ncollect ] _ nkeep ] _ nnew-like ] call ; inline
93 : mnmap-as ( m*seq quot n*exemplar m n -- result*n )
94 dup '[ [ _ (neach) ] _ ndip _ nmap-integers ] call ; inline
96 : mnmap ( m*seq quot m n -- result*n )
97 2dup '[ [ _ npick ] dip swap _ dupn ] 2dip mnmap-as ; inline
99 : ncollector-as ( quot exemplar... n -- quot' vec... )
101 [ [ length ] keep new-resizable ] _ napply
102 [ [ [ push ] _ apply-curry _ spread* ] _ ncurry compose ] _ nkeep
105 : ncollector ( quot n -- quot' vec... )
106 [ V{ } swap dupn ] keep ncollector-as ; inline
108 : nproduce-as ( pred quot exemplar... n -- seq... )
111 [ _ ncollector-as [ while ] _ ndip ]
113 [ like ] _ apply-curry _ spread*
116 : nproduce ( pred quot n -- seq... )
117 [ { } swap dupn ] keep nproduce-as ; inline
119 MACRO: nmap-reduce ( map-quot reduce-quot n -- quot )
120 -rot dupd compose [ over ] dip over '[
121 [ [ first ] _ napply @ 1 ] _ nkeep
122 _ _ (neach) (each-integer)
125 : nall? ( seqs... quot n -- ? )
126 (neach) all-integers? ; inline
128 MACRO: finish-nfind ( n -- quot )
129 [ 1 + ] keep dup dup dup '[
131 [ [ dup ] _ ndip _ nnth-unsafe ]
132 [ _ ndrop _ [ f ] times ]
136 : (nfind) ( seqs... quot n quot' -- i elts... )
138 [ '[ _ _ (neach) @ ] ] dip
139 [ '[ _ finish-nfind ] ] keep
142 : nfind ( seqs... quot n -- i elts... )
143 [ find-integer ] (nfind) ; inline
145 : nany? ( seqs... quot n -- ? )
146 [ nfind ] [ ndrop ] bi >boolean ; inline