Created
April 11, 2018 21:19
-
-
Save oxbowlakes/98448190fccc0c2acfd4b65be41ff5ba 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
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