Add a tree-fold function to iterate over the tree and build a single value.
This commit is contained in:
parent
997530b506
commit
e13da0b71e
|
@ -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))))
|
||||
|
||||
|
|
Loading…
Reference in New Issue