diff --git a/doc/d-utils.md b/doc/d-utils.md index cf72484..019c05f 100644 --- a/doc/d-utils.md +++ b/doc/d-utils.md @@ -562,6 +562,15 @@ Functional update with optional default value (defaults to #f). Returns a BST with only KV pairs matching the predicate which must accept two arguments. +### bst-reduce [procedure] + + (bst-reduce bst + proc + init) + +Like generic reduce, the proc gets accumulator, key and value +arguments. + ## util-bst-bdict [module] (import util-bst-bdict) diff --git a/src/util-bst-ldict.scm b/src/util-bst-ldict.scm index a87cf78..46b3232 100644 --- a/src/util-bst-ldict.scm +++ b/src/util-bst-ldict.scm @@ -45,4 +45,7 @@ (define (ldict-filter pred? ld) (bst-filter ld pred?)) + (define (ldict-reduce init proc ld) + (bst-reduce ld proc init)) + ) diff --git a/src/util-bst.scm b/src/util-bst.scm index c5d4acf..2cc7280 100644 --- a/src/util-bst.scm +++ b/src/util-bst.scm @@ -36,6 +36,7 @@ bst-update bst-filter + bst-reduce util-bst-tests! ) @@ -385,6 +386,15 @@ accept two arguments.") (cons (apply vector (bst-filter-pairs bst pred?)) (cddr bst))))) + (define/doc (bst-reduce bst proc init) + ("Like generic reduce, the proc gets accumulator, key and value +arguments.") + (let ((acc init)) + (bst-iter-kv bst + (lambda (kv) + (set! acc (proc acc (car kv) (cdr kv))))) + acc)) + ;; Module self-tests (define (util-bst-tests!) (run-tests