Created
June 1, 2019 14:53
-
-
Save vvviiimmm/447aed79fcf9a4ba93bbfb7566e3de1d to your computer and use it in GitHub Desktop.
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
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