shapeless

Mapping over HList with subclasses of a generic trait

岁酱吖の 提交于 2019-12-24 10:23:38
问题 I'm trying to map a poly1 function over a shapeless HList. Its elements are subclasses of a parameterised trait. However, I get the error "couldn't find implicit value for the Mapper". Here's a basic example: import shapeless._ trait Drink[+A]{ def v: A } case class Water(v: Int) extends Drink[Int] case class Juice(v: BigDecimal) extends Drink[BigDecimal] case class Squash(v: BigDecimal) extends Drink[BigDecimal] object pour extends Poly1{ implicit def caseInt: Case.Aux[Drink[Int], Int] = at

Instantiate types from recursive type grammar

我怕爱的太早我们不能终老 提交于 2019-12-24 08:46:45
问题 Given this recursive type grammar: case class Fix[F[_]](out: F[Fix[F]]) type FieldValue = Seq[String] :+: String :+: Int :+: Long :+: CNil type FieldLeaf[F] = FieldValue :+: SubField[F] :+: CNil type SubField[F] = Seq[F] type Field0[F] = (String, FieldLeaf[F]) type Field = Fix[Field0] And instances of Seq[Field] Is it feasible to instantiate concrete classes from the type grammar? I.e: def instantiate[T](fields:Seq[Field]):Option[T] = .... case class Person(id:Long, name:String) val fields

NoSuchMethodError in shapeless seen only in Spark

自闭症网瘾萝莉.ら 提交于 2019-12-24 08:24:51
问题 I am trying to write a Spark connector to pull AVRO messages off a RabbitMQ message queue. When decoding the AVRO messages, there is a NoSuchMethodError error that occurs only when running in Spark. I could not reproduce the Spark code exactly outside of spark, but I believe the two examples are sufficiently similar. I think this is the smallest code that reproduces the same scenario. I've removed all the connection parameters both because the information is private and the connection does

Class with List of Nat's between 0 and 2?

旧街凉风 提交于 2019-12-24 00:26:54
问题 Using Peter Neyens's helpful answer, I tried to create an X class that only consists of Nat 's less than or equal to 2. import shapeless._ import shapeless.ops.nat._ import shapeless.nat._ case class X[A <: Nat](values: List[A])(implicit ev: LTEq[A, _2]) The following works: scala> X( List(_1, _1, _1) ) res6: X[shapeless.nat._1] = X(List(Succ(), Succ(), Succ())) But when I used different Nat 's, i.e. _1 and _2 , I got a compile-time error: scala> X( List(_1, _2) ) <console>:23: error: could

Labelled Generic containing `$eq`

梦想与她 提交于 2019-12-23 17:27:10
问题 Shapeless 2.3.3 LabelledGeneric returns a curious result when run on the following case class: scala> case class Foo(`$eq`: Int) defined class Foo scala> LabelledGeneric[Foo] res0: shapeless.LabelledGeneric[Foo]{type Repr = Int with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("=")],Int] :: shapeless.HNil} = shapeless.LabelledGeneric$$anon$1@1ac7dbd3 Note the label returned is literally = instead of $eq . Is this behaviour a quirk of shapeless or something else? Are there

how to create an HList of Lenses from an HList

落花浮王杯 提交于 2019-12-23 16:28:24
问题 I am writing a generic table viewer which should be able to display any type of Seq[Row] where Row <: HList . The Table class looks like this: class Table[TH<:HList, TR<:HList](val hdrs: TH, val rows: Seq[TR]) When someone clicks on a column of the table viewer I would like to redraw the whole table sorted on the Ordering of that column. For that I need a to be able assign a function to sort the table on a particular colum. Using lenses for that seems one reasonable option. def sort[Elem<:Nat

Shapeless flatmap HList with Option yielding HList

折月煮酒 提交于 2019-12-23 12:34:34
问题 Given the following case class A(value:Int) case class B(value:String) val h:Option[A] :: A :: Option[B] :: Option[A] :: HNil = Some(A(1)) :: A(2) :: Some(B("two")) :: (None:Option[B]) :: HNil How can I get the following ? A(1) :: A(2) :: B("two") :: HNil My attempt below trait a extends Poly1 { implicit def any[T] = at[T](_ :: HNil) } object f extends a { implicit def some[T] = at[Option[T]](t => if (t.isDefined) t.get :: HNil else HNil) } works for map h map f > A(1) :: HNil :: A(2) :: HNil

Decoding Case Class w/ Tagged Type

假装没事ソ 提交于 2019-12-23 09:30:01
问题 Given: Given the following on Ammonite: @ import $ivy.`io.circe::circe-core:0.9.0` @ import $ivy.`io.circe::circe-generic:0.9.0` @ import $ivy.`com.chuusai::shapeless:2.3.3` @ import shapeless.tag import shapeless.tag @ trait Foo defined trait Foo @ import io.circe._, io.circe.generic.semiauto._ import io.circe._, io.circe.generic.semiauto._ @ import shapeless.tag.@@ import shapeless.tag.@@ @ implicit def taggedTypeDecoder[A, B](implicit ev: Decoder[A]): Decoder[A @@ B] = ev.map(tag[B][A](_))

Provide implicits for all subtypes of sealed type

老子叫甜甜 提交于 2019-12-23 03:41:21
问题 In my application I have multiple case classes and objects which are part of sealed trait hierarchy. I use them as messages in Akka. Those classes need to be converted to user friendly form before sending through websocket. Previously I used big pattern match to convert them in single place, but as number of types grows I would like to use implicit conversion: object Types { sealed trait Type case object SubType1 extends Type case object SubType2 extends Type case object SubType3 extends Type

Provide implicits for all subtypes of sealed type

蓝咒 提交于 2019-12-23 03:41:08
问题 In my application I have multiple case classes and objects which are part of sealed trait hierarchy. I use them as messages in Akka. Those classes need to be converted to user friendly form before sending through websocket. Previously I used big pattern match to convert them in single place, but as number of types grows I would like to use implicit conversion: object Types { sealed trait Type case object SubType1 extends Type case object SubType2 extends Type case object SubType3 extends Type