Skip to content

Instantly share code, notes, and snippets.

View tomaszperek's full-sized avatar

Tomasz Perek tomaszperek

View GitHub Profile
@tomaszperek
tomaszperek / applicativebuilder.scala
Created October 19, 2015 21:32
Applicative Builder at work
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
@tomaszperek
tomaszperek / applyRoute.scala
Created October 12, 2015 20:05
apply route to request
println(route(Request("http://www.scalac.io")))
// Response(protocol was http and host was www.scalac.io)
@tomaszperek
tomaszperek / amp.scala
Created October 12, 2015 19:48
& for combinig Direcives
def &[Y <: HList](that: Directive[Y])(implicit prepend: Prepend[X, Y]): Directive[prepend.Out] = ...
@tomaszperek
tomaszperek / apply.scala
Created October 12, 2015 19:47
Apply using FnToPoduct evidence
def apply[F](f: F)(implicit fp: FnToProduct.Aux[F, X => Route]) = ...
@tomaszperek
tomaszperek / ZipSpec.scala
Created October 10, 2015 11:35
ScalaTest specification illustrating how zip function works
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))
}
@tomaszperek
tomaszperek / zip.scala
Created October 10, 2015 11:34
Zip implementation
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)
}
@tomaszperek
tomaszperek / HsequenceSpec.scala
Created October 10, 2015 11:34
ScalaTest spec demonstrating hsequence
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)
}
@tomaszperek
tomaszperek / hsequence.scala
Created October 10, 2015 11:33
Implementation of hsequence function
def hsequence[In <: HList, Out <: HList](l : In)(implicit ev: IsHListOfFutures[In, Out], ec: ExecutionContext) = ev.hsequence(l)
@tomaszperek
tomaszperek / IsHListOfFuturesObject.scala
Created October 10, 2015 11:32
IsHListOfFutures object
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] {
@tomaszperek
tomaszperek / hconsIsHListOfFutures.scala
Created October 10, 2015 11:32
Implicit def for more comlex HLists of futures
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 :: _))
}
}