Created
November 1, 2011 22:21
-
-
Save hodzanassredin/1332136 to your computer and use it in GitHub Desktop.
first code in scala language
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
| package Hodza | |
| abstract class Expr() {} | |
| class TrueExpr extends Expr {} | |
| class FalseExpr extends Expr {} | |
| class Or[T <: Expr,U <: Expr](val left: T, val right:U) extends Expr {} | |
| trait Evaluator[T] { | |
| def eval(expr:T): Boolean | |
| } | |
| object EvalSimple{ | |
| implicit object TrueEvaluator extends Evaluator[TrueExpr] { | |
| def eval(expr : TrueExpr): Boolean = true | |
| } | |
| implicit object FalseEvaluator extends Evaluator[FalseExpr] { | |
| def eval(expr : FalseExpr): Boolean = false | |
| } | |
| } | |
| import EvalSimple._ | |
| object EvalOr{ | |
| implicit def orToEvaluator[A <: Expr, B <: Expr](implicit a: Evaluator[A], b: Evaluator[B]) : Evaluator[Or[A, B]] = { | |
| new Evaluator[Or[A, B]] { | |
| def eval(expr : Or[A, B]):Boolean = a.eval(expr.left) || b.eval(expr.right) | |
| } | |
| } | |
| } | |
| import EvalOr._ | |
| object HelloWorld { | |
| def evla[T](t: T) (implicit m: Evaluator[T]) : Boolean = m.eval(t) | |
| def main(args: Array[String]) { | |
| println(evla(new Or(new FalseExpr(), new FalseExpr()))) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment