shapeless

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

蓝咒 提交于 2019-12-03 16:15:16
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._ type Va[A] = Validation[String, A] // only works on pairs of Va[_] def validate[Ret, In1, In2](params:

Map on HList in method with Poly1 based on type parameter of class

落爺英雄遲暮 提交于 2019-12-03 16:03:19
I have class, parameterized with HList and some other type. How can I use map on HList in one of its methods? Compilation of this code throws java.lang.AssertionError : class Test[L <: HList, P](l: L, p: P) { type Cont[T] = (P, T) object generator extends (Id ~> Cont) { def apply[T](t: T) = p -> t } def test(implicit m: Mapper[generator.type, L]) = { l map generator } } new Test(1 :: HNil, 'a).test // java.lang.AssertionError My goal is this kind of result: type Cont[T] = (Symbol, T) val p = 'a object generator extends (Id ~> Cont) { def apply[T](t: T) = p -> t } scala> (1 :: 'b' :: HNil) map

Is ambiguous implicit value the only way we want to make the error existed in compilation time

二次信任 提交于 2019-12-03 16:01:19
trait Foo trait Bar extends Foo def doStuff[T <: Foo](x: T)(implicit ev: T =!:= Foo) = x doStuff(new Foo{}) //ambiguous implicit value doStuff(new Bar)// successful Implicit resolution is happening on compilation time, so in here I think there may be two implicit value with exactly same type to trigger ambiguous stuff. Right now, I am going to introduce shapeless into the team, my colleagues think this ambiguous implicit is not ideal, and I dont have strong argument about it. Is this only way to do it in order to make type safe in scala. If it is, what can I do to customize the error message ?

Deriving type class instances for case classes with exactly one field

梦想的初衷 提交于 2019-12-03 15:35:06
I'm working on a CSV parsing library ( tabulate ). It uses simple type classes for encoding / decoding: encoding, for example, is done with instances of CellEncoder (to encode a single cell) and RowEncoder (to encode entire rows). Using shapeless, I've found it pretty straightforward to automatically derive the following type class instances: RowEncoder[A] if A is a case class whose fields all have a CellEncoder . RowEncoder[A] if A is an ADT whose alternatives all have a RowEncoder . CellEncoder[A] if A is an ADT whose alternatives all have a CellEncoder . The thing is, this last one turns

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

情到浓时终转凉″ 提交于 2019-12-03 14:17:59
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 transform it for my needs. Indeed, in the answer there is only one type to parse (String), and the library

What does HList#foldLeft() return?

情到浓时终转凉″ 提交于 2019-12-03 14:13:20
I'm trying to play with HList's from Shapeless. This is my first try: trait Column[T] { val name: String } case class CV[T](col: Column[T], value: T) object CV { object columnCombinator extends Poly2 { implicit def algo[A] = at[(String, String, String), CV[A]] { case ((suffix, separator, sql), cv) ⇒ (suffix, separator, if (sql == "") cv.col.name+suffix else sql+separator+cv.col.name+suffix) } } def combine[A <: HList](columns: A, suffix: String, separator: String = " and ") (implicit l: LeftFolder[A, (String, String, String), columnCombinator.type]): String = columns.foldLeft((suffix,

In shapeless, have two lists such that one contains typeclasses of the other

拈花ヽ惹草 提交于 2019-12-03 12:45:47
In shapeless I'm trying to write a function such that takes two HLists l1 and l2 of arbitrary length which exhibit the following properties: Length of l1 and l2 are the same. l2 contains the exact types of l1 , wrapped in a constant outer type constructor. So, if l1 was 1 :: 1.2 :: "hello" :: HNil` l2 could be Ordering[Int] :: Ordering[Double] :: Ordering[String] :: HNil Using UnaryTCConstraint and LengthAux lets me constrain the lengths and require a static outer constructor for l2 , however having them conform has become a problem. Any ideas on how I could go about it? Mapped provides

What is the purpose of the emptyCoproduct and coproduct methods of the TypeClass trait in Shapeless

半城伤御伤魂 提交于 2019-12-03 10:32:04
问题 It is not fully clear to me what is the purpose of the emptyCoProduct and coproduct methods of the TypeClass trait in Shapeless. When would one use the TypeClass trait instead of the ProductTypeClass ? What are some examples of ways those two methods would be implemented? 回答1: Suppose I've got a simple type class: trait Weight[A] { def apply(a: A): Int } object Weight { def apply[A](f: A => Int) = new Weight[A] { def apply(a: A) = f(a) } } And some instances: implicit val stringWeight: Weight

Shapeless not finding implicits in test, but can in REPL

落爺英雄遲暮 提交于 2019-12-03 08:49:56
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: Int) object CustomProtocol { implicit val colorLabel: LabelledGeneric[Color] = LabelledGeneric[Color]

Using shapeless to convert tuple of Future to Future of tuple by way of HList

对着背影说爱祢 提交于 2019-12-03 07:36:36
Is there an easy way to convert a tuple of type (Future[A], Future[B], Future[C], ..., Future[N]) to Future[(A, B, C, ..., N)]? This assumes an undefined number of elements in the tuple. I've tried converting the tuple to HList and tried a similar trick of foldLeft and for comprehension as done in Future.sequence but no luck dealing with the types passed into the fold. Tried the same with recursive functions. But this code still does not compile due to missing HList.head, HList.tail. The code looks like follows: def sequence[L <: HList](in: L)(implicit ihc: IsHCons[L]) = { val list = for (i <-