Advertisement
mwchase

Excerpt from draft of code using Structured Data

Sep 30th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. class Node(adt.Sum):
  2.     __slots__ = ()
  3.  
  4.     Inner: adt.Ctor[BinOp, Node, Node]
  5.     Leaf: adt.Ctor[int]
  6.  
  7.     @match.function
  8.     def eval(self) -> int:
  9.         """Evaluate this node to an int."""
  10.  
  11.  
  12. @Node.eval.when(self=Node.Inner(match.pat.opr, match.pat.left, match.pat.right))
  13. def _eval_inner(opr, left, right):
  14.     return opr(left.eval(), right.eval())
  15.  
  16.  
  17. @Node.eval.when(self=Node.Leaf(match.pat.value))
  18. def _eval_leaf(value):
  19.     return value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement