Last active
August 7, 2017 10:25
-
-
Save timcharper/df19933d06601f3ac834 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 shapeless._ | |
import scala.concurrent._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
object FutureReducer extends Poly2 { | |
implicit def f[A, B <: HList] = at[Future[A], Future[B]] { (f, resultFuture) => | |
resultFuture flatMap { r => | |
f map { v => | |
v :: r | |
} | |
} | |
} | |
} | |
def hsequence[T <: HList](hlist: T)(implicit | |
executor: ExecutionContext, | |
folder: RightFolder[T, Future[HNil], FutureReducer.type]) = { | |
hlist.foldRight(Future.successful[HNil](HNil))(FutureReducer)(folder) | |
} | |
val hFutures = Future.successful(5) :: Future.successful("string") :: HNil | |
hsequence(hFutures) // Works! | |
// => | |
// scala.concurrent.Future[shapeless.::[Int,shapeless.::[String,shapeless.HNil]]] = scala.concurrent.impl.Promise$DefaultPromise@1cc988d7 | |
def tsequence[T <: Product, L <: HList, OL <: HList](tuple: T)(implicit | |
executor: ExecutionContext, | |
hlister: HListerAux[T, L], | |
folder: RightFolderAux[L, Future[HNil], FutureReducer.type, Future[OL]]) = { | |
hlister(tuple).foldRight(Future.successful[HNil](HNil))(FutureReducer) | |
} | |
val tFutures = (Future.successful(5), Future.successful("string")) | |
// This fails: | |
tsequence(tFutures) | |
// error: could not find implicit value for parameter folder: shapeless.RightFolderAux[L,scala.concurrent.Future[shapeless.HNil.type],FutureReducer.type,scala.concurrent.Future[OL]] | |
// tsequence(tFutures) | |
// If I provide the type parameters explicitly, then it works | |
tsequence[ | |
(Future[Int], Future[String]), | |
Future[Int] :: Future[String] :: HNil, | |
Int :: String :: HNil](tFutures) | |
// => | |
// scala.concurrent.Future[shapeless.::[Int,shapeless.::[String,shapeless.HNil]]] = scala.concurrent.impl.Promise$DefaultPromise@53661dfd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment