map_leaf method for tree

This commit is contained in:
Bertrand Benjamin 2018-01-21 11:52:22 +03:00
parent 813c1f096e
commit e64613f5be
1 changed files with 14 additions and 2 deletions

View File

@ -194,15 +194,27 @@ class Tree(object):
... ("*", (3, 4)),
... 2))
>>> t = Tree.from_nested_parenthesis(nested_par)
>>> print(t)
+
> *
| > 3
| > 4
> 2
>>> print(t.map_leaf(lambda x:2*x))
+
> *
| > 6
| > 8
> 4
"""
try:
left_applied = self.left_value.apply(function)
left_applied = self.left_value.map_leaf(function)
except AttributeError:
left_applied = function(self.left_value)
try:
right_applied = self.right_value.apply(function)
right_applied = self.right_value.map_leaf(function)
except AttributeError:
right_applied = function(self.right_value)