shapeless

Subtype polymorphism in shapeless mapping

这一生的挚爱 提交于 2019-12-01 03:01:25
问题 I've constructed the following: import shapeless._ import poly._ object Main { def main(args: Array[String]) = { object iterateOverHList extends (List ~> Iterator) { def apply[T](it: List[T]) = it.iterator } val x = List(1,2,3) :: List("cat","dog") :: HNil val xIt = x map iterateOverHList } } The above code works great and is awesome. However, I still want more. I would like to, rather than specifying that my HList will contain Lists, allow any Iterable. Like this: import shapeless._ import

Scala: type-based list partitioning

[亡魂溺海] 提交于 2019-11-30 23:28:15
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 that it states that there is no Foo[B] in the second list? Can I get rid of Foo 's type parameter T (i

Evidence that types are not equal in Scala [duplicate]

不羁的心 提交于 2019-11-30 22:13:41
This question already has an answer here: Enforce type difference 7 answers Is there any way to constraint a method so that it only makes sense if two types are proved not to be equal? trait Something[A, B] { // I can only be called if type A is the same as type B def ifEqual(implicit ev: A =:= B) // Now I cannot be called if type A is proven to be the same as type B def ifNotEqual(implicit ev: A ??? B) } Yes. From shapeless , // Type inequalities trait =:!=[A, B] implicit def neq[A, B] : A =:!= B = new =:!=[A, B] {} implicit def neqAmbig1[A] : A =:!= A = ??? implicit def neqAmbig2[A] : A =:!=

leftReduce Shapeless HList of generic types

主宰稳场 提交于 2019-11-30 20:40:30
This is essentially what I want: case class Foo[T](x: T) object combine extends Poly { implicit def caseFoo[A, B] = use((f1: Foo[A], f2: Foo[B]) => Foo((f1.x, f2.x))) } def combineHLatest[L <: HList](l: L) = l.reduceLeft(combine) So combineHLatest(Foo(1) :: Foo("hello") :: HNil) should yield Foo( (1, "hello") ) The above doesn't compile as it cannot find an implicit LeftReducer but I'm at a loss as to how to implement one. It was pretty late last night when I answered this, and while the information in the original answer below is correct, it's not necessarily the most helpful presentation.

Evidence that types are not equal in Scala [duplicate]

老子叫甜甜 提交于 2019-11-30 17:47:45
问题 This question already has answers here : Enforce type difference (7 answers) Closed 2 years ago . Is there any way to constraint a method so that it only makes sense if two types are proved not to be equal? trait Something[A, B] { // I can only be called if type A is the same as type B def ifEqual(implicit ev: A =:= B) // Now I cannot be called if type A is proven to be the same as type B def ifNotEqual(implicit ev: A ??? B) } 回答1: Yes. From shapeless, // Type inequalities trait =:!=[A, B]

Heterogeneous arguments in a Scala function

与世无争的帅哥 提交于 2019-11-30 14:01:01
How can I pass some HList as an argument? So I can make in a such way: def HFunc[F, S, T](hlist: F :: S :: T :: HNil) { // here is some code } HFunc(HList(1, true, "String")) // it works perfect But if I have a long list, and I dunno nothing about it, how can I make some operations on it? How can I pass argument and not to loose its type? It depends on your use-case. HList is useful for type-level code, so you should pass to your method not only HList , but also all necessary information like this: def hFunc[L <: HList](hlist: L)(implicit h1: Helper1[L], h2: Helper2[L]) { // here is some code

What is “at” in shapeless (scala)?

风格不统一 提交于 2019-11-30 03:41:58
I've seen an object (probably a function) called "at" sprinkled throughout the shapeless source and in code that uses shapeless. In particular, it is used in the answer to this other question . Here is the code snippet: object iterateOverHList extends Poly1 { implicit def iterable[T, L[T] <: Iterable[T]] = at[L[T]](_.iterator) } I've had some clue that it is related to the apply method of the ~> type. What specifically does "at" do, and where is it defined? Definition of PolyN#at at is a general way to work with Poly . ~> with apply is a special case of Poly1 . apply here is used to define

Heterogeneous arguments in a Scala function

孤街浪徒 提交于 2019-11-29 19:22:35
问题 How can I pass some HList as an argument? So I can make in a such way: def HFunc[F, S, T](hlist: F :: S :: T :: HNil) { // here is some code } HFunc(HList(1, true, "String")) // it works perfect But if I have a long list, and I dunno nothing about it, how can I make some operations on it? How can I pass argument and not to loose its type? 回答1: It depends on your use-case. HList is useful for type-level code, so you should pass to your method not only HList , but also all necessary information

Spark not working with pureconfig

前提是你 提交于 2019-11-29 14:57:27
I'm trying to use pureConfig and configFactory for my spark application configuration. here is my code: import pureconfig.{loadConfigOrThrow} object Source{ def apply(keyName: String, configArguments: Config): Source = { keyName.toLowerCase match { case "mysql" => val properties = loadConfigOrThrow[DBConnectionProperties](configArguments) new MysqlSource(None, properties) case "files" => val properties = loadConfigOrThrow[FilesSourceProperties](configArguments) new Files(properties) case _ => throw new NoSuchElementException(s"Unknown Source ${keyName.toLowerCase}") } } } import Source val

Implicit Resolution Failure?

℡╲_俬逩灬. 提交于 2019-11-29 14:20:35
问题 I have been working on a "shapeless style" implementation of Okasaki's dense binary number system. It's simply a type-level linked-list of bits; a sort of HList of binary Digit s. I have completed a first draft of my ops, which include the standard math operations you'd expect for natural numbers. Only now do I realize a big problem in my encoding. How do I fix the implicit resolution in my Induction example? Feel free to paste the entire snippet into a REPL. In this example, the only