Skip to content

Instantly share code, notes, and snippets.

@SystemFw
Last active June 7, 2019 05:15
Show Gist options
  • Select an option

  • Save SystemFw/5b36f7ded2b9377e99d475937345fa2f to your computer and use it in GitHub Desktop.

Select an option

Save SystemFw/5b36f7ded2b9377e99d475937345fa2f to your computer and use it in GitHub Desktop.

Revisions

  1. SystemFw revised this gist Oct 25, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Lib.scala
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ object Results {

    implicit def gen[T, L <: HList, O <: HList](
    implicit gen: Generic.Aux[T, L],
    v: Results.Aux[L, O],
    v: Lazy[Results.Aux[L, O]],
    ev: Sequencer.Aux[O, Kleisli[IO, DB, ?], L]): Results.Aux[T, Kleisli[IO, DB, T]] =
    instance(v.value.sequence.map(gen.from))
    instance(v.value.value.sequence.map(gen.from))
    }
  2. SystemFw revised this gist Oct 17, 2017. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions Lib.scala
    Original file line number Diff line number Diff line change
    @@ -36,10 +36,10 @@ object Results {

    implicit def hnil: Results.Aux[HNil, HNil] = instance(HNil)

    implicit def hcons[H, T <: HList, R <: HList](
    implicit g: Results[H],
    n: Results.Aux[T, R]): Results.Aux[H :: T, g.Out :: R] =
    instance(g.value :: n.value)
    implicit def hcons[H, T <: HList, R <: HList, O](
    implicit g: Lazy[Results.Aux[H, O]],
    n: Results.Aux[T, R]): Results.Aux[H :: T, O :: R] =
    instance(g.value.value :: n.value)

    implicit def gen[T, L <: HList, O <: HList](
    implicit gen: Generic.Aux[T, L],
  3. SystemFw revised this gist Oct 11, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Lib.scala
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,9 @@ import cats.sequence._ //requires kittens
    import cats.effect.IO //requires cats-effect
    // ofc, uses "-Ypartial-unification" and kind-projector

    case class GetResult() // replace with slick's
    case class Result() // replace with the JDBC equivalent

    case class DB(val g: GetResult) {
    case class DB(val r: Result) {
    def nextInt: IO[Int] = ??? //IO(g.nextInt)
    def nextDouble: IO[Double] = ???
    def nextFloat: IO[Float] = ???
  4. SystemFw revised this gist Oct 10, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Lib.scala
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@ import shapeless._ // requires.shapeless
    import cats._, implicits._, data.Kleisli // requires.cats
    import cats.sequence._ //requires kittens
    import cats.effect.IO //requires cats-effect
    // ofc, uses "-Ypartial-unification" and kind-projector

    case class GetResult() // replace with slick's

  5. SystemFw revised this gist Oct 10, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Lib.scala
    Original file line number Diff line number Diff line change
    @@ -24,10 +24,10 @@ object Results {
    def value = v
    }

    implicit def ints: Results.Aux[Int, Kleisli[IO, DB,Int]] =
    implicit def ints: Results.Aux[Int, Kleisli[IO, DB, Int]] =
    instance(Kleisli(r => r.nextInt))

    implicit def doubles: Results.Aux[Double, Kleisli[IO, DB,Double]] =
    implicit def doubles: Results.Aux[Double, Kleisli[IO, DB, Double]] =
    instance(Kleisli(r => r.nextDouble))

    implicit def floats: Results.Aux[Float, Kleisli[IO, DB, Float]] =
  6. SystemFw created this gist Oct 10, 2017.
    48 changes: 48 additions & 0 deletions Lib.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    import shapeless._ // requires.shapeless
    import cats._, implicits._, data.Kleisli // requires.cats
    import cats.sequence._ //requires kittens
    import cats.effect.IO //requires cats-effect

    case class GetResult() // replace with slick's

    case class DB(val g: GetResult) {
    def nextInt: IO[Int] = ??? //IO(g.nextInt)
    def nextDouble: IO[Double] = ???
    def nextFloat: IO[Float] = ???
    }

    trait Results[I] {
    type Out
    def value: Out
    }
    object Results {
    def of[T](implicit ev: Results[T]): ev.Out = ev.value

    type Aux[I, O] = Results[I] { type Out = O }
    def instance[I, O](v: O): Results.Aux[I, O] = new Results[I] {
    type Out = O
    def value = v
    }

    implicit def ints: Results.Aux[Int, Kleisli[IO, DB,Int]] =
    instance(Kleisli(r => r.nextInt))

    implicit def doubles: Results.Aux[Double, Kleisli[IO, DB,Double]] =
    instance(Kleisli(r => r.nextDouble))

    implicit def floats: Results.Aux[Float, Kleisli[IO, DB, Float]] =
    instance(Kleisli(r => r.nextFloat))

    implicit def hnil: Results.Aux[HNil, HNil] = instance(HNil)

    implicit def hcons[H, T <: HList, R <: HList](
    implicit g: Results[H],
    n: Results.Aux[T, R]): Results.Aux[H :: T, g.Out :: R] =
    instance(g.value :: n.value)

    implicit def gen[T, L <: HList, O <: HList](
    implicit gen: Generic.Aux[T, L],
    v: Results.Aux[L, O],
    ev: Sequencer.Aux[O, Kleisli[IO, DB, ?], L]): Results.Aux[T, Kleisli[IO, DB, T]] =
    instance(v.value.sequence.map(gen.from))
    }
    7 changes: 7 additions & 0 deletions Test.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    object Test {
    case class Foo(a: Int, b: Double)
    def db: DB = ???
    val d: IO[Foo] = Results.of[Foo].run(db)
    }