summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2022-11-18 20:11:31 +0100
committerLeah Neukirchen <leah@vuxu.org>2022-11-18 20:11:31 +0100
commit5bde1a7f4936e991c86da20fe0a143b527161ae2 (patch)
treea3ff2065b86b20ec1ef0b4eabd047dc0dee18c4b
parent66cd2fda549d7708acb8685933e619b0922885d5 (diff)
downloadmew-5bde1a7f4936e991c86da20fe0a143b527161ae2.tar.gz
mew-5bde1a7f4936e991c86da20fe0a143b527161ae2.tar.xz
mew-5bde1a7f4936e991c86da20fe0a143b527161ae2.zip
inject: use generator-xfold, immediately xfold if generator is passed
-rw-r--r--mew.scm6
-rw-r--r--mew.svnwiki5
-rw-r--r--tests/test.mew3
3 files changed, 9 insertions, 5 deletions
diff --git a/mew.scm b/mew.scm
index e4a07c0..e3b88c3 100644
--- a/mew.scm
+++ b/mew.scm
@@ -773,9 +773,11 @@
                     (v (g)))
                (if (eof-object? v)
                  (f)
-                 (generator-fold f v g)))))
+                 (generator-xfold f v g)))))
       ((f v) (lambda (o)
-               (generator-fold f v (gen o))))))
+               (generator-xfold f v (gen o))))
+      ((f v g)
+       ((inject f v) (gen g)))))
 
   (define (sing? l)
     (match l
diff --git a/mew.svnwiki b/mew.svnwiki
index 3b4fb3f..629d11b 100644
--- a/mew.svnwiki
+++ b/mew.svnwiki
@@ -458,12 +458,13 @@ Like {{generator-fold}}, but {{<f>}} always takes the accumulator as
 first arguments, and the items after.  This is more practical when
 multiple {{<generators>}} are passed.
 
-<procedure>(inject <f> <init>?)</procedure>
+<procedure>(inject <f> <init>? <g>?)</procedure>
 
 Returns a procedure that takes an generator (or something convertible
-by {{gen}}) and folds the function {{<f>}} over its values.  If given,
+by {{gen}}) and xfolds the function {{<f>}} over its values.  If given,
 folding starts with {{<init>}}, else with the first element yielded by
 the generator.  If the generator is empty, return {{(<f>)}}.
+If {{<g>}} is passed, immediately fold over the generator {{<g>}}.
 
 <procedure>(odometer <wheels>...)</procedure>
 
diff --git a/tests/test.mew b/tests/test.mew
index ddf376c..7782fb6 100644
--- a/tests/test.mew
+++ b/tests/test.mew
@@ -444,7 +444,8 @@
   (test 0 ((inject +) (generator)))
   (test 20 ((inject + 10) (generator 1 2 3 4)))
   (test 10 ((inject + 10) (generator)))
-  (test '((1 . 2) . 3) ((inject xcons) (generator 1 2 3))))
+  (test '((1 . 2) . 3) ((inject cons) (generator 1 2 3)))
+  (test 20 (inject + 10 #(1 2 3 4))))
 
 (test-group "sing?"
   (test #f (sing? '()))