sequences.extras: Add {2,3}nested-each/map combinators
authorDoug Coleman <doug.coleman@gmail.com>
Mon, 8 Aug 2022 22:39:40 +0000 (17:39 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Mon, 8 Aug 2022 22:41:43 +0000 (17:41 -0500)
extra/sequences/extras/extras.factor

index 0d37e3e14d118f96a3c9de09c4258144c6c767b2..24561f91460863cbd2daa829e5fb54385b1e0588 100644 (file)
@@ -774,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