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
scala> import scalaz._, Scalaz._ | |
import scalaz._ | |
import Scalaz._ | |
scala> import scala.concurrent.Future | |
import scala.concurrent.Future | |
scala> import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.ExecutionContext.Implicits.global |
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
println(route(Request("http://www.scalac.io"))) | |
// Response(protocol was http and host was www.scalac.io) |
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
def &[Y <: HList](that: Directive[Y])(implicit prepend: Prepend[X, Y]): Directive[prepend.Out] = ... |
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
def apply[F](f: F)(implicit fp: FnToProduct.Aux[F, X => Route]) = ... |
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
class ZipSpec extends FlatSpec with Matchers with ScalaFutures { | |
import shapeless._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future._ | |
"zip" should "provide one future from args of futures" in { | |
val result = zip(successful(1), successful(true), successful("string"), successful(1.0)) | |
val (a, b, c, d) = result.futureValue | |
(a, b, c, d) should equal((1, true, "string", 1.0)) | |
} |
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
def zip[P <: Product, In <: HList, Out <: HList] | |
(p: P) | |
(implicit gen: Generic.Aux[P, In], ev: IsHListOfFutures[In, Out], tupler: Tupler[Out], ec: ExecutionContext) = { | |
hsequence(gen.to(p)).map(_.tupled) | |
} |
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
class HsequenceSpec extends FlatSpec with Matchers with ScalaFutures { | |
import shapeless._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future._ | |
"hsequence" should "calculate hzip of hlist" in { | |
val fs = successful(1) :: successful(true) :: successful("string") :: successful(1.0) :: HNil | |
val a :: b :: c :: d :: HNil = hsequence(fs).futureValue | |
a :: b :: c :: d :: HNil should equal(1 :: true :: "string" :: 1.0 :: HNil) | |
} |
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
def hsequence[In <: HList, Out <: HList](l : In)(implicit ev: IsHListOfFutures[In, Out], ec: ExecutionContext) = ev.hsequence(l) |
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
object IsHListOfFutures { | |
def apply[In <: HList, Out <: HList](implicit isHzippable: IsHListOfFutures[In, Out]): IsHListOfFutures[In, Out] = isHzippable | |
implicit object HNilIsListOfFutures extends IsHListOfFutures[HNil, HNil] { | |
override def hsequence(l : HNil)(implicit ec: ExecutionContext): Future[HNil] = Future.successful(HNil) | |
} | |
implicit def hconsIsHListOfFutures[H, In <: HList, Out <: HList] | |
(implicit ev: IsHListOfFutures[In, Out]): IsHListOfFutures[Future[H] :: In, H :: Out] = new IsHListOfFutures[Future[H] :: In, H :: Out] { |
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
implicit def hconsIsHListOfFutures[H, In <: HList, Out <: HList] | |
(implicit ev: IsHListOfFutures[In, Out]): IsHListOfFutures[Future[H] :: In, H :: Out] = new IsHListOfFutures[Future[H] :: In, H :: Out] { | |
override def hsequence(l : Future[H] :: In)(implicit ec: ExecutionContext): Future[H :: Out] = { | |
val head = l.head | |
val tail = l.tail | |
head.flatMap(h => ev.hsequence(tail).map(h :: _)) | |
} | |
} |