Skip to content

Instantly share code, notes, and snippets.

@saml
Created August 3, 2015 18:19
Show Gist options
  • Select an option

  • Save saml/72a4b5671ce420d14b46 to your computer and use it in GitHub Desktop.

Select an option

Save saml/72a4b5671ce420d14b46 to your computer and use it in GitHub Desktop.
// 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