Skip to content

Instantly share code, notes, and snippets.

@Softsapiens
Forked from travisbrown/future-zip-n.scala
Created May 26, 2014 05:49
Show Gist options
  • Save Softsapiens/ad25119032008365bfde to your computer and use it in GitHub Desktop.
Save Softsapiens/ad25119032008365bfde 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(_)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment