scalaz - Printing Input + Function Output with IO -
i wrote simple function (i intended) print out (1) user input + (2) input "foobar" added it:
import scalaz.effect.io import scalaz.effect.io._ def simpleprint: (io[unit], io[string]) = { val input = readln val result = input.map(_ + "foobar") (input.flatmap(putstrln), result) } def runexample: unit = { val (in, result) = simpleprint println(">" + in.unsafeperformio) println(result.unsafeperformio) } entering repl, tested out.
when ran runexample method, typed "555", saw output, , typed "1234".
scala> net.repl.foo.runexample 555 >() 1234foobar i had expected type: "555", , see:
>555 555foobar how can change above method this?
if you're calling unsafeperformio more once, there's problem program (and ideally you'd use safeapp , not call @ all). mixing untracked effects , io unnecessarily messy. want all of effects sequenced, this:
scala> val action = putstrln(">") >> readln.map(_ + "foobar") >>= putstrln action: scalaz.effect.io[unit] = scalaz.effect.iofunctions$$anon$6@5a2ab2df scala> action.unsafeperformio > 555foobar this doesn't echo characters while typing, though. i'm pretty sure i've written code (with getchar , hand-rolled unfoldwhilem), , try dig later if you're interested.
Comments
Post a Comment