shapeless

Shapeless map HList depending on target types

会有一股神秘感。 提交于 2019-12-04 21:36:54
问题 I have the following problem, I want to map items of an HList to another HList but Strings in the source HList should only be converted to URL if the "target" type is URL. val name = "Stackoverflow" val url = "https://stackoverflow.com/q" val list = name :: url :: HNil val mapped: String :: URL :: HNil = list.map(???) As far as my research took me is that all the Poly stuff only cares about the input type but not about the output type. So are there ways to archive my goal ? 回答1: I don't think

Filter usage in shapeless, Scala

你。 提交于 2019-12-04 19:58:21
问题 It is easy to filter HList in shapeless by type: val hlist = 1 :: 2 :: "3" :: true :: false :: HNil hlist.filter[Int] But how can I make my custom type filter? I want smth like that: for example I got list of some functions: def function1(s: String) = s.toInt def function2(s: String) = s.toDouble def function3(i: Int) = i.toDouble val hflist = function1 _ :: function3 _ :: function2 _ :: HNil hflist customFilter[String] //> function1 _ :: function2 _ :: HNil So after usage of this filter,

Parse List[String] into HList

喜欢而已 提交于 2019-12-04 19:37:45
I would like to write def parse[T <: HList](list: List[String]): Validation[T] . list could be List("fooid", "barid") , and T FooId :: BarId :: HNil , and a typeclass Parse[T] which implements String => Validation[FooId] . How would I write said parse , which parses the list into T ? I'm not sure how to summon the implicit typeclasses for each of the elements of T . We can adapt the code from shapeless.ops.traversable.FromTraversable . I'm not sure what your Validation type is, but I used it as an alias for scalaz.ValidationNel[String, A] below (mostly so I could use the string syntax to

HList filtered by foldRight is not providing instances

核能气质少年 提交于 2019-12-04 14:54:59
I'm using libraryDependencies += "com.chuusai" %% "shapeless" % "2.2.4" Currently i have model HList types like sealed trait Section case class Header(...) extends Section case class Customer(...) extends Section case class Supplier(...) extends Section case class Tech(...) extends Section type ContractView = Header :: (Customer :: Supplier :: HNil) :: Tech :: HNil In my user code, i'd like to filter technical sections that are not supposed to view using foldRight as proposed in this answer : trait collectAllRF extends Poly2 { implicit def atAny[L <: HList, X] = at[X, L](_ :: _) } object

Idiomatic Scala way of deserializing delimited strings into case classes

China☆狼群 提交于 2019-12-04 14:44:38
Suppose I was dealing with a simple colon-delimited text protocol that looked something like: Event:005003:information:2013 12 06 12 37 55:n3.swmml20861:1:Full client swmml20861 registered [entry=280 PID=20864 queue=0x4ca9001b] RSET:m3node:AUTRS:1-1-24:A:0:LOADSHARE:INHIBITED:0 M3UA_IP_LINK:m3node:AUT001LKSET1:AUT001LK1:r OPC:m3node:1-10-2(P):A7:NAT0 .... I'd like to deserialize each line as an instance of a case class, but in a type-safe way. My first attempt uses type classes to define 'read' methods for each possible type that I can encounter, in addition to the 'tupled' method on the case

Scala shapeless KList with extra constraint

混江龙づ霸主 提交于 2019-12-04 14:40:42
I want to take this pattern: def accept[T](a: RList[T]) = true def accept[T, V](a: RList[T], b: RList[V])(implicit ev: a.S =:= b.S) = true def accept[T, V, Q](a: RList[T], b: RList[V], c: RList[Q])(implicit ev: a.S =:= b.S, ev2: b.S =:= c.S) = true but have it accept a KList , instead of manually overriding for all arities. Basically I want to say, "Take any number of RList s, that have the same S member type" RList is a trait, that contains a type S . (For more background on an RList and why I'm doing this, see: Constrain function based on origin (Path Dependent type? Type Generation?) ) It

Shapeless deconstruct tuple in type parameter declaration

时光怂恿深爱的人放手 提交于 2019-12-04 14:14:28
I am using a RightFolder that returns a Tuple2 and would like to return the _1 part. The first version rightFoldUntupled1 works fine but uses an additional IsComposite typeclass. In the second version rightFoldUntupled2 I try to achieve the same without IsComposite by destructuring P as a Product2[_, Values] . That doesn't work any more - the compiler is happy to witness that P is a Product2[_,_] , but as soon as I use a named type parameter for _1 it doesn't compile any more. Any idea why? The full source (with sbt ready to ~run with implicit debug output) is here: https://github.com

Deriving HList of zeroes from a type of HList of Monoids

帅比萌擦擦* 提交于 2019-12-04 13:44:49
I am learning shapeless, and currently I am trying to create a function that does the following: given a type of an HList it returns the HList of None s, with the Option types corresponding to given HList type. For instance: create[String :: Int :: HNil] // returns None[String] :: None[Int] :: HNil So the logic is the following: def create[A <: HList] { type HT = ??? //somehow getting Head type type TT = ??? //somehow getting Tail type // if HT is HNil HNil else Option.empty[HT] :: create[TT] } Looks like the HT and TT can be provided by IsHCons def createHList[L <: HList](implicit ihc:

Reshape a case class constructor?

旧时模样 提交于 2019-12-04 12:27:38
问题 Trying to find a way to "reshape" a case constructor to filling some default value. Is the following possible? def reshape[T, R1 <: HList, R2 <: HList](h: R1): R2 => T = ??? //example case class MyClass(a: Double, b: String, c: Int) val newConstructor = reshape[MyClass]('b ->> "bValue" :: HNil) newConstructor('a ->> 3.1 :: 'c ->> 4 :: HNil) res1: MyClass = MyClass(3.1, "bValue", 4) Is it possible with shapeless or do we have to go the macro route? 回答1: It's possible to construct such reshaper

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

巧了我就是萌 提交于 2019-12-04 12:19:42
问题 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