shapeless

Spark not working with pureconfig

£可爱£侵袭症+ 提交于 2019-11-28 08:33:37
问题 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 _ =>

Safely copying fields between case classes of different types

我是研究僧i 提交于 2019-11-28 07:02:37
Assuming you have case classes like the following case class Test1(a:String,b:Int,c:Char) case class Test2(a:String,b:Int) And you instantiate the classes with the following variables val test1 = Test1("first",2,'3') val test2 = Test2("1st",20) Is there a way to use the .copy method (or otherwise), to apply the variables inside Test2 to Test1, something like val test3 = test1.copy(test2) //Note this isn't valid scala code // Result should be ("1st",20,'3') If this isn't possible in pure scala, how would it be done in Shapeless 1/2 (current code is in Shapeless 1, however we are planning to

How to append or prepend an element to a tuple in Scala

江枫思渺然 提交于 2019-11-28 06:18:57
I have a tuple and want to add an element without loosing type safety. This is what I want to achieve: val tuple = ("", 1, 1f) // (String, Int, Float) val newTuple:(String, Int, Float, Double) = tuple :+ 1d It's worth noting that you can also write a code generator for this in a few lines: val tupadd = for (n <- 2 to 20) yield { val t = (0 until n).map(i => ('A'+i).toChar).mkString(", ") val u = ('A'+n).toChar val i = (0 until n).map(i => "x._"+(i+1)).mkString(", ") List( s"implicit class TupOps$n[$t](val x: ($t)) extends AnyVal {", s" def :+[$u](y: $u) = ($i, y)", s" def +:[$u](y: $u) = (y,

Limits of Nat type in Shapeless

六眼飞鱼酱① 提交于 2019-11-28 05:35:35
In shapeless, the Nat type represents a way to encode natural numbers at a type level. This is used for example for fixed size lists. You can even do calculations on type level, e.g. append a list of N elements to a list of K elements and get back a list that is known at compile time to have N+K elements. Is this representation capable of representing large numbers, e.g. 1000000 or 2 53 , or will this cause the Scala compiler to give up? I will attempt one myself. I will gladly accept a better answer from Travis Brown or Miles Sabin. Nat can currently not be used to represent large numbers In

Type casting using type parameter

左心房为你撑大大i 提交于 2019-11-27 21:45:37
问题 Given is a Java method that returns java.lang.Object s for a given string. I'd like to wrap this method in a Scala method that converts the returned instances to some type T . If the conversion fails, the method should return None . I am looking for something similar to this: def convert[T](key: String): Option[T] = { val obj = someJavaMethod(key) // return Some(obj) if obj is of type T, otherwise None } convert[Int]("keyToSomeInt") // yields Some(1) convert[String]("keyToSomeInt") // yields

Passing a Shapeless Extensible Record to a Function

ⅰ亾dé卋堺 提交于 2019-11-27 20:18:56
问题 I am trying to learn Shapeless (using version 2.10.2). I have created a very simple extensible record: val rec1 = ("foo" ->> 42) :: HNil According to the REPL, this has type shapeless.::[Int with shapeless.record.KeyTag[String("foo"),Int],shapeless.HNil] I am trying to define a simple function: def fun(x: ::[Int with KeyTag[String("foo"), Int], HNil]) = x("foo") but it does not even compile. I cannot use a String("foo") in the type declaration, and get an error. I have two questions: How can

Testing an assertion that something must not compile

本小妞迷上赌 提交于 2019-11-27 17:17:35
The problem When I'm working with libraries that support type-level programming, I often find myself writing comments like the following (from an example presented by Paul Snively at Strange Loop 2012 ): // But these invalid sequences don't compile: // isValid(_3 :: _1 :: _5 :: _8 :: _8 :: _2 :: _8 :: _6 :: _5 :: HNil) // isValid(_3 :: _4 :: _5 :: _8 :: _8 :: _2 :: _8 :: _6 :: HNil) Or this, from an example in the Shapeless repository: /** * If we wanted to confirm that the list uniquely contains `Foo` or any * subtype of `Foo`, we could first use `unifySubtypes` to upcast any * subtypes of

Converting a tuple of options to an option of tuple with Scalaz or Shapeless

青春壹個敷衍的年華 提交于 2019-11-27 14:40:51
问题 Having (Some(1), Some(2)) I expect to get Some((1, 2)) and having (Some(1), None) I expect to get None 回答1: You can use the fact that Scalaz 7 provides a Bitraverse instance for tuples and then sequence as usual (but with bisequence instead of sequence ): scala> import scalaz._, std.option._, std.tuple._, syntax.bitraverse._ import scalaz._ import std.option._ import std.tuple._ import syntax.bitraverse._ scala> val p: (Option[Int], Option[String]) = (Some(1), Some("a")) p: (Option[Int],

Getting subclasses of a sealed trait

心已入冬 提交于 2019-11-27 14:09:24
Is it possible (via macros, some form of Shapeless automagic or otherwise) to obtain a list of the subclasses of a sealed trait: At compile time? At runtime? You don't need any 3rd party library to do this: sealed trait MyTrait case object SubClass1 extends MyTrait case object SubClass2 extends MyTrait import scala.reflect.runtime.{universe => ru} val tpe = ru.typeOf[MyTrait] val clazz = tpe.typeSymbol.asClass // if you want to ensure the type is a sealed trait, // then you can use clazz.isSealed and clazz.isTrait clazz.knownDirectSubclasses.foreach(println) Output: object SubClass1 object

Sequencing an HList

梦想的初衷 提交于 2019-11-27 12:59:12
问题 Given a Shapeless HList where every list element shares the same type constructor, how can the HList be sequenced? For example: def some[A](a: A): Option[A] = Some(a) def none[A]: Option[A] = None val x = some(1) :: some("test") :: some(true) :: HNil val y = sequence(x) // should be some(1 :: "test" :: true :: HNil) def sequence[L <: HList : *->*[Option]#λ, M <: HList](l: L): Option[M] = ??? I tried to implement sequence like this: object optionFolder extends Poly2 { implicit def