apply_on_last_branches is ok

This commit is contained in:
Bertrand Benjamin 2018-01-21 12:10:45 +03:00
parent e64613f5be
commit 0f3e0870fe
1 changed files with 25 additions and 3 deletions

View File

@ -226,17 +226,39 @@ class Tree(object):
:param function: (op, a, a) -> b
:returns: b if it is a 1 level Tree, Tree otherwise
:example:
>>> nested_par = ("+", (
... ("*", (3, 4)),
... 2))
>>> t = Tree.from_nested_parenthesis(nested_par)
>>> print(t)
+
> *
| > 3
| > 4
> 2
>>> from .tree_tools import infix_str_concatenate
>>> tt = t.apply_on_last_branches(lambda *x: infix_str_concatenate(*x))
>>> print(tt)
+
> 3 * 4
> 2
"""
left_is_leaf = 0
right_is_leaf = 0
try:
# TODO: to change when typify is done |dim. févr. 19 14:49:06 EAT 2017
left_applied = self.left_value.apply_on_last_branches(function)
except AttributeError:
left_applied = self.left_value
left_is_leaf = 1
try:
# TODO: to change when typify is done |dim. févr. 19 14:49:06 EAT 2017
left_applied = self.left_value.apply_on_last_branches(function)
right_applied = self.right_value.apply_on_last_branches(function)
except AttributeError:
right_applied = self.right_value
right_is_leaf = 1
if left_is_leaf and right_is_leaf: