shapeless

Using shapeless scala to merge the fields of two different case classes

南笙酒味 提交于 2020-01-12 18:34:14
问题 I want to merge the fields of two different case classes into a single case class. For example, if I have the following case classes: case class Test(name:String, questions:List[Question], date:DateTime) case class Answer(answers:List[Answers]) I want a concise shapeless way to merge both into: TestWithAnswers(name:String, questions:List[Question], date:DateTime, answers:List[Answer]). Is there a nice shapeless answer out there? 回答1: You can use shapeless Generic to do this. val t: Test = ???

Inferred type of function that zips HLists

一世执手 提交于 2020-01-12 08:44:12
问题 Thanks to https://github.com/milessabin/shapeless/wiki/Feature-overview:-shapeless-2.0.0 I understand how to zip shapeless HLists: Import some stuff from Shapeless 2.0.0-M1: import shapeless._ import shapeless.ops.hlist._ import syntax.std.tuple._ import Zipper._ Create two HLists: scala> val h1 = 5 :: "a" :: HNil h1: shapeless.::[Int,shapeless.::[String,shapeless.HNil]] = 5 :: a :: HNil scala> val h2 = 6 :: "b" :: HNil h2: shapeless.::[Int,shapeless.::[String,shapeless.HNil]] = 6 :: b ::

Map and reduce/fold over HList of scalaz.Validation

丶灬走出姿态 提交于 2020-01-12 05:21:05
问题 I started out with something like this: def nonEmpty[A] = (msg: String) => (a: Option[A]) => a.toSuccess(msg) val postal: Option[String] = request.param("postal") val country: Option[String] = request.param("country") val params = (postal |> nonEmpty[String]("no postal" )).toValidationNel |@| (country |> nonEmpty[String]("no country")).toValidationNel params { (postal, country) => ... } Now I thought it would be nice to reduce the boilerplate for better readability and for not having to

Shapeless: Trying to restrict HList elements by their type

亡梦爱人 提交于 2020-01-11 08:22:32
问题 Question 1 - Basic LUBConstraints My first try playing around with existing LUBConstraints fails for missing evidence (see code block below). Any hint why? Isn't an empty list a valid list of longs? no element violates the constraint. import shapeless.ops.coproduct import shapeless.{::, :+:, Coproduct, HNil, HList} object testLUBConstraints { import shapeless.LUBConstraint._ // !!! see comment on question - this satisfies the implicit below!!! // implicit val hnilLUBForLong = new

Mapping over Shapeless record

这一生的挚爱 提交于 2020-01-10 18:21:05
问题 In a Play application I'm working on, I'm trying to improve our system for processing flags, some of which are meant to be persistent options as a user navigates our app via links. I'd like to use Shapeless to map from a definition of the option to its value, and also to synthesize new query parameter from only the ones marked to be propagated. I also want to be able to take advantage of Shapeless's Record functionality to get strongly typed dereferencing of the parameter values.

Use functional combinators on Scala Tuples?

ぃ、小莉子 提交于 2020-01-09 02:13:05
问题 'map' preserves the number of elements, so using it on a Tuple seems sensible. My attempts so far: scala> (3,4).map(_*2) error: value map is not a member of (Int, Int) (3,4).map(_*2) ^ scala> (3,4).productIterator.map(_*2) error: value * is not a member of Any (3,4).productIterator.map(_*2) ^ scala> (3,4).productIterator.map(_.asInstanceOf[Int]*2) res4: Iterator[Int] = non-empty iterator scala> (3,4).productIterator.map(_.asInstanceOf[Int]*2).toList res5: List[Int] = List(6, 8) It looks quite

Avoid structural type with shapeless alternative

拟墨画扇 提交于 2020-01-07 09:15:08
问题 I want to find out if a particular class member is present in a given case class or not. Following does give me that answer, failing at compile time which is right. (credit to Travis Brown) scala> def getIntId[A, R <: HList](a: A)(implicit | gen: LabelledGeneric.Aux[A, R], | sel: Selector.Aux[R, Witness.`'id`.T, Int] | ): Int = sel(gen.to(a)) case class Foo(id: String) case class Bar(id: Int, name: String) scala> getIntId(Bar(123, "bar")) res3: Int = 123 scala> getIntId(Foo("12345")) <console

Avoid structural type with shapeless alternative

笑着哭i 提交于 2020-01-07 09:13:27
问题 I want to find out if a particular class member is present in a given case class or not. Following does give me that answer, failing at compile time which is right. (credit to Travis Brown) scala> def getIntId[A, R <: HList](a: A)(implicit | gen: LabelledGeneric.Aux[A, R], | sel: Selector.Aux[R, Witness.`'id`.T, Int] | ): Int = sel(gen.to(a)) case class Foo(id: String) case class Bar(id: Int, name: String) scala> getIntId(Bar(123, "bar")) res3: Int = 123 scala> getIntId(Foo("12345")) <console

Avoid structural type with shapeless alternative

元气小坏坏 提交于 2020-01-07 09:12:17
问题 I want to find out if a particular class member is present in a given case class or not. Following does give me that answer, failing at compile time which is right. (credit to Travis Brown) scala> def getIntId[A, R <: HList](a: A)(implicit | gen: LabelledGeneric.Aux[A, R], | sel: Selector.Aux[R, Witness.`'id`.T, Int] | ): Int = sel(gen.to(a)) case class Foo(id: String) case class Bar(id: Int, name: String) scala> getIntId(Bar(123, "bar")) res3: Int = 123 scala> getIntId(Foo("12345")) <console

Scala: Copying a generic case class into another

*爱你&永不变心* 提交于 2020-01-06 17:27:55
问题 I have the following setup, where I want to copy an instance of baseData into that of moreData : sealed trait baseData { def weight: Int def priority: Int } sealed trait moreData { def weight: Int def priority: Int def t: String def id: String } case class data1(override val weight: Int, override val priority: Int) extends baseData case class moreData1 (override val weight:Int, override val priority: Int, override val t: String, override val id: String)extends moreData So copying myData into