summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2022-10-30 18:00:39 +0100
committerLeah Neukirchen <leah@vuxu.org>2022-10-30 18:00:39 +0100
commit4c876b0fa6636e2a661594829cd5dc7421241cf7 (patch)
tree13eb161afe8dac746f0502d71e111552c8a32e3e
parentc703c1170def0cf0a41282a713665ac91847f7da (diff)
downloadmew-4c876b0fa6636e2a661594829cd5dc7421241cf7.tar.gz
mew-4c876b0fa6636e2a661594829cd5dc7421241cf7.tar.xz
mew-4c876b0fa6636e2a661594829cd5dc7421241cf7.zip
add tally-accumulator, group-by-accumulator
-rw-r--r--mew.scm19
-rw-r--r--mew.svnwiki10
2 files changed, 27 insertions, 2 deletions
diff --git a/mew.scm b/mew.scm
index d05f8bb..67bedc9 100644
--- a/mew.scm
+++ b/mew.scm
@@ -4,7 +4,8 @@
      dec def div
      empty? eof esc
      fin final for generic-for-each
-     get gen genumerate gfix given giterate gmatch gsplit gwindow
+     get gen genumerate gfix given giterate gmatch group-by-accumulator
+     gsplit gslice-when gwindow
      inc into
      keys
      len loc
@@ -14,7 +15,7 @@
      prn puts
      rep
      set str slurp
-     tbl time
+     tally-accumulator tbl time
      while
      until
      vals
@@ -518,6 +519,20 @@
          body ...
          (var (eof))))))
 
+  (define (tally-accumulator)
+    (let ((tally (tbl)))
+      (lambda (x)
+        (if (eof-object? x)
+            tally
+            (hash-table-update!/default tally x inc 0)))))
+
+  (define (group-by-accumulator f)
+    (let ((groups (tbl)))
+      (lambda (x)
+        (if (eof-object? x)
+            groups
+            (hash-table-update!/default groups (f x) (op cons x _) '())))))
+
   (define-syntax one-of
     (er-macro-transformer
      (lambda (expr rename compare)
diff --git a/mew.svnwiki b/mew.svnwiki
index 2339394..93e2f01 100644
--- a/mew.svnwiki
+++ b/mew.svnwiki
@@ -253,6 +253,16 @@ Finally, return the result of accumulation.
 If {{<acc>}} is a list/vector/hash-table/string, accumulate into
 a corresponding accumulator initialized to {{<acc>}}.
 
+<procedure>(tally-accumulator)</procedure>
+
+Returns an accumulator that counts how often each element was
+accumulated.  The accumulator results in a hash-table of objects to
+numbers.
+
+<procedure>(group-by-accumulator <f>)<procedure>
+
+Returns an accumulator that stores all elements in lists in a hash-table,
+applying {{<f>}} to the element to compute the key.
 
 == Regular expressions