Reduces a map-based tree to a single value. See also Tree.mapReduce, which simultaneously maps values.
For each interior node of the tree, the reduceFn will be called to reduce that node. Its signature is (node, tree), where the node is a map. The result of this function is used in place of that node when reducing the parent node; work proceeds towards the root of the tree.
Example: A file has population data in a tree structure that does not have a consistent depth:
# census.yaml
Akkala:
Tarrey Town: 17
Necluda:
Hateno Village: 26
Kakariko Village: 57
Lurelin Village: 64
Hebra: 67
Eldin: 32
You can use reduce to distill this to a single total population number:
// population.ori
Tree.reduce(census.yaml, (node) => Tree.values(node).reduce((a, b) => a + b, 0))
The reduceFn will be called on each node of the tree. Here the reduceFn calls the JavaScript array reduce to add up the numbers that are the node’s values.
$ ori population.ori/
263