shapeless

Pick out the Nth element of a HList of Lists and return that value as a HList of values

时光总嘲笑我的痴心妄想 提交于 2019-12-09 11:08:33
问题 I have an HList in which each column represents a column of a table. Each list in the HList is of the same length. I'd like to be able to write a function which picks out individual rows of this table as a tuple or an HList of values. Eventually I will convert this to something a bit more sensible (e.g. a Case Class). import shapeless.PolyDefns.~> import shapeless.{HList, HNil} val a = List(1,2,3) :: List("a", "b", "c") :: List(true, false, true) :: HNil object broken extends (HList ~> HList)

Shapeless not finding implicits in test, but can in REPL

假如想象 提交于 2019-12-09 06:28:29
问题 I have a case class that looks like this: case class Color(name: String, red: Int, green: Int, blue: Int) I'm using Shapeless 2.3.1 with Scala 2.11.8. I'm seeing different behavior from my test and the REPL in terms of finding the implicit value for LabelledGeneric[Color] . (I'm actually trying to auto-derive some other typeclass, but I'm getting null for that too) Inside test package foo import shapeless._ import org.specs2.mutable._ case class Color(name: String, red: Int, green: Int, blue:

Passing a shapeless extensible record to a function (never ending story?

强颜欢笑 提交于 2019-12-09 06:07:18
问题 I continue to investigate extensible records as in Passing a Shapeless Extensible Record to a Function (continued): the provided solution works with functions that all takes a parameter that includes at least foo1, foo2, and foo3; that is one can write: fun1(("foo1" ->> "hello") :: ("foo2" ->> 1) :: ("foo3" ->> 1.2) :: HNil) fun1(("foo1" ->> "hello") :: ("foo2" ->> 1) :: ("foo3" ->> 1.2) :: ("foo4" ->> true) :: HNil) and so on And if we can add a second function fun2: def fun2[L <: HList :

How do you debug typelevel code?

寵の児 提交于 2019-12-09 06:01:43
问题 Most of the time, all you get is an implicit not found error. You don't know where in the chain of implicit construction it failed. Apparently you can't use runtime debug or print statement. So how do you debug type-level program other than staring at your code really hard? 回答1: I wish I had a better answer, but here it goes: Start passing the parameters explicitly, one at a time until it gives you a more useful error. (adding-prinlns-equivalent for implicits params) 回答2: You can use ??? for

How can I use Shapeless to create a function abstracting over arity

六月ゝ 毕业季﹏ 提交于 2019-12-09 04:46:44
问题 Let's consider a specific example. I have lots of functions that take a variable number of arguments, and return a Seq[T] . Say: def nonNeg(start: Int, count: Int): Seq[Int] = Iterator.from(start).take(count).toSeq For each one of those function, I need to create a "Java version" of that function, returns a java.util.List[T] . I can create the "Java version" of the above function with: def javaNonNeg(start: Int, count: Int): java.util.List[Int] = nonNeg(start, count).asJava This is somewhat

Deriving decoder instances of case classes with a single field

戏子无情 提交于 2019-12-08 08:48:57
问题 This question is tightly related to this one, but not identical: it's the same context but the opposite problem. I'm trying to derive a CellDecoder[A] , which is essentially a String => A , for A s that are case classes with a single field whose type has a CellDecoder . In order to do this, I need to require: A to be transformable to R , where R is a subtype of H :: HNil . H to have CellDecoder instance. That is: implicit def caseClassCellDecoder[A, R, H](implicit gen: Generic.Aux[A, R], ev:

HList to nested Map

半世苍凉 提交于 2019-12-08 07:52:49
问题 I would like to transform an HList type parameter to a nested Map-type, e.g. Int :: String :: String :: HNil should become Map[Int, Map[String, Map[String, T]]]] where T would be another type parameter of the same function, like: def somedef[T, L <: HList](t: T)(implicit f: ???): f.Out where f.Out is T in case of HNil or a nested Map-structure with dept L.size Is there any way this can be done? 回答1: I'm not aware of a standard thing to do such a transformation, but you could roll out your

HList to nested Map

半世苍凉 提交于 2019-12-08 02:42:28
I would like to transform an HList type parameter to a nested Map-type, e.g. Int :: String :: String :: HNil should become Map[Int, Map[String, Map[String, T]]]] where T would be another type parameter of the same function, like: def somedef[T, L <: HList](t: T)(implicit f: ???): f.Out where f.Out is T in case of HNil or a nested Map-structure with dept L.size Is there any way this can be done? I'm not aware of a standard thing to do such a transformation, but you could roll out your custom converter in the same way as various HList ops (like map ) are implemented inside shapeless (see trait

Structural Programming in Scala with Shapeless: How to use the SYB implementation correctly?

五迷三道 提交于 2019-12-08 01:30:16
问题 I want to use the SYB implementation in the Shapeless library to write the following generic traversal function: class Data // Perform the desired manipulation on the given data object manipulate extends ->((data: Data) => data) def traverseAndManipulate[B](expr: B): B = { everywhere(manipulate)(expr) } Unfortunately, this code produces the following type error (using Shapeless 2.0.0-M1 and Scala 2.10.2): type mismatch; [error] found : shapeless.EverywhereAux[SYB.manipulate.type] [error]

Using Shapeless records to combine arbitrary number of state-changing functions

跟風遠走 提交于 2019-12-07 23:51:16
问题 I'm trying to port combineReducers from Redux to Scala. The idea is that each function controls it's small part of the state and combineReducers creates a function that controls the whole state. I can't figure out required signature for the function that should work something like this: sealed trait Event case class Create() extends Event case class Save() extends Event object Reducers { def combineReducers[E, S1, S2](fTag: String, f: (E, S1) => S1, gTag: String, g: (E, S2) => S2) = ??? type