Skip to content

Instantly share code, notes, and snippets.

@travisbrown
Created May 26, 2014 02:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save travisbrown/00fadaa6b51bf882ab92 to your computer and use it in GitHub Desktop.
Save travisbrown/00fadaa6b51bf882ab92 to your computer and use it in GitHub Desktop.
import scala.concurrent.{ ExecutionContext, Future }
import shapeless._, ops.tuple.{ IsComposite, LeftFolder, Prepend }
object zipAndAdd extends Poly2 {
implicit def only[B, A](implicit p: Prepend[B, Tuple1[A]], executor: ExecutionContext) =
at[Future[B], Future[A]] {
(fb, fa) => fb.zip(fa).map { case (b, a) => p(b, Tuple1(a)) }
}
}
def zipN[P, FH, H, T, O](p: P)(implicit
comp: IsComposite.Aux[P, FH, T],
ev: FH <:< Future[H],
folder: LeftFolder.Aux[T, Future[Tuple1[H]], zipAndAdd.type, O],
executor: ExecutionContext
): O = folder(comp.tail(p), comp.head(p).map(Tuple1(_)))
@travisbrown
Copy link
Author

And then:

import scala.concurrent.ExecutionContext.Implicits.global

val triple = (Future("foo"), Future(1), Future('a))
val zipped: Future[(String, Int, Symbol)] = zipN(triple)

In response to discussion of this Stack Overflow answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment