]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: adding "map-from".
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 7 Sep 2012 21:00:04 +0000 (14:00 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 7 Sep 2012 21:00:04 +0000 (14:00 -0700)
extra/sequences/extras/extras.factor

index 2d48f3fc03c465199bbc74e68638498d0860b523..25beea2ccef77946f911242ed911b766ea23e2da 100644 (file)
@@ -1,4 +1,4 @@
-USING: accessors arrays assocs grouping kernel locals math
+USING: accessors arrays assocs fry grouping kernel locals math
 math.order math.ranges sequences sequences.private splitting ;
 FROM: sequences => change-nth ;
 IN: sequences.extras
@@ -161,6 +161,22 @@ IN: sequences.extras
 
 <PRIVATE
 
+: ((each-from)) ( i seq -- n quot )
+    [ length over - 0 max swap ] keep '[ _ + _ nth-unsafe ] ; inline
+
+: (each-from) ( i seq quot -- n quot' ) [ ((each-from)) ] dip compose ;
+    inline
+
+PRIVATE>
+
+: map-from-as ( ... seq quot: ( ... elt -- ... newelt ) i exemplar -- ... newseq )
+    [ -rot (each-from) ] dip map-integers ; inline
+
+: map-from ( ... seq quot: ( ... elt -- ... newelt ) i -- ... newseq )
+    pick map-from-as ; inline
+
+<PRIVATE
+
 : push-map-if ( ..a elt filter-quot: ( ..a elt -- ..b ? ) map-quot: ( ..a elt -- ..b newelt ) accum -- ..b )
     [ keep over ] 2dip [ when ] dip rot [ push ] [ 2drop ] if ; inline