Skip to content

Instantly share code, notes, and snippets.

@vvviiimmm
Created June 1, 2019 14:53
Show Gist options
  • Save vvviiimmm/447aed79fcf9a4ba93bbfb7566e3de1d to your computer and use it in GitHub Desktop.
Save vvviiimmm/447aed79fcf9a4ba93bbfb7566e3de1d to your computer and use it in GitHub Desktop.
import cats.effect.IO
import scala.util.{Failure, Success, Try}
object Main extends App {
/**
* Pure functions, we're good!
*/
def putStr(msg: String): IO[Unit] = IO(print(msg))
def getStr: IO[String] = IO(scala.io.StdIn.readLine())
def validateInt(str: String): IO[Int] = IO(str.toInt)
def factorial(n: Int): BigInt = Range.BigInt(1, n, 1).product
/**
* It's the end of the world now, no one cares.
*/
putStr("Please enter a number: ").unsafeRunSync()
val input = getStr.unsafeRunSync()
Try(validateInt(input).unsafeRunSync()) match {
case Success(n) =>
putStr(s"Factorial of $n is ${factorial(n)}").unsafeRunSync()
case Failure(_) => putStr("Invalid number").unsafeRunSync()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment