Skip to content

Instantly share code, notes, and snippets.

@NightRa
Last active November 20, 2015 09:35
Show Gist options
  • Select an option

  • Save NightRa/11056632 to your computer and use it in GitHub Desktop.

Select an option

Save NightRa/11056632 to your computer and use it in GitHub Desktop.

Revisions

  1. NightRa revised this gist Apr 18, 2014. 1 changed file with 12 additions and 10 deletions.
    22 changes: 12 additions & 10 deletions Task.scala
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@ package nightra.mashovNotificator.network

    import scala.concurrent.{ExecutionContext, Future}
    import scalaz.effect.IO
    import scalaz.effect.IO._
    import scalaz.Reader
    import scala.util.{Success, Failure, Try}

    @@ -21,22 +22,23 @@ case class Task[A](task: Reader[ExecutionContext, IO[Future[A]]]) {
    future
    })))

    def foreach(f: A => IO[Unit]): Task[A] = onComplete(t => IO(t.foreach(a => f(a).unsafePerformIO())))
    def foreach(f: A => IO[Unit]): Task[A] = onSuccess(f)

    def onSuccess(f: A => IO[Unit]): Task[A] = foreach(f)

    def run(implicit executor: ExecutionContext) = task.run(executor)

    def logAll: Task[A] = onComplete(t => IO(println(t)))
    def logFailure: Task[A] = onComplete{
    case Failure(f) => IO(println(f))
    def onSuccess(f: A => IO[Unit]): Task[A] = onComplete {
    case Success(value) => f(value)
    case _ => IO(())
    }

    def logSuccess: Task[A] = onComplete{
    case Success(value) => IO(println(value))
    def onFailure(f: Throwable => IO[Unit]): Task[A] = onComplete {
    case Failure(fail) => f(fail)
    case _ => IO(())
    }

    def run(implicit executor: ExecutionContext) = task.run(executor)

    def logAll(f: Try[A] => String): Task[A] = onComplete(t => putStrLn(f(t)))
    def logFailure(f: Throwable => String): Task[A] = onFailure(fail => putStrLn(f(fail)))
    def logSuccess(f: A => String): Task[A] = onSuccess(value => putStrLn(f(value)))
    }

    object Task {
  2. NightRa created this gist Apr 18, 2014.
    47 changes: 47 additions & 0 deletions Task.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    package nightra.mashovNotificator.network

    import scala.concurrent.{ExecutionContext, Future}
    import scalaz.effect.IO
    import scalaz.Reader
    import scala.util.{Success, Failure, Try}

    case class Task[A](task: Reader[ExecutionContext, IO[Future[A]]]) {
    def map[B](f: A => B): Task[B] = Task(Reader(implicit executor => task(executor).map(_.map(f))))

    def flatMap[B](f: A => Task[B]): Task[B] = Task(Reader(implicit executor => IO {
    task.run(executor).unsafePerformIO().flatMap(a => f(a).task.run(executor).unsafePerformIO())
    }))

    def filter(p: A => Boolean): Task[A] = Task(Reader(implicit executor => task(executor).map(_.filter(p))))
    def withFilter(p: A => Boolean): Task[A] = filter(p)

    def onComplete(f: Try[A] => IO[Unit]): Task[A] =
    Task(Reader(implicit executor => task(executor).map(future => {
    future.onComplete(t => f(t).unsafePerformIO())
    future
    })))

    def foreach(f: A => IO[Unit]): Task[A] = onComplete(t => IO(t.foreach(a => f(a).unsafePerformIO())))

    def onSuccess(f: A => IO[Unit]): Task[A] = foreach(f)

    def run(implicit executor: ExecutionContext) = task.run(executor)

    def logAll: Task[A] = onComplete(t => IO(println(t)))
    def logFailure: Task[A] = onComplete{
    case Failure(f) => IO(println(f))
    case _ => IO(())
    }

    def logSuccess: Task[A] = onComplete{
    case Success(value) => IO(println(value))
    case _ => IO(())
    }
    }

    object Task {
    def lift[A](f: ExecutionContext => Future[A]): Task[A] =
    Task(Reader(executor => IO {
    f(executor)
    }))
    }