]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/sequences/extras/extras.factor
sequences.extras: adding nth-index
[factor.git] / extra / sequences / extras / extras.factor
index fbe65dc2ee477ef1e8bcbed358451a3d777c3b9c..027a3af19969716904c232efaf834c26190bfbdf 100644 (file)
@@ -801,6 +801,18 @@ INSTANCE: step-slice virtual-sequence
         [ [ push ] curry compose 3nested-each ] keep
     ] keep like ; inline
 
+: each-prior ( ... seq quot: ( ... prior elt -- ... ) -- ... )
+    '[ [ swap @ ] keep ]
+    sequence-operator 0 -rot each-integer-from drop ; inline
+
+: map-prior-as ( ... seq quot: ( ... prior elt -- elt' ) exemplar -- seq' )
+    [
+        '[ [ swap @ ] keep swap ] length-operator 0 -rot
+    ] dip map-integers-as nip ; inline
+
+: map-prior ( ... seq quot: ( ... prior elt -- elt' ) -- seq' )
+    over map-prior-as ; inline
+
 TUPLE: virtual-zip-index seq ;
 
 C: <zip-index> virtual-zip-index
@@ -811,3 +823,6 @@ M: virtual-zip-index nth-unsafe
     over [ seq>> nth-unsafe ] [ 2array ] bi* ; inline
 
 INSTANCE: virtual-zip-index immutable-sequence
+
+: nth-index ( n obj seq -- i )
+    [ = dup [ drop 1 - dup 0 < ] when ] with find drop nip ;