Skip to content

Instantly share code, notes, and snippets.

Created June 2, 2016 06:24
Show Gist options
  • Save anonymous/f90ca2bffd5461b25063fc88d43bb1cd to your computer and use it in GitHub Desktop.
Save anonymous/f90ca2bffd5461b25063fc88d43bb1cd to your computer and use it in GitHub Desktop.
the description for this gist
object InterpretOpt extends (Instruction ~> Option) {
import Computations._
val nonNegative: (Position) => Option[Position] = {
p => if (p.x >= 0 &&p.y >= 0) Some(p) else None
}
override def apply[A](fa: Instruction[A]) = fa match {
case Forward(p, length) => nonNegative(forward(p, length))
case Backward(p, length) => nonNegative(backward(p, length))
case RotateLeft(p, degree) => Some(left(p, degree))
case RotateRight(p, degree) => Some(right(p, degree))
case ShowPosition(p) => Some(println(s"showing position $p"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment