hlist

Type variables in context not fixed?

谁都会走 提交于 2019-12-24 07:08:00
问题 I'm currently experimenting with typelevel code. I've got one instance with a type variable that only occurs in the context, and not in the instance itself. Somehow the compiler doesn't like that, but I can't figure out why not. Adding a functional dependency to HasRecipe effect pot target -> deps works for that error, but then the incorrect deps are inferred at the test in the last line of the code. Error: • Could not deduce (HasRecipe target pot effect deps0) from the context: (HasRecipe

Type inference on contents of shapeless HList

冷暖自知 提交于 2019-12-22 08:45:30
问题 This example is simplified. I have a set of classes like this: case class KeyMapping[KeyType](k:KeyType) class WrappedMapping[KeyType](m:T forSome {type T <: KeyMapping[KeyType]}) { val k:KeyType = ??? } In the following code the types are correctly inferred: val w = new WrappedMapping(KeyMapping("key")) //The statement below gives the correct error //type mismatch; // found : w.k.type (with underlying type String) required: Nothing //val test1:Nothing = w.k I have no clue how to infer the

The length of HList type paremeter in terms of Nat

会有一股神秘感。 提交于 2019-12-21 21:31:07
问题 Suppose I have a method without params. How can I determine a length of type parameter? def func[T <: HList]: Nat = { // some magic } 回答1: You can use ops.hlist.Length operation to calculate the Nat length of an HList . Also, getting it as an opaque Nat is not very useful, because you lose all the type-level information about the actual number. So you have to get the exact Nat type from the function: import shapeless._ import shapeless.ops.hlist.Length def func[T <: HList](implicit len:

Convert scala List[String]/List[Object] into model/HList/tuple

好久不见. 提交于 2019-12-21 05:43:08
问题 An external system returns Seq[String] (kind of DB, output like CSV/json), it's wrap of base types: string/numbers. I'd rather work with my own model. object Converter { type Output = (Int, String, Double) // for instance def convert(values: List[String]): Output } Obviously, I do not want to implement convert-method every time. Seems like I need something simpler than http://nrinaudo.github.io/tabulate/tut/parsing.html Is it possible to use HList here? Like conversion sized HList (String::

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 - 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

Applying an argument list to curried function using foldLeft in Scala

馋奶兔 提交于 2019-12-17 09:39:42
问题 Is it possible to do a foldLeft on a list of arguments, where the initial value supplied to the fold is a fully curried function, the operator is apply , and the list is a list of arguments to be passed to function f ? For example, let's say f is defined as: scala> val f = (i: Int, j: Int, k: Int, l: Int) => i+j+k+l f: (Int, Int, Int, Int) => Int = <function4> Which we can of course use directly: scala> f(1, 2, 3, 4) res1: Int = 10 Or curry and apply the arguments one at a time: scala> f

Map for generic HList

*爱你&永不变心* 提交于 2019-12-12 03:06:45
问题 Say we have following method def func[T <: HList](hlist: T, poly: Poly) (implicit mapper : Mapper[poly.type, T]): Unit = { hlist map poly } and custom Poly object f extends (Set ~>> String) { def apply[T](s : Set[T]) = s.head.toString } So I can use this func like func(Set(1, 2) :: Set(3, 4) :: HNil, f) In my code I have small number of Polies and a big number of func invocations. For this purpose I tried to move poly: Poly to implicit parameters and got expected message illegal dependent

Scala, gremlin-scala, HLists, Poly2, RightFold and a missing implicit Prepend

夙愿已清 提交于 2019-12-10 18:16:20
问题 So, I am trying to encapsulate a series of operations from gremlin-scala into an HList so I can do a RightFold over them (this would allow me to construct a gremlin query as data: specifically an HList of Operations ). Here is what I mean: usually you can make a call to gremlin-scala like so: import gremlin.scala._ import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory def graph = TinkerFactory.createModern.asScala graph.V.hasLabel("person").out("created").as("creations")

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