mwchase icon

Excerpt from draft of code using Structured Data

mwchase | PRO | 09/30/19 04:49:56 PM UTC | 0 ⭐ | 884 👁️ | Never ⏰ | []
Python |

450 B

|

None

|

0 👍

/

0 👎

class Node(adt.Sum):
    __slots__ = ()
 
    Inner: adt.Ctor[BinOp, Node, Node]
    Leaf: adt.Ctor[int]
 
    @match.function
    def eval(self) -> int:
        """Evaluate this node to an int."""
 
 
@Node.eval.when(self=Node.Inner(match.pat.opr, match.pat.left, match.pat.right))
def _eval_inner(opr, left, right):
    return opr(left.eval(), right.eval())
 
 
@Node.eval.when(self=Node.Leaf(match.pat.value))
def _eval_leaf(value):
    return value

Comments