I am trying to write the following function
def haltOnUserInput[O](process: Process[Task, O]): Process[Task, O]
which stops the process when the user sends a string to stdin . In this scenario, it is normal to wait for the current calculation to complete in the process before ending the process itself.
I tried the following:
scala> :paste // Entering paste mode (ctrl-D to finish) import scalaz.{ -\/, \/-, \/ } import scalaz.stream._ import scalaz.concurrent.Task def haltOnUserInput[O](process: Process[Task, O]): Process[Task, O] = { process.either(io.stdInLines).flatMap { case -\/(o) => Process.emit(o) case \/-(_) => println("here"); Process.halt } }
And so I test:
scala> val oneSec = scala.concurrent.duration.Duration("1s") oneSec: scala.concurrent.duration.Duration = 1 second scala> val test = haltOnUserInput(Process.awakeEvery(oneSec)).take(10).map(_.toString).to(io.stdOutLines).run test: scalaz.concurrent.Task[Unit] = scalaz.concurrent.Task@7a5e41bb scala> test.run 1000144294 nanoseconds 2000148316 nanoseconds here 3000130736 nanoseconds here 4000124898 nanoseconds 5000189134 nanoseconds 6000201269 nanoseconds here 7000127797 nanoseconds 8000132194 nanoseconds 9000191001 nanoseconds 10000126974 nanoseconds
As you can see, user input is confirmed ("here" is printed several times), but the process is not interrupted. I'm not sure that flatMap behaves as expected by wrt Process.halt .
How to write haltOnUserInput ?
source share