scala

How to make implicits available to inner function

[亡魂溺海] 提交于 2020-12-06 07:07:05
问题 I would like to define implicit value in a wrapper function and make it available to inner function, so far I managed to do that by passing implicit variable from wrapper: case class B() trait Helper { def withImplicit[A]()(block: => A): A = { implicit val b: B = B() block } } class Test extends Helper { def useImplicit()(implicit b: B): Unit = {...} def test = { withImplicit() { implicit b: B => useImplicit() } } } Is it possible to avoid implicit b: B => and make implicit val b: B = B()

spark dataframe conversion to rdd takes a long time

佐手、 提交于 2020-12-06 01:37:43
问题 I'm reading a json file of a social network into spark. I get from these a data frame which I explode to get pairs. This process works perfect. Later I want to convert this to RDD (For use with GraphX) but the RDD creation takes a very long time. val social_network = spark.read.json(my/path) // 200MB val exploded_network = social_network. withColumn("follower", explode($"followers")). withColumn("id_follower", ($"follower").cast("long")). withColumn("id_account", ($"account").cast("long")).

Get the result of a stored procedure to a dataframe or Rdd?

点点圈 提交于 2020-12-05 12:04:13
问题 How to create a data frame from the result of a stored proc? val jdbcDf = sqlContext.read.format("jdbc").options(Map( "driver" -> "com.microsoft.sqlserver.jdbc.SQLServerDriver", "url" -> jdbcSqlConn, "dbtable" -> "(exec aStoredProc) a" // Error )).load() 回答1: This is not logically possible since the stored procedure can return 0 or more result-sets. If the no of rows generated by the procedure is small the query can be executed in the driver application and the resultset can be converted into

Is there a type-class that checks for existence of at least one implicit of a type?

有些话、适合烂在心里 提交于 2020-12-05 11:48:25
问题 I have a trait Foo[T, U] and a type-level algorithm that given an L <: HList and a target type U , tells me whether there exists T in L such that there is an implicit Foo[T, U] in scope. This is implemented using the following type class: trait Search[L <: HList, U] object Search { def apply[L <: HList, U](implicit s: Search[L, U]): U = null ... } and we have the following: object Test { type L = Int :: String :: HNil implicit val foo: Foo[String, Boolean] = null Search[L, Boolean] //compiles

Pattern matching on a list in Scala

懵懂的女人 提交于 2020-12-04 15:58:45
问题 I'm a little confused regarding pattern matching on a list in Scala. For example. val simplelist: List[Char] = List('a', 'b', 'c', 'd') //> simplelist : List[Char] = List(a, b, c, d) def simple_fun(list: List[Char]) = list match { case (x:Char) :: (y:List[Char]) => println(x) case _ => Nil } //> simple_fun: (list: List[Char])Any simple_fun(simplelist) //> a //| res0: Any = () This currently prints only one line of output. Should it not run/pattern match on each element of the List ? EDIT: I

Pattern matching on a list in Scala

陌路散爱 提交于 2020-12-04 15:58:15
问题 I'm a little confused regarding pattern matching on a list in Scala. For example. val simplelist: List[Char] = List('a', 'b', 'c', 'd') //> simplelist : List[Char] = List(a, b, c, d) def simple_fun(list: List[Char]) = list match { case (x:Char) :: (y:List[Char]) => println(x) case _ => Nil } //> simple_fun: (list: List[Char])Any simple_fun(simplelist) //> a //| res0: Any = () This currently prints only one line of output. Should it not run/pattern match on each element of the List ? EDIT: I

Does Scala have syntax for 0- and 1-tuples?

时光毁灭记忆、已成空白 提交于 2020-12-04 14:51:05
问题 scala> val two = (1,2) two: (Int, Int) = (1,2) scala> val one = (1,) <console>:1: error: illegal start of simple expression val one = (1,) ^ scala> val zero = () zero: Unit = () Is this: val one = Tuple1(5) really the most concise way to write a singleton tuple literal in Scala? And does Unit work like an empty tuple? Does this inconsistency bother anyone else? 回答1: really the most concise way to write a singleton tuple literal in Scala? Yes. And does Unit work like an empty tuple? No, since

Does Scala have syntax for 0- and 1-tuples?

﹥>﹥吖頭↗ 提交于 2020-12-04 14:43:31
问题 scala> val two = (1,2) two: (Int, Int) = (1,2) scala> val one = (1,) <console>:1: error: illegal start of simple expression val one = (1,) ^ scala> val zero = () zero: Unit = () Is this: val one = Tuple1(5) really the most concise way to write a singleton tuple literal in Scala? And does Unit work like an empty tuple? Does this inconsistency bother anyone else? 回答1: really the most concise way to write a singleton tuple literal in Scala? Yes. And does Unit work like an empty tuple? No, since

Does Scala have syntax for 0- and 1-tuples?

流过昼夜 提交于 2020-12-04 14:42:45
问题 scala> val two = (1,2) two: (Int, Int) = (1,2) scala> val one = (1,) <console>:1: error: illegal start of simple expression val one = (1,) ^ scala> val zero = () zero: Unit = () Is this: val one = Tuple1(5) really the most concise way to write a singleton tuple literal in Scala? And does Unit work like an empty tuple? Does this inconsistency bother anyone else? 回答1: really the most concise way to write a singleton tuple literal in Scala? Yes. And does Unit work like an empty tuple? No, since

Does Scala have syntax for 0- and 1-tuples?

天大地大妈咪最大 提交于 2020-12-04 14:42:07
问题 scala> val two = (1,2) two: (Int, Int) = (1,2) scala> val one = (1,) <console>:1: error: illegal start of simple expression val one = (1,) ^ scala> val zero = () zero: Unit = () Is this: val one = Tuple1(5) really the most concise way to write a singleton tuple literal in Scala? And does Unit work like an empty tuple? Does this inconsistency bother anyone else? 回答1: really the most concise way to write a singleton tuple literal in Scala? Yes. And does Unit work like an empty tuple? No, since