]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/sequences/extras/extras.factor
sequences.extras: Add {2,3}nested-each/map combinators
[factor.git] / extra / sequences / extras / extras.factor
index dc56d117d478c7d7b27873fa10579a447b08df4c..24561f91460863cbd2daa829e5fb54385b1e0588 100644 (file)
@@ -379,6 +379,12 @@ PRIVATE>
 : >string-list ( seq -- seq' )
     [ "\"" 1surround ] map "," join ;
 
+: with-string-lines ( str quot -- str' )
+    [ string-lines ] dip map "\n" join ; inline
+
+: prepend-lines-with-spaces ( str -- str' )
+    [ "    " prepend ] with-string-lines ;
+
 : one? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
     [ find ] 2keep rot [
         [ 1 + ] 2dip find-from drop not
@@ -768,3 +774,29 @@ M: step-slice length
     zero? [ 1 + ] unless ; inline
 
 INSTANCE: step-slice virtual-sequence
+
+: 2nested-each ( seq1 seq2 quot -- )
+    swapd '[
+        swap _ with each
+    ] with each ; inline
+
+: 3nested-each ( seq1 seq2 seq3 quot -- )
+    [ spin ] dip '[
+        -rot [
+            swap _ with with each
+        ] with with each
+    ] with with each ; inline
+
+: 2nested-map ( seq1 seq2 quot -- seq )
+    2over [ length ] bi@ * reach
+    [
+        new-resizable
+        [ [ push ] curry compose 2nested-each ] keep
+    ] keep like ; inline
+
+: 3nested-map ( seq1 seq2 seq3 quot -- seq )
+    3 nover [ length ] tri@ * * 5 npick
+    [
+        new-resizable
+        [ [ push ] curry compose 3nested-each ] keep
+    ] keep like ; inline