Sequence `HList`s
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 scalaz._ | |
import scalaz.syntax.apply._ | |
sealed trait UnapplyFirst[F[_], L <: HList] { | |
def TC: Applicative[F] | |
} | |
object UnapplyFirst { | |
implicit def unapplyFirst[H, T <: HList](implicit ev: Unapply[Applicative, H]): UnapplyFirst[ev.M, H :: T] = new UnapplyFirst[ev.M, H :: T] { | |
def TC = ev.TC | |
} | |
} | |
sealed trait Sequencer[F[_], I <: HList, O <: HList] { | |
def apply(in: I)(implicit F: Applicative[F]): F[O] | |
} | |
object Sequencer { | |
implicit def nilSequencer[F[_]]: Sequencer[F, HNil, HNil] = new Sequencer[F, HNil, HNil] { | |
def apply(in: HNil)(implicit F: Applicative[F]) = | |
F.pure(HNil: HNil) | |
} | |
implicit def consSequencer[F[_], H, T <: HList, O <: HList](implicit seq: Sequencer[F, T, O]): Sequencer[F, F[H] :: T, H :: O] = new Sequencer[F, F[H] :: T, H :: O] { | |
def apply(in: F[H] :: T)(implicit F: Applicative[F]) = in match { | |
case head :: tail => | |
(head |@| seq(tail)) { _ :: _ } | |
} | |
} | |
} | |
def sequence[F[_], I <: HList, O <: HList](in: I)(implicit unapplied: UnapplyFirst[F, I], sequencer: Sequencer[F, I, O]): F[O] = | |
sequencer(in)(unapplied.TC) |
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 scalaz._ | |
import scalaz.std.option._ | |
import shapeless._ | |
import shapeless.contrib.scalaz._ | |
// works nicely | |
val in = Option(1) :: HNil | |
sequence(in) |
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 scalaz._ | |
import scalaz.std.string._ | |
import scalaz.syntax.validation._ | |
import shapeless._ | |
import shapeless.contrib.scalaz._ | |
// doesn't work | |
val in = 3.success[String] :: HNil | |
sequence(in) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment