Created
May 16, 2016 12:32
-
-
Save fomkin/d5380dff4cc8a93920c0eff75d93e3bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent.Future | |
import scala.language.{implicitConversions, higherKinds, postfixOps} | |
import scala.concurrent.ExecutionContext.Implicits.global | |
trait Kleisli[M[+_], -A, +B] { | |
def apply(a: A): M[B] | |
def >=>[C](k: Kleisli[M, B, C]): Kleisli[M, A, C] | |
def <=<[C](k: Kleisli[M, C, A]): Kleisli[M, C, B] | |
} | |
object futureKleisli { | |
case class FutureKleisli[A, B](run: A ⇒ Future[B]) extends Kleisli[Future, A, B] { self => | |
def apply(a: A) = run(a) | |
def >=>[C](k: Kleisli[Future, B, C]) = | |
FutureKleisli[A, C](a => self(a).flatMap(b => k(b))) | |
def <=<[C](k: Kleisli[Future, C, A]) = | |
FutureKleisli[C, B](a => k(a).flatMap(b => self(b))) | |
} | |
implicit def futureKleisli[A, B](f: A => Future[B]): Kleisli[Future, A, B] = | |
FutureKleisli(f) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment