shapeless

Shapeless lenses in idea

╄→гoц情女王★ 提交于 2020-07-06 13:27:09
问题 I try to use shapless lenses of version 2.0.0 for scala 2.10.3 I have the code similar to this one: import shapeless._ case class A(map: Map[String, String]) case class B(a: A) val mapLens = lens[B] >> 'a >> 'map the infered type in idea of mapLens is AnyRef with Lens[B, Nothing] {val gen: LabelledGeneric.Aux[Nothing, ::[record.FieldType[Witness.Lt[Symbol]#T, Nothing], Nothing]]} so if I want to change value of B instance mapLens.set(b)(b.a.map + ("foo" -> "bar")) I get a type mismatch error.

How to generically extract field names with Shapeless?

只谈情不闲聊 提交于 2020-07-04 13:44:08
问题 Given a case class A I can extract its field names with Shapeless using the following snippet: val fieldNames: List[String] = { import shapeless._ import shapeless.ops.record.Keys val gen = LabelledGeneric[A] val keys = Keys[gen.Repr].apply keys.toList.map(_.name) } This works all nice, but how can I implement this in a more generic manner, so that I can conveniently use this technique for arbitrary classes, like val fields: List[String] = fieldNames[AnyCaseClass] Is there a library that

How to Convert Seq to Json using Circe inside a function - keep getting “implicit value not found” error

前提是你 提交于 2020-06-26 12:11:05
问题 I am learning Circe and Scala for a project at work. To explain my issue, start with the following example: import io.circe.syntax._ object TestDrive extends App { val labels = Seq("Banana", "Banano", "Grapefruit") println(labels.asJson) } Ok so this outputs: ["Banana","Banano","Grapefruit"] This is good. Now I want to make my code a bit more general. I want to write a function that takes in a Sequence, whose elements can be of type AnyVal. Here is my attempt: import io.circe.syntax._ import

How to Convert Seq to Json using Circe inside a function - keep getting “implicit value not found” error

二次信任 提交于 2020-06-26 12:10:30
问题 I am learning Circe and Scala for a project at work. To explain my issue, start with the following example: import io.circe.syntax._ object TestDrive extends App { val labels = Seq("Banana", "Banano", "Grapefruit") println(labels.asJson) } Ok so this outputs: ["Banana","Banano","Grapefruit"] This is good. Now I want to make my code a bit more general. I want to write a function that takes in a Sequence, whose elements can be of type AnyVal. Here is my attempt: import io.circe.syntax._ import

When using the singleton type feature of Scala shapeless, how to force the compiler to use narrow/singleton type as an implicit parameter?

只愿长相守 提交于 2020-05-14 07:22:05
问题 This is a follow-up of one of my previous questions: In scala shapeless, is it possible to use literal type as a generic type parameter? I'm trying to write scala code for vector multiplication, while using shapeless to make the compiler aware of the dimension of each vector: trait IntTypeMagnet[W <: Witness.Lt[Int]] extends Serializable { def witness: W } object IntTypeMagnet { case class Impl[W <: Witness.Lt[Int]](witness: W) extends IntTypeMagnet[W] {} implicit def fromInt[W <: Int](v: W):

When using the singleton type feature of Scala shapeless, how to force the compiler to use narrow/singleton type as an implicit parameter?

余生颓废 提交于 2020-05-14 07:21:44
问题 This is a follow-up of one of my previous questions: In scala shapeless, is it possible to use literal type as a generic type parameter? I'm trying to write scala code for vector multiplication, while using shapeless to make the compiler aware of the dimension of each vector: trait IntTypeMagnet[W <: Witness.Lt[Int]] extends Serializable { def witness: W } object IntTypeMagnet { case class Impl[W <: Witness.Lt[Int]](witness: W) extends IntTypeMagnet[W] {} implicit def fromInt[W <: Int](v: W):

Is there a way to define multiple implicit evidences via a single HList?

Deadly 提交于 2020-05-13 19:02:26
问题 I have a piece of code, conceptually similar to the following one: //library code trait Support[K, V] def partialHandler[K, V](key: K, value: V)(implicit ev: Support[K, V]) = ??? //user code implicit val intIntSupport = new Support[Int, Int] {} implicit val intStringSupport = new Support[Int, String] {} ... partialHandler(1, "foo) partialHandler(1, 1) I wonder if there is a way to let users of this library define supported (K, V) types more elegantly, e.g.: val supportedTypes = new Support

In scala shapeless, is it possible to use literal type as a generic type parameter?

生来就可爱ヽ(ⅴ<●) 提交于 2020-04-13 06:45:09
问题 Assuming that I'm writing a program for vector multiplication. Following the requirement in this article: https://etrain.github.io/2015/05/28/type-safe-linear-algebra-in-scala The multiplication should only compile successfully if the dimension of both vectors are equal. For this I define a generic type Axis that uses a shapeless literal type (the number of dimension) as the type parameter: import shapeless.Witness trait Axis extends Serializable case object UnknownAxis extends Axis trait

Type based collection partitioning in Scala

不羁的心 提交于 2020-03-18 03:25:28
问题 Given the following data model: sealed trait Fruit case class Apple(id: Int, sweetness: Int) extends Fruit case class Pear(id: Int, color: String) extends Fruit I've been looking to implement a segregate basket function which for the given basket of fruits will return separate baskets of apples and pears: def segregateBasket(fruitBasket: Set[Fruit]): (Set[Apple], Set[Pear]) I've attempted a couple of approaches, but none of them seems to be fitting the bill perfectly. Below are my attempts:

Type based collection partitioning in Scala

可紊 提交于 2020-03-18 03:22:02
问题 Given the following data model: sealed trait Fruit case class Apple(id: Int, sweetness: Int) extends Fruit case class Pear(id: Int, color: String) extends Fruit I've been looking to implement a segregate basket function which for the given basket of fruits will return separate baskets of apples and pears: def segregateBasket(fruitBasket: Set[Fruit]): (Set[Apple], Set[Pear]) I've attempted a couple of approaches, but none of them seems to be fitting the bill perfectly. Below are my attempts: