def METHOD_NAME(tree, join, leaf=identity):
    """Apply functions onto recursive containers (tree).
    join - a dictionary mapping container types to functions
      e.g. ``{list: minimize, tuple: chain}``
    Keys are containers/iterables.  Values are functions [a] -> a.
    Examples
    ========
    >>> tree = [(3, 2), (4, 1)]
    >>> treeapply(tree, {list: max, tuple: min})
    2
    >>> def mul(*args):
    ...     total = 1
    ...     for arg in args:
    ...         total *= arg
    ...     return total
    >>> treeapply(tree, {list: mul, tuple: lambda *args: sum(args)})
    25
    """
    for typ in join:
        if isinstance(tree, typ):
            return join[typ](*map(functools.partial(METHOD_NAME, join=join, leaf=leaf),
                                  tree))
    return leaf(tree)