Maps and reduces a tree to a single value.
- The
mapFn, if provided, will be invoked to map individual leaf values in the tree. The signature of this function is(value, key, tree)(the same as for thevaluefunction inTree.map.). If themapFnis null, values will be used as is. - For each level of the tree, the
reduceFnwill be called with the mapped values. Its signature is(values, keys, tree). The result of this function is used as the result of reducing this branch 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 mapReduce to distill this to a single total population number:
// population.ori
Tree.mapReduce(census.yaml, null, (values) => values.reduce((a, b) => a + b, 0))
Here the population number will be used as is, so the mapFn parameter will be null. The reduceFn will be called with an array of values and keys for each level of the tree. The reduceFn above uses the JavaScript array reduce to add up the numbers in the array.
$ ori population.ori/
263