shapeless

Add or modify values in a shapeless HMap

徘徊边缘 提交于 2019-12-11 01:56:05
问题 Does anyone know how I might add or modify values in a shapeless HMap? The only functions I see on the HMap definition are: get + (which looks like it's creating a new map and adding the (k,v) tuple) - (same as above) My suspicion is that I will need to use lens? 回答1: shapeless.HMap is immutable. It's a wrapper (with type-level enhancements) of scala.collection.immutable.Map . + adds or modifies a pair (returning new HMap). Lenses also create a copy. Immutability/persistence is typical for FP

Powerset of an HList of Options

落花浮王杯 提交于 2019-12-11 01:39:46
问题 I am playing around with Shapeless and I am trying to compute the (kind-of) powerset of an HList of Option s. Basically, I want to interpret the HList as a set, in which case the Option value of an element tells me it belongs to the set or not. For example, given Some(5) :: Some("a") :: HNil , I want to obtain the following HList s: Some(5) :: Some("a") :: HNil Some(5) :: None :: HNil None :: Some("a") :: HNil None :: None :: HNil I tried the following: object test2 extends Poly2{ implicit

Scala HList manipulation : is a flexible structure possible?

大憨熊 提交于 2019-12-11 01:05:15
问题 I wrote a min example of something I do not manage to do. Am I in the wrong way? object minNotWoringkExample { import shapeless._ def typed[T](t: => T) {} class Bar { type L <: HList } class Foo { type UPDATE[L <: HList] <: HList } class FooI extends Foo { type UPDATE[L <: HList] = FilterNot[L, FooI]#Out } def update[O <: Foo, B <: Bar] = new Bar { type L = O#UPDATE[B#L] } val myBar = new Bar { type L = FooI :: Foo :: HNil } val myUpdatedBar = update[FooI, Bar {type L = FooI :: Foo :: HNil}]

Using shapeless to extract data from case classes, modify it, and recreate the case class

守給你的承諾、 提交于 2019-12-10 18:22:02
问题 I have a program with some case classes. One of them might be a Person, which relies on some other classes, including one called StemmedString which will be important here: case class Person(name: String, skills: List[Skill], hobbbies: List[StemmedString] case class Skill(score: Int, title: StemmedString) case class StemmedString(original: String, stemmed: String) Now I want to be able to translate this person into another language. To do that, I want to go from Person -> List[String]. That's

Scala, gremlin-scala, HLists, Poly2, RightFold and a missing implicit Prepend

夙愿已清 提交于 2019-12-10 18:16:20
问题 So, I am trying to encapsulate a series of operations from gremlin-scala into an HList so I can do a RightFold over them (this would allow me to construct a gremlin query as data: specifically an HList of Operations ). Here is what I mean: usually you can make a call to gremlin-scala like so: import gremlin.scala._ import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory def graph = TinkerFactory.createModern.asScala graph.V.hasLabel("person").out("created").as("creations")

Scala case class arguments instantiation from array

ⅰ亾dé卋堺 提交于 2019-12-10 13:46:15
问题 Consider a case class with a possibly large number of members; to illustrate the case assume two arguments, as in case class C(s1: String, s2: String) and therefore assume an array with size of at least that many arguments, val a = Array("a1", "a2") Then scala> C(a(0), a(1)) res9: C = c(a1,a2) However, is there an approach to case class instantiation where there is no need to refer to each element in the array for any (possibly large) number of predefined class members ? 回答1: No, you can't.

How to convert generic potentially nested map Map[String, Any] to case class using any library in Scala?

喜夏-厌秋 提交于 2019-12-10 12:38:17
问题 I've not had much joy with reflection, this answer using shapeless works for some cases (but seems to have many edge cases) Shapeless code to convert Map[String, Any] to case class cannot handle optional substructures Does anyone know of a nice library that does this in just a few LOCs? 回答1: Using jackson: libraryDependencies += "com.fasterxml.jackson.core" % "jackson-databind" % "2.9.8" libraryDependencies += "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.9.8" case class Foo(a

How to implement an heterogeneous container in Scala

ぃ、小莉子 提交于 2019-12-10 10:57:39
问题 I need an heterogeneous, typesafe container to store unrelated type A, B, C. Here is a kind of type-level specification : trait Container { putA(a: A) putB(b: B) putC(c: C) put(o: Any) = { o match { case a: A => putA(a) case b: B => putB(b) case c: C => putC(c) } getAllAs : Seq[A] getAllBs : Seq[B] getAllCs : Seq[C] } Which type is best suites to backed this container ? Is it worth creating a Containerable[T] typeclass for types A, B, C ? thks. 回答1: As other have suggested, you can leverage

Test two scala shapeless HList types for equivalence via implicit

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 10:44:22
问题 I'm interested in testing whether two HList heterogeneous records are "equivalent"; that is, they have the same key/val pairs, but not necessarily in the same order. Is there a predefined type predicate that does what EquivHLists does in the code fragment below? // shapeless heterogeneous records with "equivalent" types. // these should compile if given as the arguments to 'f' below. val hrec1 = ("a" ->> 1) :: ("b" ->> 2) :: HNil val hrec2 = ("b" ->> 2) :: ("a" ->> 1) :: HNil // only compiles

Splitting an HList that was concatenated using Prepend[A, B]

本秂侑毒 提交于 2019-12-10 03:36:56
问题 I'm essentially looking for the opposite of the type class Prepend[A, B] . If I have something like: type A = String :: Int :: HNil type B = Boolean :: Double :: HNil val a: A = "a" :: 1 :: HNil val b: B = false :: 2.1 :: HNil scala> val ab = a ++ b ab: shapeless.::[String,shapeless.::[Int,shapeless.::[Boolean,shapeless.::[Double,shapeless.HNil]]]] = a :: 1 :: false :: 2.1 :: HNil I have an HList a of type A and an HList b of type B , I can find a prepend: Prepend[A, B] such that I can