Dictionary reduce, prepare members-base usage.
This commit is contained in:
parent
e26b78b254
commit
77204b80e4
3 changed files with 56 additions and 5 deletions
|
@ -34,6 +34,7 @@
|
|||
dict-keys
|
||||
dict-map
|
||||
dict-filter
|
||||
dict-reduce
|
||||
dictionary-tests!
|
||||
)
|
||||
|
||||
|
@ -117,6 +118,16 @@
|
|||
(cons (car d) r)
|
||||
r)))))
|
||||
|
||||
;; 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)
|
||||
(acc init))
|
||||
(if (null? d)
|
||||
acc
|
||||
(loop (cdr d)
|
||||
(proc acc (caar d) (cdar d))))))
|
||||
|
||||
;; Performs self-tests of the dictionary module.
|
||||
(define (dictionary-tests!)
|
||||
(run-tests
|
||||
|
@ -144,6 +155,9 @@
|
|||
'((a . 1)
|
||||
(b . 2)))
|
||||
'((a . 1)))
|
||||
(test-eq? dict-reduce
|
||||
(dict-reduce 0 (lambda (a k v) (+ a v)) '((a . 1) (b . 2)))
|
||||
3)
|
||||
))
|
||||
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue