Add a tree-fold function to iterate over the tree and build a single value.

This commit is contained in:
Feufochmar 2022-05-20 19:40:40 +02:00
parent 997530b506
commit e13da0b71e
1 changed files with 12 additions and 1 deletions

View File

@ -4,7 +4,7 @@
tree tree? tree-key tree-value tree-children
make-tree tree-ref tree-set!
tree-node tree-child tree->list
tree-for-each tree-find-path)
tree-for-each tree-find-path tree-fold)
; Tree structures, with key and possible value on nodes
; Like a table, with a list of keys as reference to a value
@ -79,3 +79,14 @@
(map
(lambda (c) (tree-find-path c pred (cons (tree-key tr) path)))
(tree-children tr)))))
; Fold the tree into a single value
; This works around the fun function who takes two arguments : the value of a node, the list of results of tree-fold applied recursively on all children
(define (tree-fold tr fun)
(fun
(tree-value tr)
(map
(lambda (x)
(tree-fold x fun))
(tree-children tr))))