Reimplement and document ldict-reduce.

This commit is contained in:
Dominik Pantůček 2023-04-09 20:06:02 +02:00
parent 90fa195dc5
commit 6a35b94f34
2 changed files with 16 additions and 5 deletions

View file

@ -121,6 +121,17 @@ Returns a new dictionary containing only key/value pairs matching the
given predicate. The procedure ```pred?``` must accept two arguments - given predicate. The procedure ```pred?``` must accept two arguments -
the key and the value. 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 ### IO
(import util-io) (import util-io)

View file

@ -193,13 +193,13 @@
;; Reduce over dictinary, the reducing procedure gets accumulator, ;; Reduce over dictinary, the reducing procedure gets accumulator,
;; key and value as its three arguments. ;; key and value as its three arguments.
(define (dict-reduce init proc d) (define (ldict-reduce init proc ld)
(let loop ((d d) (let loop ((pairs (ldict-pairs ld))
(acc init)) (acc init))
(if (null? d) (if (null? pairs)
acc acc
(loop (cdr d) (loop (cdr pairs)
(proc acc (caar d) (cdar d)))))) (proc acc (caar pairs) (cdar pairs))))))
;; Functional update ;; Functional update
(define (dict-update d k proc . dfls) (define (dict-update d k proc . dfls)