From 28ad4b62515325463656627fa3d80a984f8c0fdb Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Fri, 16 Dec 2022 00:18:05 +0100 Subject: add parallel for --- mew.scm | 9 +++++++++ mew.svnwiki | 7 +++++-- tests/test.mew | 10 +++++++++- 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 {{}}, consisting of lists, vectors, hash-tables and strings. Create copies of values up to a level of {{}}, or infinitely by default. -(for ( ) ...) -(for (( . ) ) ...) +(for ( ...) ...) +(for (( . ) ...) ...) If {{}} is a list or a vector, iterate over its elements. If {{}} is a procedure, consider it a SRFI-158 generator and iterate over its values. If {{}} is a hash-table, iterate over its keys and values. +Multiple {{ }}-pairs may be specified, then {{for}} +iterates over these in *parallel*, stopping after the shortest ends. + (search ?) Returns the offset of the sequence (string/list/vector) {{}} 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)))) -- cgit 1.4.1