Take this simple bit of code:
var line = "";
do {
println("Please enter a non-empty line: ")
line = readLine()
} while (line.isEmpty())
println("You entered a non-empty line: " + line)
This is definitely not particularly elegant, especially with bad browsing line- however, I think it's pretty easy to read.
Now, trying to translate this directly to the scalaz effect, I came up with:
def nonEmptyLine: IO[String] = for {
_ <- putStrLn("Please enter a non-empty line:")
line <- readLn
r <- if (line.isEmpty()) nonEmptyLine else IO(line)
} yield r
(for {
line <- nonEmptyLine
_ <- putStrLn("You entered a non-empty line: " + line)
} yield ()).unsafePerformIO
What makes me feel like I'm missing out on something, since that doesn't sound like an improvement at all? Is there any higher order flow control that I am missing?
source
share