shapeless

Folding a list of different types using Shapeless in Scala

爷,独闯天下 提交于 2019-12-31 10:51:37
问题 As I known, shapeless provides the HList ( Heterogenous list) type which can include multiple types. Is it possible to fold HList ? for example, // ref - Composable application architecture with reasonably priced monad // code - https://github.com/stew/reasonably-priced/blob/master/src/main/scala/reasonable/App.scala import scalaz.{Coproduct, Free, Id, NaturalTransformation} def or[F[_], G[_], H[_]](f: F ~> H, g: G ~> H): ({type cp[α] = Coproduct[F,G,α]})#cp ~> H = new NaturalTransformation[(

Folding a list of different types using Shapeless in Scala

自古美人都是妖i 提交于 2019-12-31 10:51:16
问题 As I known, shapeless provides the HList ( Heterogenous list) type which can include multiple types. Is it possible to fold HList ? for example, // ref - Composable application architecture with reasonably priced monad // code - https://github.com/stew/reasonably-priced/blob/master/src/main/scala/reasonable/App.scala import scalaz.{Coproduct, Free, Id, NaturalTransformation} def or[F[_], G[_], H[_]](f: F ~> H, g: G ~> H): ({type cp[α] = Coproduct[F,G,α]})#cp ~> H = new NaturalTransformation[(

Shapeless code to convert Map[String, Any] to case class cannot handle optional substructures

寵の児 提交于 2019-12-31 04:06:08
问题 I'm trying to use this https://stackoverflow.com/a/31641779/1586965 (How to use shapeless to convert generic Map[String, Any] to case class inside generic function?) to process case class Address(street: String, zip: Int) case class PersonOptionalAddress(name: String, address: Option[Address]) I have a test that fails: "Convert Map to PersonOptionalAddress Some" in { CaseClassFromMap[PersonOptionalAddress](Map( "name" -> "Tom", "address" -> Some(Map("street" -> "Jefferson st", "zip" -> 10000)

Deriving nested shapeless lenses using only a type

我们两清 提交于 2019-12-31 02:45:35
问题 I'm trying to come up with something similar to Classy Lenses to use with cats-mtl. For this, I want to be able to construct a Lens based on provided types only. I found no way to do it using operations provided in shapeless, so I'm writing a new one. import shapeless._ class Classy[O[_, _], S, A](val get: O[S, A]) object Classy { def apply[O[_, _], S, A](implicit ev: Classy[O, S, A]): Classy[O, S, A] = ev implicit def rootLens[S]: Classy[Lens, S, S] = new Classy(OpticDefns.id[S]) implicit

Unable to map on HList

被刻印的时光 ゝ 提交于 2019-12-28 03:00:48
问题 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) }

Checksum at the Type Level using Shapeless

半城伤御伤魂 提交于 2019-12-25 08:42:46
问题 I came up with the following per this problem: package net import shapeless.{::, HNil, _} import ops.hlist.IsHCons import nat._ import ops.nat.{Mod, Prod, Sum} trait IsValid[A] object IsValid { def apply[L <: HList](implicit ev: IsValid[L]) = ev // account number: 3 4 5 8 8 2 8 6 5 // position names: d9 d8 d7 d6 d5 d4 d3 d2 d1 // // checksum calculation: // (d1+2*d2+3*d3 +..+9*d9) mod 11 = 0 implicit def validNumber[D9 <: Nat, D8 <: Nat, D7 <: Nat, D6 <: Nat, D5 <: Nat, D4 <: Nat, D3 <: Nat,

Function that retrieves element from HList (while preserving its type)

别等时光非礼了梦想. 提交于 2019-12-25 03:27:23
问题 I have this type which will be generated via shapeless: type hlistt = STUDENT.type :: AUTO_LOANS.type :: HNil Basically I have a bunch of case objects extending a trait so I managed to create a method which gives me instances of all case objects as an HList Then using import shapeless.ops.hlist.Last and init I wrote a method to retrieve one of the nodes in the HList if the value is equal to the string "student": def getLast(hl:hlistt) = { val last0=Last[hlistt] val la=last0(hl) if (la.value =

Using `String` instead of `Symbol` in `LabelledGeneric` etc

房东的猫 提交于 2019-12-25 01:34:56
问题 As indicated in both the Type Astronaut's Guide to shapeless (footnote 4), and Gitter April 5, 2018 3:35 PM, future versions of shapeless may change the key constraint of KeyTag (and therefore FieldType ) from Symbol to String . I am therefore trying to use string literals (supported in typelevel scala compiler, and also recent 2.12 releases with the -Yliteral-types flag) as the key type in my fields, to future-proof my code. However, at present, all typeclasses using labels, particularly

Kittens - ambiguous imports (again)

柔情痞子 提交于 2019-12-25 00:52:33
问题 This answer https://stackoverflow.com/a/56366311/2682459 shows how it is possible to use an object to provide a custom implementation of a typeclass when using Kitten. Applying the same principle to the following code though doesn't work: package com.xxx.yyy.zzz import cats._, cats.derived._, cats.implicits._ object Test extends App { case class Inner(double: Double) case class Outer(inner: Inner, s: String) implicit object doubleEq extends Eq[Double] { override def eqv(x: Double, y: Double):

Spark 2.1 with Pureconfig 0.8 workaround for Maven

守給你的承諾、 提交于 2019-12-24 20:49:50
问题 The below solution taken from Spark not working with pureconfig seems to be the working solution for sbt but having a hard time figuring out a maven version for doing this. Trying to get pureconfig 0.8 working with spark 2.1 using spark-submit but still getting the pesky Exception in thread "main" java.lang.NoSuchMethodError: shapeless.Witness$.mkWitness(Ljava/lang/Object;)Lshapeless/Witness; error outside of IntelliJ. assemblyShadeRules in assembly := Seq( ShadeRule.rename("shapeless.**" ->