implicits

Workaround for importing spark implicits everywhere

ε祈祈猫儿з 提交于 2019-12-05 12:52:21
I'm new to Spark 2.0 and using datasets in our code base. I'm kinda noticing that I need to import spark.implicits._ everywhere in our code. For example: File A class A { def job(spark: SparkSession) = { import spark.implcits._ //create dataset ds val b = new B(spark) b.doSomething(ds) doSomething(ds) } private def doSomething(ds: Dataset[Foo], spark: SparkSession) = { import spark.implicits._ ds.map(e => 1) } } File B class B(spark: SparkSession) { def doSomething(ds: Dataset[Foo]) = { import spark.implicits._ ds.map(e => "SomeString") } } What I wanted to ask is if there's a cleaner way to

How to define type lambda properly?

落爺英雄遲暮 提交于 2019-12-04 23:32:46
I used =:= as example type lambda for purpose of making simple minimal example. =:= type take two arguments, I'd like to curry one at type level. I take naive implementation type Curry[G] = {type l[L] = L =:= G} but in practical uses it causes errors: type X = Int type Y = Int type CurryInt[T] = T =:= Int type Curry[G] = {type l[L] = L =:= G} type CurrStatic = {type l[L] = L =:= Int} object CurryObj {type l[L] = L =:= Int} trait Apply[P[_], T] implicit def liftApply[P[_], T](implicit ev : P[T]) = new Apply[P,T] {} implicitly[Apply[CurryInt, Y]] // ok implicitly[Apply[Curry[X]#l, Y]] // fails

Other programming languages that support implicits “a la Scala”

不羁的心 提交于 2019-12-03 22:36:33
Scala implicits are very powerfull. I'm curious if they are a new/unique feature of Scala, or the concept already existed in other programming languages. Thanks. EDIT : To clarify my question, yes, I'm talking about this concrete implementation. Having "implicit things" all around seemed strange at first, but having used it for a while and seeing how others use it, I'm impressed by how well it works. Looks like the inspiration was Haskell's type classes. At least one blog article claims that implicits have their origin in Haskell type classes ; the article refers to a 2006 paper by Martin

Scala view application puzzler

半世苍凉 提交于 2019-12-03 10:03:45
问题 Say we have the following two traits: trait Foo[A] { def howMany(xs: List[A]) = xs.size } trait Bar And an implicit conversion from the second to the first: implicit def bar2foo[A](bar: Bar) = new Foo[A] {} We create a Bar and a list of integers: val bar = new Bar {} val stuff = List(1, 2, 3) Now I'd expect the following to work: bar howMany stuff But it doesn't: scala> bar howMany stuff <console>:13: error: type mismatch; found : List[Int] required: List[A] bar howMany stuff ^ So we go to

Type classes in Scala

别说谁变了你拦得住时间么 提交于 2019-12-03 07:26:34
问题 Having a background in Haskell I am currently trying to get familiar with Scala. I encountered some problems trying to translate a small, extensible expression language from Haskell into Scala. The underlying issue of writing a data type that is extensible with both new data-variants and operations is commonly known as the expression problem. My original solution in Haskell uses type classes and instance declarations with constraints. The base of my expression is defined as follows: module

Scala dependency injection: alternatives to implicit parameters

旧时模样 提交于 2019-12-03 04:59:40
问题 Please pardon the length of this question. I often need to create some contextual information at one layer of my code, and consume that information elsewhere. I generally find myself using implicit parameters: def foo(params)(implicit cx: MyContextType) = ... implicit val context = makeContext() foo(params) This works, but requires the implicit parameter to be passed around a lot, polluting the method signatures of layer after layout of intervening functions, even if they don't care about it

Type classes in Scala

社会主义新天地 提交于 2019-12-02 22:19:42
Having a background in Haskell I am currently trying to get familiar with Scala. I encountered some problems trying to translate a small, extensible expression language from Haskell into Scala. The underlying issue of writing a data type that is extensible with both new data-variants and operations is commonly known as the expression problem . My original solution in Haskell uses type classes and instance declarations with constraints. The base of my expression is defined as follows: module Expr where class Expr e where eval :: e -> Integer data Lit = Lit Integer instance Expr Lit where eval

Scala dependency injection: alternatives to implicit parameters

[亡魂溺海] 提交于 2019-12-02 18:14:55
Please pardon the length of this question. I often need to create some contextual information at one layer of my code, and consume that information elsewhere. I generally find myself using implicit parameters: def foo(params)(implicit cx: MyContextType) = ... implicit val context = makeContext() foo(params) This works, but requires the implicit parameter to be passed around a lot, polluting the method signatures of layer after layout of intervening functions, even if they don't care about it themselves. def foo(params)(implicit cx: MyContextType) = ... bar() ... def bar(params)(implicit cx:

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 =:!=

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]