shapeless

Type based collection partitioning in Scala

老子叫甜甜 提交于 2020-03-18 03:21:13
问题 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:

Mapped types in Scala

一曲冷凌霜 提交于 2020-01-22 20:04:51
问题 Is there a way to derive a type from an existing one in Scala? For example, for case class Person(name: String, age: Int) I'd like to get a Product / Tuple of (Option[String], Option[Int]) , i.e. a type mapped from an existing one. There's a feature in Typescript (mapped types) that allows this relatively easily, which is how I started thinking down this path. But I'm not sure how something like this would be done in Scala. I feel like the solution involves using shapeless in some way but I'm

Understanding the Aux pattern in Scala Type System

ぐ巨炮叔叔 提交于 2020-01-20 04:34:25
问题 This question may be asked and answered before, but I would like to understand this with an example and I could not reason out where the Aux pattern might be helpful! So here is the trait: trait Foo[A] { type B def value: B } Why do I have a type that is kind of bound to the return type of the value function? What do I achieve doing this? In particular, where would I use such patterns? 回答1: Imagine a typeclass for getting the last element of any tuple. trait Last[A] { type B def last(a: A): B

Creating an HList of all pairs from two HLists

依然范特西╮ 提交于 2020-01-19 07:28:28
问题 I'm using shapeless in Scala, and I'd like to write a function allPairs that will take two HLists and return an HList of all pairs of elements. For example: import shapeless._ val list1 = 1 :: "one" :: HNil val list2 = 2 :: "two" :: HNil // Has value (1, 2) :: (1, "two") :: ("one", 2) :: ("one", "two") :: HNil val list3 = allPairs(list1, list2) Any idea how to do this? Also, I'd like to emphasize I'm looking for a function , not an inlined block of code. 回答1: You can't use a for

Creating an HList of all pairs from two HLists

五迷三道 提交于 2020-01-19 07:27:25
问题 I'm using shapeless in Scala, and I'd like to write a function allPairs that will take two HLists and return an HList of all pairs of elements. For example: import shapeless._ val list1 = 1 :: "one" :: HNil val list2 = 2 :: "two" :: HNil // Has value (1, 2) :: (1, "two") :: ("one", 2) :: ("one", "two") :: HNil val list3 = allPairs(list1, list2) Any idea how to do this? Also, I'd like to emphasize I'm looking for a function , not an inlined block of code. 回答1: You can't use a for

Is it possible to 'transform' a HMap into another HMap

流过昼夜 提交于 2020-01-15 07:10:09
问题 If I have a Shapeless HMap[MappingA] (with implicits properly defined for the type MappingA[K, V] ), can I type-safely transform/map it to a HMap[MappingB] . class MappingA[K, V] implicit val intToString = new MappingA[Int, String] implicit val stringToInt = new MappingA[String, Int] class MappingB[K, V] implicit val longToString = new MappingA[Long, String] implicit val stringToLong = new MappingA[String, Long] val hm1 = HMap[MappingA](1 -> "one", "two" -> 2) // How to... val hm2: HMap

Generically rewriting Scala case classes

ぐ巨炮叔叔 提交于 2020-01-14 13:36:30
问题 Is it possible to generically replace arguments in a case class? More specifically, say I wanted a substitute function that received a "find" case class and a "replace" case class (like the left and right sides of a grammar rule) as well as a target case class, and the function would return a new case class with arguments of the find case class replaced with the replace case class? The function could also simply take a case class (Product?) and a function to be applied to all arguments

Diverging implicit expansion for type class

梦想与她 提交于 2020-01-13 13:13:53
问题 I'm trying to extend the Parser type class from this blog post to support nested case classes parsing. Since I'm new to shapeless, I've decided to start with the non-general case, that would only support a case class with 2 fields that are themselves case classes. To illustrate this better, here's the case classes definition first: case class Person(name: String, age: Int) case class Family(husband: Person, wife: Person) In addition to the code from that blog post, I've created one more

Diverging implicit expansion for type class

廉价感情. 提交于 2020-01-13 13:13:12
问题 I'm trying to extend the Parser type class from this blog post to support nested case classes parsing. Since I'm new to shapeless, I've decided to start with the non-general case, that would only support a case class with 2 fields that are themselves case classes. To illustrate this better, here's the case classes definition first: case class Person(name: String, age: Int) case class Family(husband: Person, wife: Person) In addition to the code from that blog post, I've created one more

Shapeless: map from coproduct to different coproduct

北战南征 提交于 2020-01-13 07:59:09
问题 In the following, I'm trying to make a polymorphic function to convert a RawFeatureValue into a RefinedFeatureValue . import shapeless._ object test { type RawFeatureValue = Int :+: Double :+: String :+: CNil type RefinedFeatureValue = Int :+: Double :+: CNil private object convert extends Poly1 { implicit def caseInt = at[Int](i => i) implicit def caseDouble = at[Double](d => d) implicit def caseString = at[String](s => s.hashCode) } val a = Coproduct[RawFeatureValue](12) val b: