Skip to content

Instantly share code, notes, and snippets.

@kevinmeredith
Created June 16, 2017 21:00
Show Gist options
  • Save kevinmeredith/2ef318859ddfb9cbdbb4ba1f8e2570c1 to your computer and use it in GitHub Desktop.
Save kevinmeredith/2ef318859ddfb9cbdbb4ba1f8e2570c1 to your computer and use it in GitHub Desktop.
Example from Stucchio's blog on scalaz-stream's `signal`
import scalaz._, Scalaz._, concurrent._, stream.Process, stream.async

val signal = async.signal[Boolean]
val signalChanges: Process[Task,Boolean] = signal.discrete

val updateSignal: Task[Unit] = Task.apply {
  signal.set(true).run // Time = 1
  Thread.sleep(2000)
  signal.set(true).run // Time = 2
  Thread.sleep(2000)
  signal.set(false).run //Time = 3
}

val monitorSignal: Task[Unit] = Task.apply {
signalChanges.map(x => {
  println("" + x + " -> " + System.currentTimeMillis)
  }).take(3).run.run
}

scala> Task.gatherUnordered(List(updateSignal, monitorSignal)).run
true -> 1497646419142
true -> 1497646421127
false -> 1497646423129
res2: List[Unit] = List((), ())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment