Skip to content

Instantly share code, notes, and snippets.

@yusefnapora
Created May 13, 2016 14:41
Show Gist options
  • Save yusefnapora/03863b5f0acc63c3d38f1183462c7af5 to your computer and use it in GitHub Desktop.
Save yusefnapora/03863b5f0acc63c3d38f1183462c7af5 to your computer and use it in GitHub Desktop.
Applicative instance for scala Future using cats
object Foo {
// based on this scalaz version: https://gist.github.com/Fristi/e7139a89e2c66d455e97
implicit def applicativeFuture(implicit executionContext: ExecutionContext)
= new Applicative[Future] {
override def pure[A](x: A): Future[A] = Future.successful(x)
override def ap[A, B](ff: Future[(A) => B])(fa: Future[A]): Future[B] =
ff.flatMap(fa.map(_))
override def map[A, B](fa: Future[A])(f: (A) => B): Future[B] = fa.map(f)
override def product[A, B](fa: Future[A], fb: Future[B]): Future[(A, B)] =
for {
a <- fa
b <- fb
} yield (a, b)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment