Skip to content

Instantly share code, notes, and snippets.

@ezhulenev
Created April 12, 2014 19:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezhulenev/10553038 to your computer and use it in GitHub Desktop.
Save ezhulenev/10553038 to your computer and use it in GitHub Desktop.
scalaz-stream equivalent to Play Framework's Enumerator.fromCallback
import java.util.concurrent.atomic.AtomicInteger
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.future
import scala.util.{Failure, Success}
import scalaz.concurrent.Task
import scalaz.stream.Process.End
import scalaz.stream._
import scalaz.{-\/, \/-}
val cnt = new AtomicInteger(0)
def serviceCall: Future[Option[String]] = future {
if (cnt.incrementAndGet() <= 10) Some(s"Task #${cnt.get()}") else None
}
val task: Task[String] = Task.async(cb => serviceCall onComplete {
case Success(Some(str)) => cb(\/-(str))
case Success(None) => cb(-\/(End))
case Failure(err) => cb(-\/(err))
})
Process.repeatEval(task).runLog.run.foreach(println)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment