Scala, represent pattern of boolean tuple into something else.
Posted
by
Berlin Brown
on Stack Overflow
See other posts from Stack Overflow
or by Berlin Brown
Published on 2011-01-09T23:38:53Z
Indexed on
2011/01/09
23:53 UTC
Read the original article
Hit count: 180
scala
This is a cellular automata rule (input Boolean == Left, Center, Right Cell) and output Boolean .
What is a better way to represent this in Scala.
trait Rule {
def ruleId() : Int
def rule(inputState:(Boolean, Boolean, Boolean)) : Boolean
override def toString : String = "Rule:" + ruleId
}
class Rule90 extends Rule {
def ruleId() = 90
def rule(inputState:(Boolean, Boolean, Boolean)) : Boolean = {
// Verbose version, show all 8 states
inputState match {
case (true, true, true) => false
case (true, false, true) => false
case (false, true, false) => false
case (false, false, false) => false
case _ => true
}
}
}
© Stack Overflow or respective owner