shapeless

Different types in Map Scala

偶尔善良 提交于 2019-11-27 12:23:36
问题 I need a Map where I put different types of values (Double, String, Int,...) in it, key can be String. Is there a way to do this, so that I get the correct type with map.apply(k) like val map: Map[String, SomeType] = Map() val d: Double = map.apply("double") val str: String = map.apply("string") I already tried it with a generic type class Container[T](element: T) { def get: T = element } val d: Container[Double] = new Container(4.0) val str: Container[String] = new Container("string") val m:

Converting Map[String,Any] to a case class using Shapeless

◇◆丶佛笑我妖孽 提交于 2019-11-27 11:50:34
The question here asks about mapping a case class to a Map[String,Any]. I was wondering what would be the other way around, converting Map[String,Any] to a case class. Given the following map: val mp = Map("name" -> "Tom", "address" -> Map("street" -> "Jefferson st", "zip" -> 10000)) Convert it to a case class of Person : case class Person(name:String, address:Address) case class Address(street:String, zip:Int) val p = Person("Tom", Address("Jefferson st", 10000)) with something like this: val newP = mp.asCC[Person] assert(newP.get == p) How should I do that with Shapeless. Travis Brown Here's

Why is the Aux technique required for type-level computations?

瘦欲@ 提交于 2019-11-27 11:39:43
I'm pretty sure I'm missing something here, since I'm pretty new to Shapeless and I'm learning, but when is the Aux technique actually required ? I see that it is used to expose a type statement by raising it up into the signature of another "companion" type definition. trait F[A] { type R; def value: R } object F { type Aux[A,RR] = F[A] { type R = RR } } but isn't this nearly equivalent to just putting R in the type signature of F ? trait F[A,R] { def value: R } implicit def fint = new F[Int,Long] { val value = 1L } implicit def ffloat = new F[Float,Double] { val value = 2.0D } def f[T,R](t:T

Any reason why scala does not explicitly support dependent types?

扶醉桌前 提交于 2019-11-27 10:01:39
There are path dependent types and I think it is possible to express almost all the features of such languages as Epigram or Agda in Scala, but I'm wondering why Scala does not support this more explicitly like it does very nicely in other areas (say, DSLs) ? Anything I'm missing like "it is not necessary" ? Miles Sabin Syntactic convenience aside, the combination of singleton types, path-dependent types and implicit values means that Scala has surprisingly good support for dependent typing, as I've tried to demonstrate in shapeless . Scala's intrinsic support for dependent types is via path

Unable to map on HList

旧街凉风 提交于 2019-11-27 08:58:29
I was trying to solve this problem with shapeless. However I am for some reason unable to map on the HList . I'll let the code speak for itself. import shapeless._ import HList._ case class Foo(a: Option[Int], b: Option[Int]) val a = Foo(Some(3), None) val b = Foo(Some(22), Some(1)) implicit val fooIso = HListIso(Foo.apply _, Foo.unapply _) val mapper = new (({ type O2[+A] = (Option[A], Option[A]) })#O2 ~> Option) { def apply[A](x: (Option[A], Option[A])): Option[A] = x._1.orElse(x._2) } fooIso.fromHList(fooIso.toHList(a).zip(fooIso.toHList(b)).map(mapper)) Error message is: <console>:55:

Converting nested case classes to nested Maps using Shapeless

北城余情 提交于 2019-11-27 07:28:40
I am trying to solve this question using Shapeless, in summary it's about converting a nested case class to Map[String,Any], here is the example: case class Person(name:String, address:Address) case class Address(street:String, zip:Int) val p = Person("Tom", Address("Jefferson st", 10000)) It wants to convert p to following: Map("name" -> "Tom", "address" -> Map("street" -> "Jefferson st", "zip" -> 10000)) I am trying to do it using Shapeless LabelledGeneric , here is what I for so far: import shapeless._ import record._, syntax.singleton._ import ops.record._ import shapeless.ops.record._ def

Can't prove that singleton types are singleton types while generating type class instance

冷暖自知 提交于 2019-11-27 07:03:22
Suppose I've got a type class that proves that all the types in a Shapeless coproduct are singleton types: import shapeless._ trait AllSingletons[A, C <: Coproduct] { def values: List[A] } object AllSingletons { implicit def cnilSingletons[A]: AllSingletons[A, CNil] = new AllSingletons[A, CNil] { def values = Nil } implicit def coproductSingletons[A, H <: A, T <: Coproduct](implicit tsc: AllSingletons[A, T], witness: Witness.Aux[H] ): AllSingletons[A, H :+: T] = new AllSingletons[A, H :+: T] { def values = witness.value :: tsc.values } } We can show that it works with a simple ADT: sealed

Are HLists nothing more than a convoluted way of writing tuples?

☆樱花仙子☆ 提交于 2019-11-27 05:48:40
I am really interested in finding out where the differences are, and more generally, to identify canonical use cases where HLists cannot be used (or rather, don't yield any benefits over regular lists). (I am aware that there are 22 (I believe) TupleN in Scala, whereas one only needs a single HList, but that is not the kind of conceptual difference I am interested in.) I've marked a couple of questions in the text below. It might not actually be necessary to answer them, they are more meant to point out things that are unclear to me, and to guide the discussion in certain directions.

Use functional combinators on Scala Tuples?

橙三吉。 提交于 2019-11-27 04:10:34
'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 painful... And I haven't even begun to try to convert it back to a tuple. Am I doing it wrong? Could

Dynamically parametrize Poly1 function in shapeless

岁酱吖の 提交于 2019-11-27 03:31:16
问题 I have this situation (stripped down to the essential parts) class Foo[L <: HList](columns: L) { class toRecord(row: Row) extends Poly1 { implicit def caseColumn[T] = at[Column[T]] { /* map to a record field */ } } def asRecord = { val resultSet: Stream[Row] = // computation to get result set resultSet.map { row => val m = new toRecord(row) // this can't work columns.map(m) } } } This doesn't work since map wants a stable identifier and m is not. So I would need as many Poly1 singleton