Skip to content

Instantly share code, notes, and snippets.

@macalinao
Created November 25, 2017 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macalinao/f40e7cb4a911c4a29ad8c051f1691cd1 to your computer and use it in GitHub Desktop.
Save macalinao/f40e7cb4a911c4a29ad8c051f1691cd1 to your computer and use it in GitHub Desktop.
[info] Compiling 1 Scala source to /Users/ian/proj/macalinao/scala-playground/target/scala-2.12/classes...
[error] /Users/ian/proj/macalinao/scala-playground/src/main/scala/pw/ian/playground/shapeless/BrokenSplitter.scala:87: ambiguous implicit values:
[error] both value scgen in object StatsAndPopGenerator of type => shapeless.Generic[pw.ian.playgroiund.shapeless.StatsAndPopGenerator.ScM]{type Repr = scala.collection.immutable.Map[Int,pw.ian.playgroiund.shapeless.StatsAndPopGenerator.Moments] :: shapeless.HNil}
[error] and value cgen in object StatsAndPopGenerator of type => shapeless.Generic[pw.ian.playgroiund.shapeless.StatsAndPopGenerator.ScS]{type Repr = Option[pw.ian.playgroiund.shapeless.StatsAndPopGenerator.Stat] :: shapeless.HNil}
[error] match expected type shapeless.Generic.Aux[MG,M]
[error] ] = genericGenerator
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 0 s, completed Nov 25, 2017 4:29:41 PM
package pw.ian.playgroiund.shapeless
import cats.implicits._
import shapeless._
trait StatsAndPopGenerator[M, S, P] {
def generate(in: M): (S, P)
}
object StatsAndPopGenerator {
case class Moments()
case class Stat()
case class Population()
def computeStatsAndPopulation(in: Map[Int, Moments]): (Map[Int, Stat], Population) = {
???
}
def apply[M, S, P](fn: M => (S, P)): StatsAndPopGenerator[M, S, P] = {
new StatsAndPopGenerator[M, S, P] {
def generate(in: M): (S, P) = {
fn(in)
}
}
}
implicit val hnilGenerator: StatsAndPopGenerator[HNil, HNil, HNil] =
apply(_ => (HNil, HNil))
implicit def hlistGenerator[
M <: HList, S <: HList, P <: HList,
](
implicit tailSPG: StatsAndPopGenerator[M, Map[Int, S], P],
): StatsAndPopGenerator[
Map[Int, Moments] :: M,
Map[Int, Option[Stat] :: S],
Option[Population] :: P,
] = apply { case (moments :: rest) =>
val (statsHead, popHead) = computeStatsAndPopulation(moments)
val (statsTail, popTail) = tailSPG.generate(rest)
(
statsTail.transform { case (key, value) =>
statsHead.get(key) :: value
},
popHead.some :: popTail,
)
}
def genericGenerator[
MG, SG, PG,
M <: HList, S <: HList, P <: HList,
](
implicit genm: Generic.Aux[MG, M],
gens: Generic.Aux[SG, S],
genp: Generic.Aux[PG, P],
generator: StatsAndPopGenerator[M, S, P],
): StatsAndPopGenerator[
MG, SG, PG,
] = apply { m =>
val (statsL, popL) = generator.generate(genm.to(m))
(
gens.from(statsL),
genp.from(popL),
)
}
case class ScM(
a: Map[Int, Moments],
)
case class ScS(
a: Option[Stat],
)
case class ScP(
a: Option[Population],
)
implicit val scgen = Generic[ScM]
implicit val cgen = Generic[ScS]
implicit val pgen = Generic[ScP]
val acsdGenerator: StatsAndPopGenerator[
ScM, ScS, ScP,
] = genericGenerator
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment