archimedes.tree.reduceยถ

archimedes.tree.reduce(
function: Callable[[V, ArrayLike], V],
tree: Tree,
initializer: V,
is_leaf: Callable[[Any], bool] | None = None,
) Vยถ

Reduce a tree to a single value using a function and initializer.

Traverses the tree, applying the reduction function to each leaf and an accumulator, similar to Pythonโ€™s built-in functools.reduce but operating on all leaves of a tree.

Parameters:
  • function (callable) โ€“ A function of two arguments: (accumulated_result, leaf_value) that returns a new accumulated result.

  • tree (Tree) โ€“ A tree to reduce.

  • initializer (Any) โ€“ The initial value for the accumulator.

  • is_leaf (callable, optional) โ€“ A function that takes a tree node as input and returns a boolean indicating whether it should be considered a leaf.

Returns:

result โ€“ The final accumulated value after applying the function to all leaves.

Return type:

Any

See also

tree_map

Apply a function to each leaf in a tree

tree_leaves

Extract just the leaf values from a tree