hlist

mapping over HList inside a function

我的梦境 提交于 2020-12-29 06:35:31
问题 The following code seems obvious enough to compile and run case class Pair(a: String, b: Int) val pairGen = Generic[Pair] object size extends Poly1 { implicit def caseInt = at[Int](x => 1) implicit def caseString = at[String](_.length) } def funrun(p: Pair) = { val hp: HList = pairGen.to(p) hp.map(size) } but the compiler says "could not find implicit value for parameter mapper". In my use case, I want to map over an HList to get and HList of String(s) and then convert the HList of String(s)

mapping over HList inside a function

瘦欲@ 提交于 2020-12-29 06:35:20
问题 The following code seems obvious enough to compile and run case class Pair(a: String, b: Int) val pairGen = Generic[Pair] object size extends Poly1 { implicit def caseInt = at[Int](x => 1) implicit def caseString = at[String](_.length) } def funrun(p: Pair) = { val hp: HList = pairGen.to(p) hp.map(size) } but the compiler says "could not find implicit value for parameter mapper". In my use case, I want to map over an HList to get and HList of String(s) and then convert the HList of String(s)

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

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 ::

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

shapeless HList to TupleN where the tuple shape need not exactly match the HList shape

限于喜欢 提交于 2020-01-01 04:32:07
问题 I would like to create the equivalent of: def toTupleN[A1, ..., AN, L <: HList](l: L): TupleN[A1, ..., AN] Code using toTupleN should compile only when there is exactly one N combination of values in l that the tuple could be created from. Anything else should generate a compile-time error. Available implicit conversions should be taken into account. Note that there are no restrictions on the size of l or the ordering of values in it. Example: val l = 23 :: (1, "wibble") :: (2, "wobble") ::

Function that retrieves element from HList (while preserving its type)

别等时光非礼了梦想. 提交于 2019-12-25 03:27:23
问题 I have this type which will be generated via shapeless: type hlistt = STUDENT.type :: AUTO_LOANS.type :: HNil Basically I have a bunch of case objects extending a trait so I managed to create a method which gives me instances of all case objects as an HList Then using import shapeless.ops.hlist.Last and init I wrote a method to retrieve one of the nodes in the HList if the value is equal to the string "student": def getLast(hl:hlistt) = { val last0=Last[hlistt] val la=last0(hl) if (la.value =