From 6a35b94f34ebb12f2ebfe95ffec851d8ae288fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Sun, 9 Apr 2023 20:06:02 +0200 Subject: [PATCH] Reimplement and document ldict-reduce. --- doc/utils.md | 11 +++++++++++ src/util-dict.scm | 10 +++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/doc/utils.md b/doc/utils.md index 434450d..bf86b70 100644 --- a/doc/utils.md +++ b/doc/utils.md @@ -121,6 +121,17 @@ Returns a new dictionary containing only key/value pairs matching the given predicate. The procedure ```pred?``` must accept two arguments - the key and the value. + (ldict-reduce init proc ld) + +* ```init``` - initial accumulator value +* ```proc``` - accumulating procedure +* ```ld``` - a ldict instance + +Performs a reduce operation on the pairs of given dictionary using +```init``` as initial accumulator. The accumulating procedure must +accept three arguments: the value accumulated so far, the key and the +value. + ### IO (import util-io) diff --git a/src/util-dict.scm b/src/util-dict.scm index c8740c9..72443d7 100644 --- a/src/util-dict.scm +++ b/src/util-dict.scm @@ -193,13 +193,13 @@ ;; Reduce over dictinary, the reducing procedure gets accumulator, ;; key and value as its three arguments. - (define (dict-reduce init proc d) - (let loop ((d d) + (define (ldict-reduce init proc ld) + (let loop ((pairs (ldict-pairs ld)) (acc init)) - (if (null? d) + (if (null? pairs) acc - (loop (cdr d) - (proc acc (caar d) (cdar d)))))) + (loop (cdr pairs) + (proc acc (caar pairs) (cdar pairs)))))) ;; Functional update (define (dict-update d k proc . dfls)