Is there an easy way to convert a tuple of type (Future[A], Future[B], Future[C], ..., Future[N]) to Future[(A, B, C, ..., N)]? This assumes an undefined number of elements in the tuple.
I've tried converting the tuple to HList and tried a similar trick of foldLeft and for comprehension as done in Future.sequence but no luck dealing with the types passed into the fold. Tried the same with recursive functions. But this code still does not compile due to missing HList.head, HList.tail. The code looks like follows:
def sequence[L <: HList](in: L)(implicit ihc: IsHCons[L]) = {
val list = for (i <- in.head; j <- in.tail.head) yield HList(i, j)
@tailrec
def sequence2(fList: Future[HList], listF: HList): Future[HList] = {
if (listF == HNil) fList
else {
val nextFList = for (l <- fList; e <- listF.head.asInstanceOf[Future[_]]) yield l :+ e
sequence2(nextFList, listF.tail)
}
}
sequence2(list, in.tail.tail)
}
This code should return Future[HList] which we can then map back to tuple with the tupled function. Ideally we need to check for the fact we have less than 3 elements in the tuple. But lets assume the input is an HList of size 3 or larger for this exercise.
I'm on Shapeless 1.2.4 and can't move yet due to other dependencies.
Thanks in advance!