Created
August 3, 2015 18:19
-
-
Save saml/72a4b5671ce420d14b46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Expr is something that can be simplified. | |
| type Expr interface { | |
| simplify() Expr | |
| } | |
| // binary operator on int. | |
| type operator struct { | |
| op func(a int, b int) int | |
| a Expr | |
| b Expr | |
| } | |
| func valueOf(e Expr) (int, error) { | |
| switch t := e.(type) { | |
| } | |
| } | |
| func (o *operator) simplify() Expr { | |
| switch t := a.(type) { | |
| } | |
| } | |
| func add(a int, b int) int { | |
| return a + b | |
| } | |
| func mult(a int, b int) int { | |
| return a * b | |
| } | |
| func Add(a Expr, b Expr) Expr { | |
| return &operator{op: add, a: a, b: b} | |
| } | |
| func Mult(a Expr, b Expr) Expr { | |
| return &operator{op: mult, a: a, b: b} | |
| } | |
| Add( | |
| Mult( | |
| Add( | |
| Int(1), | |
| Mult( | |
| Int(0), | |
| Var("x") | |
| ) | |
| ), | |
| Int(3) | |
| ), | |
| Int(12) | |
| ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment