def treeapply(tree, join, leaf=identity):
 for typ in join:
  if isinstance(tree, typ):
   return join[typ](*map(partial(treeapply, join=join, leaf=leaf), tree))
 return leaf(tree)

def treeapply ( tree , join , leaf = identity ) : for typ in join : if isinstance ( tree , typ ) : return join [ typ ] ( * map ( partial ( treeapply , join = join , leaf = leaf ) , tree ) ) return leaf ( tree )
apply functions onto recursive containers join - a dictionary mapping container types to functions e .