Skip to content

Instantly share code, notes, and snippets.

Created April 7, 2014 04:01
Show Gist options
  • Save anonymous/10014650 to your computer and use it in GitHub Desktop.
Save anonymous/10014650 to your computer and use it in GitHub Desktop.
Scala.jsFiddle gist
@JSExport
object ScalaJSExample{
@JSExport
def main(args: Array[String]): Unit = {
sealed trait Expr
case class And(l: Expr, r: Expr) extends Expr
case class Or(l: Expr, r: Expr) extends Expr
case class Neg(o: Expr) extends Expr
case class Xor(l: Expr, r: Expr) extends Expr
case class Value(v: Boolean) extends Expr
case class Var(ident: String) extends Expr
def eval(expr: Expr)(implicit map: Map[String, Boolean]): Boolean = {
expr match {
case Value(v) => v
case And(l, r) => eval(l) && eval(r)
case Or(l, r) => eval(l) || eval(r)
case Neg(o) => !eval(o)
case Xor(l, r) => {
val lv = eval(l)
val rv = eval(r)
lv && !rv || !lv && rv
}
case Var(ident) => map(ident)
}
}
def possibilities(n: Int): List[List[Boolean]] = {
val outerList = for(i <- 0 to n) yield {
val innerList = List.fill(i)(true) ++ List.fill(n - i)(false)
innerList.permutations.toList
}
outerList.toList.flatten
}
val ast = And(Value(true), Var("foo"))
println(eval(ast)(Map("foo" -> false)))
println("hi")
def genEnvironment(vars: Set[String]): List[Map[String, Boolean]] = {
def possibilities(n: Int): List[List[Boolean]] = {
val outerList = for (i <- 0 to n) yield {
val innerList = List.fill(i)(true) ++ List.fill(n - i)(false)
innerList.permutations.toList
}
outerList.toList.flatten
}
val varList = vars.toList
val values = possibilities(varList.length)
for (iteration <- values)
yield Map(varList.zip(iteration): _*)
}
genEnvironment(Set("a", "b", "c")).foreach(x => println(x))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment