<PRIVATE
-: setup-each ( seq -- n quot )
+! slow-each versions don't unslice
+
+: setup-slow-each ( seq -- n quot )
[ length check-length ] keep [ nth-unsafe ] curry ; inline
-: (each) ( seq quot -- n quot' )
+: (slow-each) ( seq quot -- n quot' )
+ [ setup-slow-each ] dip compose ; inline
+
+: (slow-each-index) ( seq quot -- n quot' )
+ [ setup-slow-each [ keep ] curry ] dip compose ; inline
+
+: setup-each ( seq -- i n quot )
+ dup slice? [
+ [ from>> ] [ to>> ] [ seq>> ] tri
+ ] [
+ [ length check-length 0 swap ] keep
+ ] if [ nth-unsafe ] curry ; inline
+
+: (each) ( seq quot -- i n quot' )
[ setup-each ] dip compose ; inline
-: (each-index) ( seq quot -- n quot' )
+: (each-from) ( seq quot i -- i n quot' )
+ [ (each) ] dip [ + ] curry 2dip ; inline
+
+: (each-index) ( seq quot -- i n quot' )
[ setup-each [ keep ] curry ] dip compose ; inline
+: (each-index-from) ( seq quot i -- i n quot' )
+ [ (each-index) ] dip [ + ] curry 2dip ; inline
+
: (collect) ( quot into -- quot' )
[ [ keep ] dip set-nth-unsafe ] 2curry ; inline
(collect) each-integer ; inline
: map-into ( seq quot into -- )
- [ (each) ] dip collect ; inline
+ [ (slow-each) ] dip collect ; inline
: 2nth-unsafe ( n seq1 seq2 -- elt1 elt2 )
[ nth-unsafe ] bi-curry@ bi ; inline
over [ dupd nth-unsafe ] [ drop f ] if ; inline
: (find) ( seq quot quot' -- i elt )
- pick [ [ (each) ] dip call ] dip finish-find ; inline
+ pick [ [ (slow-each) ] dip call ] dip finish-find ; inline
: (find-from) ( n seq quot quot' -- i elt )
[ 2dup bounds-check? ] 2dip
if ; inline
: (find-index) ( seq quot quot' -- i elt )
- pick [ [ (each-index) ] dip call ] dip finish-find ; inline
+ pick [ [ (slow-each-index) ] dip call ] dip finish-find ; inline
: (find-index-from) ( n seq quot quot' -- i elt )
[ 2dup bounds-check? ] 2dip
PRIVATE>
: each ( ... seq quot: ( ... x -- ... ) -- ... )
- (each) each-integer ; inline
+ (each) (each-integer) ; inline
: each-from ( ... seq quot: ( ... x -- ... ) i -- ... )
- -rot (each) (each-integer) ; inline
+ (each-from) (each-integer) ; inline
: reduce ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... result )
swapd each ; inline
overd [ [ collect ] keep ] new-like ; inline
: map-as ( ... seq quot: ( ... elt -- ... newelt ) exemplar -- ... newseq )
- [ (each) ] dip map-integers ; inline
+ [ (slow-each) ] dip map-integers ; inline
: map ( ... seq quot: ( ... elt -- ... newelt ) -- ... newseq )
over map-as ; inline
[ find-integer ] (find-index) ; inline
: all? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
- (each) all-integers? ; inline
+ (each) (all-integers?) ; inline
: push-if ( ..a elt quot: ( ..a elt -- ..b ? ) accum -- ..b )
[ keep ] dip rot [ push ] [ 2drop ] if ; inline
[ dup ] swap [ keep ] curry produce nip ; inline
: each-index ( ... seq quot: ( ... elt index -- ... ) -- ... )
- (each-index) each-integer ; inline
+ (each-index) (each-integer) ; inline
: map-index-as ( ... seq quot: ( ... elt index -- ... newelt ) exemplar -- ... newseq )
[ dup length <iota> ] 2dip 2map-as ; inline