+ ! Copyright (C) 2007 Slava Pestov, Chris Double, Doug Coleman,
+ ! Eduardo Cavazos, Daniel Ehrenberg.
+ ! See http://factorcode.org/license.txt for BSD license.
USING: combinators.lib kernel sequences math namespaces assocs
random sequences.private shuffle math.functions mirrors
- arrays math.parser math.private sorting strings ascii ;
-arrays math.parser sorting strings ascii macros ;
++arrays math.parser math.private sorting strings ascii macros ;
IN: sequences.lib
+ : each-withn ( seq quot n -- ) nwith each ; inline
+
+ : each-with ( seq quot -- ) with each ; inline
+
+ : each-with2 ( obj obj list quot -- ) 2 each-withn ; inline
+
+ : map-withn ( seq quot n -- newseq ) nwith map ; inline
+
+ : map-with ( seq quot -- ) with map ; inline
+
+ : map-with2 ( obj obj list quot -- newseq ) 2 map-withn ; inline
+
+ MACRO: nfirst ( n -- )
+ [ [ swap nth ] curry [ keep ] curry ] map concat [ drop ] compose ;
+
+ : prepare-index ( seq quot -- seq n quot )
+ >r dup length r> ; inline
+
+ : each-index ( seq quot -- )
+ #! quot: ( elt index -- )
+ prepare-index 2each ; inline
+
+ : map-index ( seq quot -- )
+ #! quot: ( elt index -- obj )
+ prepare-index 2map ; inline
+
+ ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+ : sigma ( seq quot -- n )
+ [ rot slip + ] curry 0 swap reduce ; inline
+
+ : count ( seq quot -- n )
+ [ 1 0 ? ] compose sigma ; inline
+
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: map-reduce ( seq map-quot reduce-quot -- result )
! List the positions of obj in seq
: indices ( seq obj -- seq )
- >r dup length swap r>
- [ = [ ] [ drop f ] if ] curry
- 2map
- [ ] subset ;
+ >r dup length swap r>
+ [ = [ ] [ drop f ] if ] curry
+ 2map
+ [ ] subset ;
+
+<PRIVATE
+: (attempt-each-integer) ( i n quot -- result )
+ [
+ iterate-step roll
+ [ 3nip ] [ iterate-next (attempt-each-integer) ] if*
+ ] [ 3drop f ] if-iterate? ; inline
+PRIVATE>
+
+: attempt-each ( seq quot -- result )
- (each) iterate-prep (attempt-each-integer) ; inline
++ (each) iterate-prep (attempt-each-integer) ; inline