Skip to content

Instantly share code, notes, and snippets.

@semanticer
Last active March 10, 2016 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save semanticer/b448eb0d0e3ab9d6ba40 to your computer and use it in GitHub Desktop.
Save semanticer/b448eb0d0e3ab9d6ba40 to your computer and use it in GitHub Desktop.
trait Exp {
def eval: Int
}
case class Oper(op: (Int, Int) => Int, left: Exp, right: Exp) extends Exp {
override def eval: Int = op(left.eval, right.eval)
}
case class Num(value: Int) extends Exp {
override def eval: Int = value
}
object Exp {
def eval(exp: Exp): Int = {
exp match {
case Oper(op, l, r) =>
op(eval(r), eval(l))
case Num(v) => v
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment