shapeless

Generic transform/fold/map over tuple/hlist containing some F[_]

℡╲_俬逩灬. 提交于 2019-12-21 05:27:07
问题 I recently asked Map and reduce/fold over HList of scalaz.Validation and got a great answer as to how to transform a fixed sized tuple of Va[T] (which is an alias for scalaz.Validation[String, T] ) into a scalaz.ValidationNel[String, T] . I've since then been studying Shapeless and type level programming in general to try to come up with a solution that works on tuples of any size. This is what I'm starting out with: import scalaz._, Scalaz._, shapeless._, contrib.scalaz._, syntax.std.tuple._

Convert a Seq[String] to a case class in a typesafe way

安稳与你 提交于 2019-12-21 04:54:18
问题 I have written a parser which transforms a String to a Seq[String] following some rules. This will be used in a library. I am trying to transform this Seq[String] to a case class. The case class would be provided by the user (so there is no way to guess what it will be). I have thought to shapeless library because it seems to implement the good features and it seems mature, but I have no idea to how to proceed. I have found this question with an interesting answer but I don't find how to

Getting elements from an HList

故事扮演 提交于 2019-12-20 21:46:29
问题 I toyed around with HList and the following works as expected: val hl = 1 :: "foo" :: HNil val i: Int = hl(_0) val s: String = hl(_1) However, I can't get the following piece of code working (let's assume for a moment random access on lists is a smart idea ;-)): class Container(hl: HList) { def get(n: Nat) = hl(n) } val container = new Container(1 :: "foo" :: HNil) val i: Int = container.get(_0) val s: String = container.get(_1) I'd like to have get return an Int and String according to it's

Scala parameters pattern (Spray routing example)

江枫思渺然 提交于 2019-12-20 10:47:30
问题 Sorry about the vague title...wasn't sure how to characterize this. I've seen/used a certain code construction in Scala for some time but I don't know how it works. It looks like this (example from Spray routing): path( "foo" / Segment / Segment ) { (a,b) => { // <-- What's this style with a,b? ... }} In this example, the Segements in the path are bound to a and b respectively inside the associated block. I know how to use this pattern but how does it work? Why didn't it bind something to

Materialize the Value for a Type that has One Inhabitant

Deadly 提交于 2019-12-20 10:25:30
问题 Thanks to @MilesSabin's answer I can write a type level Fibonacci sequence: sealed trait Digit case object Zero extends Digit case object One extends Digit sealed trait Dense { type N <: Dense } sealed trait DNil extends Dense { type N = DNil } case object DNil extends DNil final case class ::[+H <: Digit, +T <: Dense](digit: H, tail: T) extends Dense { type N = digit.type :: tail.N } /* The `A`th Fibonacci number is `B` */ trait Fib[A <: Dense, B <: Dense] object Fib { implicit val f0 = new

Shapeless: Generic.Aux

妖精的绣舞 提交于 2019-12-20 08:29:52
问题 I'm trying to understand how Generic works (and TypeClass too). The github wiki is very sparse on examples and documentation. Is there a canonical blog post / documentation page describing Generic and TypeClass in detail? In concrete, what is the difference between these two methods?: def find1[T](implicit gen: Generic[T]): Generic[T] = gen def find2[T](implicit gen: Generic[T]): Generic[T] { type Repr = gen.Repr } = gen given object Generic { type Aux[T, Repr0] = Generic[T] { type Repr =

Shapeless: own HList constraint using Coproduct

混江龙づ霸主 提交于 2019-12-20 03:22:03
问题 (NOTE: Split from Shapeless: Trying to restrict HList elements by their type ) Question 2 - Own Constraint using Coproduct What I really wanted to do is to write a new constraint using Coproduct. trait CPConstraint[L <: HList, CP <: Coproduct] extends Serializable object CPConstraint { import shapeless.ops.coproduct.Selector._ def apply[L <: HList, CP <: Coproduct](implicit cpc: CPConstraint[L, CP]): CPConstraint[L, CP] = cpc type <*<[CP <: Coproduct] = { // TODO: just invented a symbol ...

Scala: type-based list partitioning

天大地大妈咪最大 提交于 2019-12-19 03:49:17
问题 I have this code that I would like to improve on: sealed abstract class A case class B() extends A case class C() extends A case class D() extends A case class Foo[+T <: A](a: T) /** Puts instances that match Foo(B()) in the first list and everything else, * i.e. Foo(C()) and Foo(D()), in the second list. */ def partition(foos: List[Foo[_ <: A]]): (List[Foo[B]], List[Foo[_ <: A]]) = { // ... } I would like to improve on this in the following respects: Can I change partition 's return type so

scala - generic unzip for HList

你离开我真会死。 提交于 2019-12-18 13:31:31
问题 I have the following Scala problem: Write a function that will take a list of HLists List(23 :: “a” :: 1.0d :: HNil, 24 :: “b” :: 2.0d :: HNil) # this is list of hlists and return back HList of Lists List[Int](23, 24) :: List[String](“a”, “b") :: List[Double](1.0d, 2.0d) :: HNil # this is hlist of lists This is sort of like generic unzipN. Is it at all possible for arbitrary HList? Thank you. 回答1: There are lots of ways to solve this problem, and defining a custom type class (as in Nikita's

Shapeless: generic lens parameterized by case class or field

倖福魔咒の 提交于 2019-12-18 04:08:23
问题 Based on: import shapeless._ case class Content(field: Int) lens[Content] >> 'field I am trying to make a lens-creating method, something along: def makeLens[T <: Product](s: Symbol) = lens[T] >> s But it seems non-obvious. Is it possible to do? If not, the end result I'm trying to achieve is a generic method for updating nested Maps with case-class contents, e.g.: import scalaz._ import Scalaz._ import PLens._ import shapeless._ import shapeless.contrib.scalaz._ def nestedMapLens[R, T <: