Skip to content

Instantly share code, notes, and snippets.

@arturaz
Created March 21, 2015 09:46
Show Gist options
  • Select an option

  • Save arturaz/d85c8dcce2b3a23aad1e to your computer and use it in GitHub Desktop.

Select an option

Save arturaz/d85c8dcce2b3a23aad1e to your computer and use it in GitHub Desktop.
SafeFizzBuzz
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