summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2022-12-16 00:18:05 +0100
committerLeah Neukirchen <leah@vuxu.org>2022-12-16 00:18:05 +0100
commit28ad4b62515325463656627fa3d80a984f8c0fdb (patch)
treed74de06cc1f46c599387d247744779c4934c72d3
parent234dbc781c328db11c3cc17a74d2c9301e5d82bf (diff)
downloadmew-28ad4b62515325463656627fa3d80a984f8c0fdb.tar.gz
mew-28ad4b62515325463656627fa3d80a984f8c0fdb.tar.xz
mew-28ad4b62515325463656627fa3d80a984f8c0fdb.zip
add parallel for
-rw-r--r--mew.scm9
-rw-r--r--mew.svnwiki7
-rw-r--r--tests/test.mew10
3 files changed, 23 insertions, 3 deletions
diff --git a/mew.scm b/mew.scm
index be9ae9b..17be96d 100644
--- a/mew.scm
+++ b/mew.scm
@@ -420,8 +420,17 @@
           ((procedure? obj) generator-for-each)
           (#t (error "no generic-for-each defined"))))
 
+  (define-syntax for-regroup
+    (syntax-rules ()
+      ((_ () ((i obj) ...) body ...)
+       (void (final (gmap (match-lambda* ((i ...) body ...)) (gen obj) ...))))
+      ((_ (i obj rest ...) (j ...) body ...)
+       (for-regroup (rest ...) (j ... (i obj)) body ...))))
+
   (define-syntax for
     (syntax-rules ()
+      ((_ (i obj j obj2 rest ...) body ...)
+       (for-regroup (i obj j obj2 rest ...) () body ...))
       ((_ (i obj) body ...)
        (let ((o obj))
          ((generic-for-each o) (match-lambda (i body ...)) o)))))
diff --git a/mew.svnwiki b/mew.svnwiki
index 3d30816..958da33 100644
--- a/mew.svnwiki
+++ b/mew.svnwiki
@@ -386,14 +386,17 @@ Return a duplicate of the nested datastructure {{<obj>}}, consisting
 of lists, vectors, hash-tables and strings.  Create copies of values
 up to a level of {{<depth>}}, or infinitely by default.
 
-<syntax>(for (<var> <obj>) <body>...)</syntax>
-<syntax>(for ((<key> . <val>) <tbl>) <body>...)</syntax>
+<syntax>(for (<var> <obj> ...) <body>...)</syntax>
+<syntax>(for ((<key> . <val>) <tbl> ...) <body>...)</syntax>
 
 If {{<obj>}} is a list or a vector, iterate over its elements.
 If {{<obj>}} is a procedure, consider it a SRFI-158 generator
 and iterate over its values.
 If {{<obj>}} is a hash-table, iterate over its keys and values.
 
+Multiple {{<var> <obj>}}-pairs may be specified, then {{for}}
+iterates over these in *parallel*, stopping after the shortest ends.
+
 <procedure>(search <needle> <haystack> <offset>?)</procedure>
 
 Returns the offset of the sequence (string/list/vector) {{<needle>}}
diff --git a/tests/test.mew b/tests/test.mew
index d72b881..3aa70bb 100644
--- a/tests/test.mew
+++ b/tests/test.mew
@@ -376,7 +376,6 @@
   (test 3 (len (generator 5 6 7)))
   (test-error (len #t)))
 
-; for
 (test-group "dup"
   (let* ((x (list (list "foo") "bar"))
          (y1 (dup x))
@@ -387,6 +386,15 @@
     (test #f (eq? (car x) (car y3)))
     (test #t (eq? (caar x) (caar y3)))))
 
+(test-group "for"
+  (test 3 (accumulate (c (count-accumulator))
+            (for (x (vector 3 4 5))
+              (c x))))
+  (test 2 (accumulate (c (count-accumulator))
+            (for (x (vector 3 4 5)
+                  y (vector 6 7))
+              (c x)))))
+
 
 (test-group "eof"
   (test-assert (eof-object? (eof))))