Fix(API): Rewrite tree apply_on_last_level and reorganise simplify

This commit is contained in:
2018-11-19 12:23:37 +01:00
parent 6edf4fe5b4
commit e37a8a5a2e
4 changed files with 67 additions and 29 deletions

View File

@@ -18,7 +18,7 @@ Abstracts tools for calculs manipulations
> *
| > 3
| > 4
>>> print(t.apply_on_last_level(compute, typing))
>>> print(t.apply_on_last_level(compute))
+
> 2
> 12
@@ -34,7 +34,12 @@ Abstracts tools for calculs manipulations
> /
| > 3
| > 4
>>> print(t.apply_on_last_level(compute, typing))
>>> print(t.apply_on_last_level(compute))
Traceback (most recent call last):
...
NotImplementedError: Can't divide 2 int. Need to create a Fraction instead
>>> tt = t.apply_on_last_level(typing)
>>> print(tt.apply_on_last_level(compute))
+
> /
| > 2
@@ -42,7 +47,12 @@ Abstracts tools for calculs manipulations
> /
| > 3
| > 4
>>> type(t.right_value)
<class 'mapytex.calculus.core.tree.Tree'>
>>> type(tt.right_value)
<class 'mapytex.calculus.core.MO.fraction.MOFraction'>
>>> tt.right_value
<MOFraction 3 / 4>
>>> t = Tree.from_str("2+3x")
>>> print(t)
@@ -56,7 +66,7 @@ Abstracts tools for calculs manipulations
from .tree import Tree, AssocialTree
from .compute import compute
from .typing import typing
from .typing import typing, TypingError
from .renders import tree2txt, tree2tex

View File

@@ -294,13 +294,10 @@ class Tree(object):
return Tree(self.node, left_applied, right_applied)
def apply_on_last_level(self,
function,
fallback = lambda n,l,r: Tree(n,l,r)):
def apply_on_last_level(self, function):
""" Apply the function on last level of the tree before leaf
:param function: (op, a, a) -> b function to apply on last level
:param fallback: (op, a, a) -> b fallback function to apply on subtree which raise a NotImplementedError. The return value will be concidered as an leaf
:returns: b if it is a 1 level Tree, Tree otherwise
@@ -330,30 +327,17 @@ class Tree(object):
"""
left_is_leaf = 0
right_is_leaf = 0
try:
left_applied = self.left_value.\
apply_on_last_level(function, fallback)
except NotImplementedError:
# TODO: overload __getitem__ to ease unpacking |dim. sept. 23 19:17:50 CEST 2018
left_is_leaf = 1
left_tree = self.left_value
left_applied = fallback(left_tree.node,
left_tree.left_value,
left_tree.right_value)
apply_on_last_level(function)
except AttributeError:
left_applied = self.left_value
left_is_leaf = 1
right_is_leaf = 0
try:
right_applied = self.right_value.\
apply_on_last_level(function, fallback)
except NotImplementedError:
right_is_leaf = 1
right_tree = self.right_value
right_applied = fallback(right_tree.node,
right_tree.left_value,
right_tree.right_value)
apply_on_last_level(function)
except AttributeError:
right_applied = self.right_value
right_is_leaf = 1