Last active
May 20, 2017 07:04
-
-
Save macalinao/f7713886180d1843d04c9932601b661d to your computer and use it in GitHub Desktop.
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
[info] Compiling 9 Scala sources 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/KittensMonoidDerivation.scala:33: value dragon is not a member of type parameter Wrapper | |
[error] val groups = al.groupBy(_.dragon) |+| bl.groupBy(_.dragon) | |
[error] ^ | |
[error] /Users/ian/proj/macalinao/scala-playground/src/main/scala/pw/ian/playground/shapeless/KittensMonoidDerivation.scala:33: type mismatch; | |
[error] found : cats.kernel.Monoid[Map[Nothing,List[Wrapper]]] | |
[error] required: cats.Semigroup[scala.collection.immutable.Map[K,List[Wrapper]]] | |
[error] (which expands to) cats.kernel.Semigroup[scala.collection.immutable.Map[K,List[Wrapper]]] | |
[error] val groups = al.groupBy(_.dragon) |+| bl.groupBy(_.dragon) | |
[error] ^ | |
[error] /Users/ian/proj/macalinao/scala-playground/src/main/scala/pw/ian/playground/shapeless/KittensMonoidDerivation.scala:33: value |+| is not a member of scala.collection.immutable.Map[K,List[Wrapper]] | |
[error] val groups = al.groupBy(_.dragon) |+| bl.groupBy(_.dragon) | |
[error] ^ | |
[error] /Users/ian/proj/macalinao/scala-playground/src/main/scala/pw/ian/playground/shapeless/KittensMonoidDerivation.scala:33: value dragon is not a member of type parameter Wrapper | |
[error] val groups = al.groupBy(_.dragon) |+| bl.groupBy(_.dragon) | |
[error] ^ | |
[error] four errors found | |
[error] (compile:compileIncremental) Compilation failed | |
[error] Total time: 2 s, completed May 20, 2017 2:02:43 AM | |
> |
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
package pw.ian.playground.shapeless | |
import cats._ | |
import cats.implicits._ | |
import cats.data.NonEmptyList | |
import cats.derived._, monoid._, legacy._ | |
// runMain "pw.ian.playground.shapeless.KittensMonoidDerivation" | |
object KittensMonoidDerivation { | |
trait Dragon | |
case object Infernal extends Dragon | |
case class Moments(ct: Int, sum: Int, sumSq: Int) | |
case class Wrapper(dragon: Dragon, data: Option[Moments]) | |
case class All(wrappers: Seq[Wrapper]) | |
implicit val momentsMonoid: Monoid[Moments] = Monoid[Moments] | |
implicit val wrapperSemi = new Semigroup[Wrapper] { | |
def combine(a: Wrapper, b: Wrapper): Wrapper = { | |
a.copy(data = a.data |+| b.data) | |
} | |
} | |
implicit def sortedSeqMonoid[Wrapper]: Monoid[Seq[Wrapper]] = new Monoid[Seq[Wrapper]] { | |
def combine(a: Seq[Wrapper], b: Seq[Wrapper]): Seq[Wrapper] = { | |
val al = a.toList | |
val bl = b.toList | |
val groups = al.groupBy(_.dragon) |+| bl.groupBy(_.dragon) | |
groups | |
.mapValues(x => NonEmptyList.fromList(x).map(_.reduce)) | |
.values.flatten.toList | |
} | |
def empty = List() | |
} | |
def main(args: Array[String]): Unit = { | |
val a = All( | |
wrappers = Seq( | |
Wrapper( | |
dragon = Infernal, | |
data = Moments(1, 2, 3).some, | |
), | |
), | |
) | |
val b = All( | |
wrappers = Seq( | |
Wrapper( | |
dragon = Infernal, | |
data = Moments(4, 5, 6).some, | |
), | |
), | |
) | |
println(a |+| b) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment