Skip to content

Instantly share code, notes, and snippets.

@oxbowlakes
Created April 11, 2018 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oxbowlakes/98448190fccc0c2acfd4b65be41ff5ba to your computer and use it in GitHub Desktop.
Save oxbowlakes/98448190fccc0c2acfd4b65be41ff5ba to your computer and use it in GitHub Desktop.
object EquationalReasoningBreakdown extends SafeApp {
import scalaz.effect._
def parse(ioa: IO[String]): IO[Int] =
ioa
.flatMap { s =>
IO(s.toInt)
}
.except { case NonFatal(_) =>
IO(-1)
}
val runc: IO[Unit] =
parse(IO.readStrLn)
.map(i => s"Thanks for $i")
.flatMap(IO.putStrLn)
}
// Looking at the signature of parse alone, it'stempting to conclude that the program can be refactored as follows:
object EquationalReasoningBreakdown extends SafeApp {
import scalaz.effect._
def parse(a: String): IO[Int] =
IO(s.toInt)
.except { case NonFatal(_) =>
IO(-1)
}
val runc: IO[Unit] =
IO.readStrLn
.flatMap(parse)
.map(i => s"Thanks for $i")
.flatMap(IO.putStrLn)
}
//These programs are equivalent, aren't they?
// Aren't they?
// No!
//
// In the first program, a failure in `IO.readStrLn` was handled by the error-handling in parse
// In the second, it was not
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment