Created
March 21, 2015 09:46
-
-
Save arturaz/d85c8dcce2b3a23aad1e to your computer and use it in GitHub Desktop.
SafeFizzBuzz
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
| object SafeFizzBuzz extends scalaz.effect.SafeApp { | |
| import scalaz._ | |
| import scalaz.effect._ | |
| import scalaz.std.string._ | |
| override def run(args: ImmutableArray[String]): IO[Unit] = { | |
| def int2answer(i: Int) = | |
| if (i % 3 == 0 && i % 5 == 0) Some("fizzbuzz") | |
| else if (i % 3 == 0) Some("fizz") | |
| else if (i % 5 == 0) Some("buzz") | |
| else None | |
| lazy val mainLoop: IO[Unit] = for { | |
| _ <- IO.putStr("Enter a number (done to finish): ") | |
| line <- IO.readLn.map(_.stripLineEnd) | |
| _ <- | |
| if (line == "done") IO.ioUnit | |
| else for { | |
| _ <- parseInt(line).fold( | |
| _ => IO.putStrLn(s"$line was not a number!"), | |
| int2answer(_).fold(IO.ioUnit)(IO.putStrLn) | |
| ) | |
| _ <- mainLoop | |
| } yield () | |
| } yield () | |
| mainLoop | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment