SlideShare ist ein Scribd-Unternehmen logo
1 von 51
Downloaden Sie, um offline zu lesen
Understanding Implicits in
Scala
https://github.com/shashankgowdal/understanding-implicits
● Shashank L
● Senior Software engineer at Tellius
● Big data consultant and trainer at
datamantra.io
● www.shashankgowda.com
Scala
Scala is a modern programming language designed to
express common programming patterns in a concise,
elegant, and type-safe way.
Scala is concise
● Do more with less code
● Type inference
● Higher level
Scala is concise
● Less boilerplate code
What are Implicits?
Implicits
● Language feature - not from external framework
● Implicits are next level of making Scala concise
● Scala Implicits allows to omit calling methods or
referencing variables explicitly
● Rely on the compiler to make the connections
● No runtime checks
○ Everything is type checked at compile time
● Enables advanced and elegant architecture design
Implicits are not
● Global variables
● Dynamically applied
● Dangerous
Implicit conversions in Java
static long sq(long a){
return a*a;
}
sq(10)
sq(10L)
● Java has implicit kind of type casting
● Only language features use them
● No public API to use it
Implicits
● Implicit parameters
● Implicit conversions
Implicit Parameters
Implicit parameters
● Function parameter annotated with the implicit keyword
def multiply (value:Int) (implicit by:Int) = value * by
● Parameter need not be passed explicitly
multiply(10)
● Tell the compiler once what needs to be passed
implicit val multiplier = 2
● Compilation error if implicit value not found
error: could not find implicit value for parameter by: Int
● Developer can be explicit
multiply(10)(3)
Implicit parameters
● Implicit can be a val, var or def
implicit def f: Int = if(monday) 2 else 4
● Can only use implicit once in the parameter list, all the parameters
following it will be implicit
def example1(implicit x:Int) //x is implicit
def example2(implicit x:Int, y:Int) //x & y is implicit
def example3(x:Int, implicit y:Int) //won't compile
def example4(x:Int)(implicit y:Int) //y is implicit
def example5(implicit x:Int)(y:Int) //won't compile
def example6(implicit x:Int)(implicit y:Int) //won't compile
Dependency injection
Dependency injection
● Separates creation of client’s dependencies from client’s
behaviour, allows program to be loosely coupled
● Similar to parameter passing. Dependencies are injected
rather than created inside
● This makes code
○ understandable
○ modular
○ reusable
○ testable
Dependency injection in Java
● Spring is the widely used framework in Java for DI
● Popular and well designed framework
● Relies on annotations for injection
● External configuration files is needed
● Dependency injection at runtime
○ Surprises at runtime if something isn’t right
● External library not a language feature
● Scala way to do DI
● Language feature - No need of framework
● User Authentication service with Persistence
● UserRepository - Handles storage
● UserService - Handles authentication and depends on UserRepository for
persistence
● ComponentRegistry - Wires components and instantiates services
com.shashank.implicits.dependency_injection.cakepattern
Dependency injection - Cake pattern
Service
Storage
InMemory MongoDB
Dependency injection - Cake pattern
● Pros
○ Compile time checked
○ No need of external configuration files
○ Switch different implementations
○ Create multiple environments
○ Test individual components
● Cons
○ Lot of boilerplate code
○ Cannot fast prototype (requires complete architecture picture)
○ Hard to get it right initially
Dependency injection - Implicits
● UserRepository dependency is passed as an implicit parameter
to UserService
● No need of boilerplate code with components, implementations
● Enables faster prototyping with advantages of Cake pattern
● Switch between different implementation of UserRepository by
changing the instantiation of it at once place
com.shashank.implicits.dependency_injection.implicitway
Context passing
● Similar to Dependency injection
● Keeping all dependencies in the form of context
● Common enough that simply "not having to pass it
everywhere" is itself a valuable goal
● Scala library uses it to pass around an ExecutionContext
to every code that needs to be run asynchronously.
Context passing
ExecutionContext in Future
object Future {
def apply[T](body: =>T)(implicit executor: ExecutionContext): scala.concurrent.Future[T] = {
val runnable = new PromiseCompletingRunnable(body)
executor.prepare.execute(runnable)
runnable.promise.future
}
}
Find first index of first occurrence of the word in a file
implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global
val firstOccurrence: Future[Int] = Future {
val source = scala.io.Source.fromFile("/very/big/file.txt")
source.toSeq.indexOfSlice("N628DL")
}
com.shashank.implicits.context_passing
Implicit Conversion
Implicits conversion
● Type conversions
● Extension methods
Type conversion
● Type conversion refers to changing an entity of one
datatype to another
● Type conversion can be
○ Explicit
Explicit conversion in some way is called casting
double doubleValue = 10 + (int)10.05;
○ Implicit
Implicit conversion is an automatic conversion by the
compiler
double doubleValue = 10 + 10.60;
Type conversion in Scala
● If an object’s type has to be converted from A to B
● Create an implicit conversion function
○ Takes parameter of type A
○ Implement conversion logic to convert to B and return
● Used extensively by Scala
● Conversion of Scala Boolean to Java Boolean object
implicit def boolean2Boolean(x: Boolean) = java.lang.Boolean.valueOf(x)
com.shashank.implicits.datatype_conversions
Extension methods
● As a library developer, providing custom methods to a class not
under your control is important
● Add special methods for specific type of your objects
● Spark has special methods for
○ PairRDD - reduceByKey, aggregateByKey
○ DoubleRDD - stats, mean, sum, histogram
● These methods are not in base RDD, It's added as extension
methods through implicits
● PairRDD methods are available only if the RDD type if
key-value pair
● DoubleRDD methods are available only if the RDD type is
Double
Extension methods
● If we want extend methods sum, mean for class A
● Create a custom class B with following properties
○ Single-parameter constructor expecting object of type A
○ Implement extension methods using object of type A
class JsonUser(user:User) {
def asJsObject: JsObject ={
JsObject(Map("userId" -> JsString(x.userId),
"email" -> JsString(x.email),
"isAdmin" -> JsBoolean(x.isAdmin)))
}
}
○ Define implicit type conversion to convert from A to B
implicit def toJsonUser(user:User) = new JsonUser(user)
com.shashank.implicits.extending_types
Implicit Class
● Syntactic sugar/shorthand for Extension methods
● Instead of creating a class and implicit def for conversion,
it can be combined together into a implicit class
implicit class JsonUser(user:User) {
def asJsObject: JsObject ={
JsObject(Map("userId" -> JsString(x.userId),
"email" -> JsString(x.email),
"isAdmin" -> JsBoolean(x.isAdmin)))
}
}
● Recommended to be in another trait/ class/ object
● Takes only one non-implicit parameter in constructor
com.shashank.implicits.implicit_class
Implicit Class
● Late trait implementation
implicit class UserOrdering(user: User) extends Ordered[User] {
override def compare(that: User): Int = user.userId.compare(that.userId)
}
com.shashank.implicits.implicit_class
Ad Hoc polymorphism
● Polymorphic function that can be applied to arguments of different
types
def convertToJson(x:Jsonable) :String = x.serialize
● Values passed to x should be of type Jsonable (type checks)
trait Jsonable[T]{
def serialize(t: T): Json
}
● Making String type Jsonable
class StringJsonable(t: String) extends Jsonable[String]{
def serialize() = s""$t""
}
● Calling convertToJson method
convertToJson(StringJsonable("spark_meetup"))
com.shashank.implicits.adhoc_polymorphism.adapter
Ad Hoc polymorphism
● Type system that supports ad hoc polymorphism
● Named after the language feature in Haskell
● Type class is used to provide evidence that a class String
satisfies an interface Jsonable.
def convertToJson[T](x: T)(implicit converter: Jsonable[T]): String = converter.serialize(x)
● Create an implicit converter from String type to Json String
implicit object StringJsonable(t: String) extends Jsonable[String]{
def serialize() = s""$t""
}
● Calling convertToJson method
convertToJson("spark_meetup")
com.shashank.implicits.adhoc_polymorphism.typeclasses
Type classes
● Provides combination of Bridge pattern and adapter pattern
● Third parties can also implement Jsonable support
implicit object DateJsonable(t: Date) extends Jsonable[String]{
private val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
def serialize() = s""${dateFormat.format(t)}""
}
● Calling convertToJson method
convertToJson(new Date())
com.shashank.implicits.adhoc_polymorphism.typeclasses_extension
Type classes
● Represented in the form [T: Bound]
● Plain type parameter T together with an implicit parameter of
type Bound[T]
● Syntactic sugar for Methods expecting TypeClass
def convertToJson[T : Jsonable](x: T): String = x.serialize()
● More useful and clean when need to just pass them to other
methods that use them.
com.shashank.implicits.implicitly
Context bound
● If Context bounds were used for Aesthetic purpose and there is
a need to access the implicit parameter
● implicitly with the implicit type can be used to get the implicit
parameter
def convertToJson[T : Jsonable](x: T): String = implicitly[Jsonable[T]].serialize()
com.shashank.implicits.implicitly
Implicitly
Behind the scenes
● Compiler tries to find Implicit when
○ method being called doesn’t exist on the object’s class
○ method being called takes an implicit parameter
● Implicits are resolved at 2 priority levels
○ Resolve scope - Higher priority
○ Extended scope - Lower priority
Implicit resolution
● Implicits defined in the current scope
implicit val n: Int = 5
def add(x: Int)(implicit y: Int) = x + y
add(5) // takes n from the current scope
● Explicit imports
import scala.collection.JavaConversions.mapAsScalaMap
def env = System.getenv() // Java map
val term = env("TERM") // implicit conversion from Java Map to Scala Map
● Wildcard imports
import scala.collection.JavaConversions._
val keySet = System.getenv().keySet() //returns Java Set collection
val sparkHomeSet = keySet.exists(key => key == "SPARK_HOME")
// implicit conversion from Java Set to Scala Set
Resolve scope
● Companion object of the source type
case class User(name:String, score:Double)
object User {
implicit def toJson(user:User):JsObject =
JsObject(Map("user" -> JsString(user.name), "score" -> JsNumber(user.score)))
}
val json:JsObject = User("myname", 30)
● Companion object of the expected type
case class User(name:String, score:Double)
case class JsonUser(json:String)
object JsonUser {
implicit def toJsonUser(user:User):JsonUser =
JsonUser(JsObject(Map("user" -> JsString(user.name), "score" -> JsNumber(user.score))).prettyPrint)
}
val jsonUser:JsonUser = User("myname", 30)
Extended scope
● Implicit Scope of Type arguments
case class User(name:String, score:Double)
object User {
implicit val ord = new Ordering[User] {
def compare(x: User, y: User): Int = implicitly[Ordering[Double]].compare(x.score, y.score)
}
}
val users = List(User("abc", 10), User("efg", 7), User("dcg", 9)).sorted
Extended scope
● Resolve scope implicits has precedence over Extended
scope Implicits
● Multiple implicits in the same scope level is resolved using
Static overloading rule
○ Take the implicit which has more specific type
● If all the implicits have the same precedence according to Static
overloading rule, then it results in a compilation error
com.shashank.implicits.resolution
Implicit conflicts
Implicit usage in frameworks
● Scala collection ordering
List(5, 3, 7).sorted
List("a", "x", "b").sorted
def sorted[B](implicit ord: Ordering[B])
def sorted[B](implicit ord: Ordering[B])
def max[U](implicit ord: Ordering[U])
● Scala collection numerical operations
List(5, 3, 7).sum
List(5, 3, 7).product
def product[B](implicit num: Numeric[B])
def sum[B](implicit num: Numeric[B])
Implicit usage in Frameworks
● Scala and Java collection inter compatibility
import collection.JavaConverters._
val jul: java.util.List[Int] = ArrayBuffer(1, 2, 3).asJava
jul.add(4)
val jul = new java.util.ArrayList[Int]()
jul.add(10); jul.add(20)
import scala.collection.JavaConversions._
jul.map(x => x.toDouble)
● Scala and Java data type inter compatibility
val booleanValue:java.lang.Boolean = false
implicit def boolean2Boolean(x: Boolean) = java.lang.Boolean.valueOf(x)
Implicit usage in Frameworks
● Spark Pair RDD functions
val rdd = sc.parallelize(List(("a", 10.0), ("c", 20.0), ("c", 20.0), ("d", 20.0))
rdd.groupByKey
rdd.reduceByKey
● Spark Double RDD functions
val rdd = sc.parallelize(List(1.0, 2.0, 3.0, 5.0, 20.0, 19.02, 19.29, 11.09, 21.0), 2)
rdd.sum
rdd.variance
● Spark Dataset API
import sparkSession.implicits._
val data = sparkSession.read.text("src/main/resources/data.txt").as[String]
def as[U : Encoder]: Dataset[U] = Dataset[U](sparkSession, logicalPlan)
implicit def newStringEncoder: Encoder[String] = Encoders.STRING
Implicit usage in Frameworks
● Spray JSON
Int JsNumber
String JsString
CaseClass JsObject
implicit object IntJsonFormat extends JsonFormat[Int] {
def write(x: Int) = JsNumber(x)
def read(value: JsValue) = value match {
case JsNumber(x) => x.intValue
}
}
def toJson(implicit format: JsonFormat[T]): JsValue = format.write(any)
val score = 100
score.toJson.prettyPrint
case class User(userId:String, email:String, isAdmin:Boolean)
implicit val userFormat = jsonFormat3(User)
user.toJson.prettyPrint
Implicit usage in Frameworks
● Do not overuse
● Avoid wildcard imports
import some.random.class.implicits._
● Don’t convert from one library type to another
● Avoid Implicit conversion which changes semantics
val jul:java.utilList = ...
import scala.collection.JavaConversions._
jul.map(a => a)
import scala.collection.JavaConverters._
jul.asScala.map(a => a)
● Use extended scope over resolve scope
What not to do?
● Resolution rules can be difficult
● Automatic conversions
● Slow compilation
● IDE slows down
● Do not overuse
Beware
● Under the hood of Implicits -
https://www.slideshare.net/DerekWyatt1/scala-implicits-not-to-be-feared
● Under the hood of Implicits -
https://www.youtube.com/watch?v=01zMZVf1ItU&t=12s
● What to leave Implicit by -
https://www.youtube.com/watch?v=Oij5V7LQJsA
● Implicit resolution -
https://stackoverflow.com/questions/5598085/where-does-scala-look-for-impli
cits
References
Thank you

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Templates
TemplatesTemplates
Templates
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Java interface
Java interfaceJava interface
Java interface
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Python functions
Python functionsPython functions
Python functions
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Function arguments In Python
Function arguments In PythonFunction arguments In Python
Function arguments In Python
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
python Function
python Function python Function
python Function
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
OOP java
OOP javaOOP java
OOP java
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
String in java
String in javaString in java
String in java
 

Ähnlich wie Understanding Implicits in Scala

Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scaladatamantra
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesEelco Visser
 
Sharable of qualities of clean code
Sharable of qualities of clean codeSharable of qualities of clean code
Sharable of qualities of clean codeEman Mohamed
 
Anatomy of spark catalyst
Anatomy of spark catalystAnatomy of spark catalyst
Anatomy of spark catalystdatamantra
 
The best of AltJava is Xtend
The best of AltJava is XtendThe best of AltJava is Xtend
The best of AltJava is Xtendtakezoe
 
Framework engineering JCO 2011
Framework engineering JCO 2011Framework engineering JCO 2011
Framework engineering JCO 2011YoungSu Son
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScriptJorg Janke
 
Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1Mukesh Kumar
 
Functional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 DevelopersFunctional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 DevelopersJayaram Sankaranarayanan
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptxVijalJain3
 
Structured web programming
Structured web programmingStructured web programming
Structured web programmingahfast
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developementfrwebhelp
 
Programming Languages: some news for the last N years
Programming Languages: some news for the last N yearsProgramming Languages: some news for the last N years
Programming Languages: some news for the last N yearsRuslan Shevchenko
 

Ähnlich wie Understanding Implicits in Scala (20)

Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific Languages
 
Sharable of qualities of clean code
Sharable of qualities of clean codeSharable of qualities of clean code
Sharable of qualities of clean code
 
Advanced PHP Simplified
Advanced PHP SimplifiedAdvanced PHP Simplified
Advanced PHP Simplified
 
Anatomy of spark catalyst
Anatomy of spark catalystAnatomy of spark catalyst
Anatomy of spark catalyst
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
The best of AltJava is Xtend
The best of AltJava is XtendThe best of AltJava is Xtend
The best of AltJava is Xtend
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
 
Framework engineering JCO 2011
Framework engineering JCO 2011Framework engineering JCO 2011
Framework engineering JCO 2011
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1
 
Functional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 DevelopersFunctional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 Developers
 
X++ 1.pptx
X++ 1.pptxX++ 1.pptx
X++ 1.pptx
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
Structured web programming
Structured web programmingStructured web programming
Structured web programming
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
 
Scala Days NYC 2016
Scala Days NYC 2016Scala Days NYC 2016
Scala Days NYC 2016
 
Programming Languages: some news for the last N years
Programming Languages: some news for the last N yearsProgramming Languages: some news for the last N years
Programming Languages: some news for the last N years
 

Mehr von datamantra

Multi Source Data Analysis using Spark and Tellius
Multi Source Data Analysis using Spark and TelliusMulti Source Data Analysis using Spark and Tellius
Multi Source Data Analysis using Spark and Telliusdatamantra
 
State management in Structured Streaming
State management in Structured StreamingState management in Structured Streaming
State management in Structured Streamingdatamantra
 
Spark on Kubernetes
Spark on KubernetesSpark on Kubernetes
Spark on Kubernetesdatamantra
 
Understanding transactional writes in datasource v2
Understanding transactional writes in  datasource v2Understanding transactional writes in  datasource v2
Understanding transactional writes in datasource v2datamantra
 
Introduction to Datasource V2 API
Introduction to Datasource V2 APIIntroduction to Datasource V2 API
Introduction to Datasource V2 APIdatamantra
 
Exploratory Data Analysis in Spark
Exploratory Data Analysis in SparkExploratory Data Analysis in Spark
Exploratory Data Analysis in Sparkdatamantra
 
Core Services behind Spark Job Execution
Core Services behind Spark Job ExecutionCore Services behind Spark Job Execution
Core Services behind Spark Job Executiondatamantra
 
Optimizing S3 Write-heavy Spark workloads
Optimizing S3 Write-heavy Spark workloadsOptimizing S3 Write-heavy Spark workloads
Optimizing S3 Write-heavy Spark workloadsdatamantra
 
Structured Streaming with Kafka
Structured Streaming with KafkaStructured Streaming with Kafka
Structured Streaming with Kafkadatamantra
 
Understanding time in structured streaming
Understanding time in structured streamingUnderstanding time in structured streaming
Understanding time in structured streamingdatamantra
 
Spark stack for Model life-cycle management
Spark stack for Model life-cycle managementSpark stack for Model life-cycle management
Spark stack for Model life-cycle managementdatamantra
 
Productionalizing Spark ML
Productionalizing Spark MLProductionalizing Spark ML
Productionalizing Spark MLdatamantra
 
Introduction to Structured streaming
Introduction to Structured streamingIntroduction to Structured streaming
Introduction to Structured streamingdatamantra
 
Building real time Data Pipeline using Spark Streaming
Building real time Data Pipeline using Spark StreamingBuilding real time Data Pipeline using Spark Streaming
Building real time Data Pipeline using Spark Streamingdatamantra
 
Testing Spark and Scala
Testing Spark and ScalaTesting Spark and Scala
Testing Spark and Scaladatamantra
 
Migrating to Spark 2.0 - Part 2
Migrating to Spark 2.0 - Part 2Migrating to Spark 2.0 - Part 2
Migrating to Spark 2.0 - Part 2datamantra
 
Migrating to spark 2.0
Migrating to spark 2.0Migrating to spark 2.0
Migrating to spark 2.0datamantra
 
Scalable Spark deployment using Kubernetes
Scalable Spark deployment using KubernetesScalable Spark deployment using Kubernetes
Scalable Spark deployment using Kubernetesdatamantra
 
Introduction to concurrent programming with akka actors
Introduction to concurrent programming with akka actorsIntroduction to concurrent programming with akka actors
Introduction to concurrent programming with akka actorsdatamantra
 
Interactive Data Analysis in Spark Streaming
Interactive Data Analysis in Spark StreamingInteractive Data Analysis in Spark Streaming
Interactive Data Analysis in Spark Streamingdatamantra
 

Mehr von datamantra (20)

Multi Source Data Analysis using Spark and Tellius
Multi Source Data Analysis using Spark and TelliusMulti Source Data Analysis using Spark and Tellius
Multi Source Data Analysis using Spark and Tellius
 
State management in Structured Streaming
State management in Structured StreamingState management in Structured Streaming
State management in Structured Streaming
 
Spark on Kubernetes
Spark on KubernetesSpark on Kubernetes
Spark on Kubernetes
 
Understanding transactional writes in datasource v2
Understanding transactional writes in  datasource v2Understanding transactional writes in  datasource v2
Understanding transactional writes in datasource v2
 
Introduction to Datasource V2 API
Introduction to Datasource V2 APIIntroduction to Datasource V2 API
Introduction to Datasource V2 API
 
Exploratory Data Analysis in Spark
Exploratory Data Analysis in SparkExploratory Data Analysis in Spark
Exploratory Data Analysis in Spark
 
Core Services behind Spark Job Execution
Core Services behind Spark Job ExecutionCore Services behind Spark Job Execution
Core Services behind Spark Job Execution
 
Optimizing S3 Write-heavy Spark workloads
Optimizing S3 Write-heavy Spark workloadsOptimizing S3 Write-heavy Spark workloads
Optimizing S3 Write-heavy Spark workloads
 
Structured Streaming with Kafka
Structured Streaming with KafkaStructured Streaming with Kafka
Structured Streaming with Kafka
 
Understanding time in structured streaming
Understanding time in structured streamingUnderstanding time in structured streaming
Understanding time in structured streaming
 
Spark stack for Model life-cycle management
Spark stack for Model life-cycle managementSpark stack for Model life-cycle management
Spark stack for Model life-cycle management
 
Productionalizing Spark ML
Productionalizing Spark MLProductionalizing Spark ML
Productionalizing Spark ML
 
Introduction to Structured streaming
Introduction to Structured streamingIntroduction to Structured streaming
Introduction to Structured streaming
 
Building real time Data Pipeline using Spark Streaming
Building real time Data Pipeline using Spark StreamingBuilding real time Data Pipeline using Spark Streaming
Building real time Data Pipeline using Spark Streaming
 
Testing Spark and Scala
Testing Spark and ScalaTesting Spark and Scala
Testing Spark and Scala
 
Migrating to Spark 2.0 - Part 2
Migrating to Spark 2.0 - Part 2Migrating to Spark 2.0 - Part 2
Migrating to Spark 2.0 - Part 2
 
Migrating to spark 2.0
Migrating to spark 2.0Migrating to spark 2.0
Migrating to spark 2.0
 
Scalable Spark deployment using Kubernetes
Scalable Spark deployment using KubernetesScalable Spark deployment using Kubernetes
Scalable Spark deployment using Kubernetes
 
Introduction to concurrent programming with akka actors
Introduction to concurrent programming with akka actorsIntroduction to concurrent programming with akka actors
Introduction to concurrent programming with akka actors
 
Interactive Data Analysis in Spark Streaming
Interactive Data Analysis in Spark StreamingInteractive Data Analysis in Spark Streaming
Interactive Data Analysis in Spark Streaming
 

Kürzlich hochgeladen

Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 

Kürzlich hochgeladen (20)

Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

Understanding Implicits in Scala

  • 2. ● Shashank L ● Senior Software engineer at Tellius ● Big data consultant and trainer at datamantra.io ● www.shashankgowda.com
  • 3. Scala Scala is a modern programming language designed to express common programming patterns in a concise, elegant, and type-safe way.
  • 4. Scala is concise ● Do more with less code ● Type inference ● Higher level
  • 5. Scala is concise ● Less boilerplate code
  • 7. Implicits ● Language feature - not from external framework ● Implicits are next level of making Scala concise ● Scala Implicits allows to omit calling methods or referencing variables explicitly ● Rely on the compiler to make the connections ● No runtime checks ○ Everything is type checked at compile time ● Enables advanced and elegant architecture design
  • 8. Implicits are not ● Global variables ● Dynamically applied ● Dangerous
  • 9. Implicit conversions in Java static long sq(long a){ return a*a; } sq(10) sq(10L) ● Java has implicit kind of type casting ● Only language features use them ● No public API to use it
  • 12. Implicit parameters ● Function parameter annotated with the implicit keyword def multiply (value:Int) (implicit by:Int) = value * by ● Parameter need not be passed explicitly multiply(10) ● Tell the compiler once what needs to be passed implicit val multiplier = 2 ● Compilation error if implicit value not found error: could not find implicit value for parameter by: Int ● Developer can be explicit multiply(10)(3)
  • 13. Implicit parameters ● Implicit can be a val, var or def implicit def f: Int = if(monday) 2 else 4 ● Can only use implicit once in the parameter list, all the parameters following it will be implicit def example1(implicit x:Int) //x is implicit def example2(implicit x:Int, y:Int) //x & y is implicit def example3(x:Int, implicit y:Int) //won't compile def example4(x:Int)(implicit y:Int) //y is implicit def example5(implicit x:Int)(y:Int) //won't compile def example6(implicit x:Int)(implicit y:Int) //won't compile
  • 15. Dependency injection ● Separates creation of client’s dependencies from client’s behaviour, allows program to be loosely coupled ● Similar to parameter passing. Dependencies are injected rather than created inside ● This makes code ○ understandable ○ modular ○ reusable ○ testable
  • 16. Dependency injection in Java ● Spring is the widely used framework in Java for DI ● Popular and well designed framework ● Relies on annotations for injection ● External configuration files is needed ● Dependency injection at runtime ○ Surprises at runtime if something isn’t right ● External library not a language feature
  • 17. ● Scala way to do DI ● Language feature - No need of framework ● User Authentication service with Persistence ● UserRepository - Handles storage ● UserService - Handles authentication and depends on UserRepository for persistence ● ComponentRegistry - Wires components and instantiates services com.shashank.implicits.dependency_injection.cakepattern Dependency injection - Cake pattern Service Storage InMemory MongoDB
  • 18. Dependency injection - Cake pattern ● Pros ○ Compile time checked ○ No need of external configuration files ○ Switch different implementations ○ Create multiple environments ○ Test individual components ● Cons ○ Lot of boilerplate code ○ Cannot fast prototype (requires complete architecture picture) ○ Hard to get it right initially
  • 19. Dependency injection - Implicits ● UserRepository dependency is passed as an implicit parameter to UserService ● No need of boilerplate code with components, implementations ● Enables faster prototyping with advantages of Cake pattern ● Switch between different implementation of UserRepository by changing the instantiation of it at once place com.shashank.implicits.dependency_injection.implicitway
  • 21. ● Similar to Dependency injection ● Keeping all dependencies in the form of context ● Common enough that simply "not having to pass it everywhere" is itself a valuable goal ● Scala library uses it to pass around an ExecutionContext to every code that needs to be run asynchronously. Context passing
  • 22. ExecutionContext in Future object Future { def apply[T](body: =>T)(implicit executor: ExecutionContext): scala.concurrent.Future[T] = { val runnable = new PromiseCompletingRunnable(body) executor.prepare.execute(runnable) runnable.promise.future } } Find first index of first occurrence of the word in a file implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global val firstOccurrence: Future[Int] = Future { val source = scala.io.Source.fromFile("/very/big/file.txt") source.toSeq.indexOfSlice("N628DL") } com.shashank.implicits.context_passing
  • 24. Implicits conversion ● Type conversions ● Extension methods
  • 25. Type conversion ● Type conversion refers to changing an entity of one datatype to another ● Type conversion can be ○ Explicit Explicit conversion in some way is called casting double doubleValue = 10 + (int)10.05; ○ Implicit Implicit conversion is an automatic conversion by the compiler double doubleValue = 10 + 10.60;
  • 26. Type conversion in Scala ● If an object’s type has to be converted from A to B ● Create an implicit conversion function ○ Takes parameter of type A ○ Implement conversion logic to convert to B and return ● Used extensively by Scala ● Conversion of Scala Boolean to Java Boolean object implicit def boolean2Boolean(x: Boolean) = java.lang.Boolean.valueOf(x) com.shashank.implicits.datatype_conversions
  • 27. Extension methods ● As a library developer, providing custom methods to a class not under your control is important ● Add special methods for specific type of your objects ● Spark has special methods for ○ PairRDD - reduceByKey, aggregateByKey ○ DoubleRDD - stats, mean, sum, histogram ● These methods are not in base RDD, It's added as extension methods through implicits ● PairRDD methods are available only if the RDD type if key-value pair ● DoubleRDD methods are available only if the RDD type is Double
  • 28. Extension methods ● If we want extend methods sum, mean for class A ● Create a custom class B with following properties ○ Single-parameter constructor expecting object of type A ○ Implement extension methods using object of type A class JsonUser(user:User) { def asJsObject: JsObject ={ JsObject(Map("userId" -> JsString(x.userId), "email" -> JsString(x.email), "isAdmin" -> JsBoolean(x.isAdmin))) } } ○ Define implicit type conversion to convert from A to B implicit def toJsonUser(user:User) = new JsonUser(user) com.shashank.implicits.extending_types
  • 29. Implicit Class ● Syntactic sugar/shorthand for Extension methods ● Instead of creating a class and implicit def for conversion, it can be combined together into a implicit class implicit class JsonUser(user:User) { def asJsObject: JsObject ={ JsObject(Map("userId" -> JsString(x.userId), "email" -> JsString(x.email), "isAdmin" -> JsBoolean(x.isAdmin))) } } ● Recommended to be in another trait/ class/ object ● Takes only one non-implicit parameter in constructor com.shashank.implicits.implicit_class
  • 30. Implicit Class ● Late trait implementation implicit class UserOrdering(user: User) extends Ordered[User] { override def compare(that: User): Int = user.userId.compare(that.userId) } com.shashank.implicits.implicit_class
  • 32. ● Polymorphic function that can be applied to arguments of different types def convertToJson(x:Jsonable) :String = x.serialize ● Values passed to x should be of type Jsonable (type checks) trait Jsonable[T]{ def serialize(t: T): Json } ● Making String type Jsonable class StringJsonable(t: String) extends Jsonable[String]{ def serialize() = s""$t"" } ● Calling convertToJson method convertToJson(StringJsonable("spark_meetup")) com.shashank.implicits.adhoc_polymorphism.adapter Ad Hoc polymorphism
  • 33. ● Type system that supports ad hoc polymorphism ● Named after the language feature in Haskell ● Type class is used to provide evidence that a class String satisfies an interface Jsonable. def convertToJson[T](x: T)(implicit converter: Jsonable[T]): String = converter.serialize(x) ● Create an implicit converter from String type to Json String implicit object StringJsonable(t: String) extends Jsonable[String]{ def serialize() = s""$t"" } ● Calling convertToJson method convertToJson("spark_meetup") com.shashank.implicits.adhoc_polymorphism.typeclasses Type classes
  • 34. ● Provides combination of Bridge pattern and adapter pattern ● Third parties can also implement Jsonable support implicit object DateJsonable(t: Date) extends Jsonable[String]{ private val dateFormat = new SimpleDateFormat("yyyy-MM-dd") def serialize() = s""${dateFormat.format(t)}"" } ● Calling convertToJson method convertToJson(new Date()) com.shashank.implicits.adhoc_polymorphism.typeclasses_extension Type classes
  • 35. ● Represented in the form [T: Bound] ● Plain type parameter T together with an implicit parameter of type Bound[T] ● Syntactic sugar for Methods expecting TypeClass def convertToJson[T : Jsonable](x: T): String = x.serialize() ● More useful and clean when need to just pass them to other methods that use them. com.shashank.implicits.implicitly Context bound
  • 36. ● If Context bounds were used for Aesthetic purpose and there is a need to access the implicit parameter ● implicitly with the implicit type can be used to get the implicit parameter def convertToJson[T : Jsonable](x: T): String = implicitly[Jsonable[T]].serialize() com.shashank.implicits.implicitly Implicitly
  • 38. ● Compiler tries to find Implicit when ○ method being called doesn’t exist on the object’s class ○ method being called takes an implicit parameter ● Implicits are resolved at 2 priority levels ○ Resolve scope - Higher priority ○ Extended scope - Lower priority Implicit resolution
  • 39. ● Implicits defined in the current scope implicit val n: Int = 5 def add(x: Int)(implicit y: Int) = x + y add(5) // takes n from the current scope ● Explicit imports import scala.collection.JavaConversions.mapAsScalaMap def env = System.getenv() // Java map val term = env("TERM") // implicit conversion from Java Map to Scala Map ● Wildcard imports import scala.collection.JavaConversions._ val keySet = System.getenv().keySet() //returns Java Set collection val sparkHomeSet = keySet.exists(key => key == "SPARK_HOME") // implicit conversion from Java Set to Scala Set Resolve scope
  • 40. ● Companion object of the source type case class User(name:String, score:Double) object User { implicit def toJson(user:User):JsObject = JsObject(Map("user" -> JsString(user.name), "score" -> JsNumber(user.score))) } val json:JsObject = User("myname", 30) ● Companion object of the expected type case class User(name:String, score:Double) case class JsonUser(json:String) object JsonUser { implicit def toJsonUser(user:User):JsonUser = JsonUser(JsObject(Map("user" -> JsString(user.name), "score" -> JsNumber(user.score))).prettyPrint) } val jsonUser:JsonUser = User("myname", 30) Extended scope
  • 41. ● Implicit Scope of Type arguments case class User(name:String, score:Double) object User { implicit val ord = new Ordering[User] { def compare(x: User, y: User): Int = implicitly[Ordering[Double]].compare(x.score, y.score) } } val users = List(User("abc", 10), User("efg", 7), User("dcg", 9)).sorted Extended scope
  • 42. ● Resolve scope implicits has precedence over Extended scope Implicits ● Multiple implicits in the same scope level is resolved using Static overloading rule ○ Take the implicit which has more specific type ● If all the implicits have the same precedence according to Static overloading rule, then it results in a compilation error com.shashank.implicits.resolution Implicit conflicts
  • 43. Implicit usage in frameworks
  • 44. ● Scala collection ordering List(5, 3, 7).sorted List("a", "x", "b").sorted def sorted[B](implicit ord: Ordering[B]) def sorted[B](implicit ord: Ordering[B]) def max[U](implicit ord: Ordering[U]) ● Scala collection numerical operations List(5, 3, 7).sum List(5, 3, 7).product def product[B](implicit num: Numeric[B]) def sum[B](implicit num: Numeric[B]) Implicit usage in Frameworks
  • 45. ● Scala and Java collection inter compatibility import collection.JavaConverters._ val jul: java.util.List[Int] = ArrayBuffer(1, 2, 3).asJava jul.add(4) val jul = new java.util.ArrayList[Int]() jul.add(10); jul.add(20) import scala.collection.JavaConversions._ jul.map(x => x.toDouble) ● Scala and Java data type inter compatibility val booleanValue:java.lang.Boolean = false implicit def boolean2Boolean(x: Boolean) = java.lang.Boolean.valueOf(x) Implicit usage in Frameworks
  • 46. ● Spark Pair RDD functions val rdd = sc.parallelize(List(("a", 10.0), ("c", 20.0), ("c", 20.0), ("d", 20.0)) rdd.groupByKey rdd.reduceByKey ● Spark Double RDD functions val rdd = sc.parallelize(List(1.0, 2.0, 3.0, 5.0, 20.0, 19.02, 19.29, 11.09, 21.0), 2) rdd.sum rdd.variance ● Spark Dataset API import sparkSession.implicits._ val data = sparkSession.read.text("src/main/resources/data.txt").as[String] def as[U : Encoder]: Dataset[U] = Dataset[U](sparkSession, logicalPlan) implicit def newStringEncoder: Encoder[String] = Encoders.STRING Implicit usage in Frameworks
  • 47. ● Spray JSON Int JsNumber String JsString CaseClass JsObject implicit object IntJsonFormat extends JsonFormat[Int] { def write(x: Int) = JsNumber(x) def read(value: JsValue) = value match { case JsNumber(x) => x.intValue } } def toJson(implicit format: JsonFormat[T]): JsValue = format.write(any) val score = 100 score.toJson.prettyPrint case class User(userId:String, email:String, isAdmin:Boolean) implicit val userFormat = jsonFormat3(User) user.toJson.prettyPrint Implicit usage in Frameworks
  • 48. ● Do not overuse ● Avoid wildcard imports import some.random.class.implicits._ ● Don’t convert from one library type to another ● Avoid Implicit conversion which changes semantics val jul:java.utilList = ... import scala.collection.JavaConversions._ jul.map(a => a) import scala.collection.JavaConverters._ jul.asScala.map(a => a) ● Use extended scope over resolve scope What not to do?
  • 49. ● Resolution rules can be difficult ● Automatic conversions ● Slow compilation ● IDE slows down ● Do not overuse Beware
  • 50. ● Under the hood of Implicits - https://www.slideshare.net/DerekWyatt1/scala-implicits-not-to-be-feared ● Under the hood of Implicits - https://www.youtube.com/watch?v=01zMZVf1ItU&t=12s ● What to leave Implicit by - https://www.youtube.com/watch?v=Oij5V7LQJsA ● Implicit resolution - https://stackoverflow.com/questions/5598085/where-does-scala-look-for-impli cits References