summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2022-10-31 18:04:45 +0100
committerLeah Neukirchen <leah@vuxu.org>2022-10-31 18:04:45 +0100
commit60554dbaea5b76f152999804031796c242735a50 (patch)
treeb89619bc51b5f8dc7ac1356582658ce30a809c5e
parent9660e016ba05d53d66cc0bd7f667a10e1d18683d (diff)
downloadmew-60554dbaea5b76f152999804031796c242735a50.tar.gz
mew-60554dbaea5b76f152999804031796c242735a50.tar.xz
mew-60554dbaea5b76f152999804031796c242735a50.zip
add inject
-rw-r--r--mew.scm15
-rw-r--r--mew.svnwiki7
2 files changed, 20 insertions, 2 deletions
diff --git a/mew.scm b/mew.scm
index e90ea98..70edbdd 100644
--- a/mew.scm
+++ b/mew.scm
@@ -6,12 +6,12 @@
      empty? eof esc
      fin final for fun*
      gen generic-for-each genumerate get gfix giterate given gmatch group-by-accumulator gslice-when gsplit gwindow
-     inc into
+     inc inject into
      keys
      len loc
      mod
      negate nth
-     one-of op
+     one-of op op*
      per prn puts
      rep
      set str slurp
@@ -549,4 +549,15 @@
 
   (define (per . args)
     (apply comp (reverse args)))
+
+  (define inject
+    (case-lambda
+      ((f) (lambda (o)
+             (let* ((g (gen o))
+                    (v (g)))
+               (if (eof-object? v)
+                 (f)
+                 (generator-fold f v g)))))
+      ((f v) (lambda (o)
+               (generator-fold f v (gen o))))))
 )
diff --git a/mew.svnwiki b/mew.svnwiki
index bbe4a44..49831a9 100644
--- a/mew.svnwiki
+++ b/mew.svnwiki
@@ -281,6 +281,13 @@ numbers.
 Returns an accumulator that stores all elements in lists in a hash-table,
 applying {{<f>}} to the element to compute the key.
 
+<procedure>(inject <f> [<init>])</procedure>
+
+Returns a procedure that takes an generator (or something convertible
+by {{gen}}) and folds 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>)}}.
+
 == Regular expressions
 
 <procedure>(?~ <str> <irx>)</procedure>