Last active
December 10, 2015 19:29
-
-
Save timperrett/4481525 to your computer and use it in GitHub Desktop.
Solution to the candy machine problem from FP in Scala MEAP.
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
| scalaVersion := "2.10.0" | |
| libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.0.0-M7" | |
| resolvers += "sontatype.releases" at "https://oss.sonatype.org/content/repositories/releases/" |
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 demo | |
| sealed trait Input | |
| case object Coin extends Input | |
| case object Turn extends Input | |
| case class Machine(locked: Boolean, candies: Int, coins: Int) | |
| import scalaz.syntax.state._ | |
| import scalaz.State, State._ | |
| object Demo { | |
| def main(args: Array[String]): Unit = println { | |
| import Machine._ | |
| use(Coin :: Turn :: Coin :: Turn :: Coin :: Turn :: Nil) eval (default) | |
| } | |
| } | |
| object Machine { | |
| val default = Machine(locked = true, candies = 2, coins = 0) | |
| def use(ops: List[Input]): State[Machine, Int] = | |
| for { | |
| m <- init | |
| _ <- modify(ops.map(handleInput).reduce(_ andThen _)) | |
| r <- get | |
| } yield r.coins | |
| def handleInput(what: Input): Machine => Machine = m => | |
| what match { | |
| case Coin => | |
| if(m.locked == true && m.candies > 0) | |
| m.copy(locked = false, coins = m.coins+1) | |
| else m | |
| case Turn => | |
| if(!m.locked) | |
| m.copy(candies = m.candies-1, locked = true) | |
| else m | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment