SlideShare a Scribd company logo
1 of 11
Download to read offline
Type classes
Dmytro Mitin
https://stepik.org/course/Introduction-to-programming-
with-dependent-types-in-Scala-2294/
March 2017
Dmytro Mitin Type classes
Inheritance in Java (implementing interface)
public interface Comparable<A> {
boolean cmp(A a1, A a2);
}
public class MyClass implements Comparable<MyClass> {
private int x;
@Override
public boolean cmp(MyClass a1, MyClass a2) {
return a1.x < a2.x;
}
}
public class OtherClass implements Comparable<OtherClass> {
private List<Integer> xs;
@Override
public boolean cmp(OtherClass a1, OtherClass a2) {
return a1.xs.get(0) < a2.xs.get(0);
}
}
Dmytro Mitin Type classes
Inheritance in Scala (extending trait)
trait Comparable[A] {
def cmp(a1: A, a2: A): Boolean
}
class MyClass(val x: Int) extends Comparable[MyClass] {
override def cmp(a1: MyClass, a2: MyClass): Boolean =
a1.x < a2.x
}
class OtherClass(val xs: List[Int]) extends Comparable[OtherClass] {
override def cmp(a1: OtherClass, a2: OtherClass): Boolean =
a1.xs.head < a2.xs.head
}
Dmytro Mitin Type classes
Type classes in Haskell
class Comparable a where
cmp :: a -> a -> Bool
instance Comparable Int where
cmp x y = x < y
instance Comparable a => Comparable [a] where
cmp xs ys = cmp (head xs) (head ys)
cmp (1 :: Int) 2
cmp [1 :: Int, 2] [2, 0]
> True
Dmytro Mitin Type classes
Implicits in Scala
Implicit parameters
def f(x: Int)(implicit y: String): String = x.toString + y
implicit val s: String = "abc"
f(10)
> 10abc
Implicit objects
class MyClass
def f(x: Int)(implicit y: MyClass): Int = x + y.hashCode
// implicit val obj = new MyClass
implicit object obj extends MyClass
f(10)
> ... /* some number */
Dmytro Mitin Type classes
Implicits in Scala
Implicit conversions
implicit def f(x: Int): String = x.toString + "abc"
def g(s: String): List[Char] = s.toList
g(123)
> List(1, 2, 3, a, b, c)
Implicit classes
implicit class MyClass(x: Int) {
def myMethod: Int = x + 1
}
123.myMethod
> 124
Dmytro Mitin Type classes
Type classes in Scala
trait Comparable[A] {
def cmp(a1: A, a2: A): Boolean
}
implicit object intComparable extends Comparable[Int] {
override def cmp(a1: Int, a2: Int): Boolean = a1 < a2
}
implicit class ComparableInt(x: Int) {
def cmp(y: Int): Boolean =
implicitly[Comparable[Int]].cmp(x, y)
}
Dmytro Mitin Type classes
Type classes in Scala
implicit def listComparable[A: Comparable]: Comparable[List[A]] =
new Comparable[List[A]] {
override def cmp(a1: List[A], a2: List[A]): Boolean =
implicitly[Comparable[A]].cmp(a1.head, a2.head)
}
// implicit def listComparable[A](implicit elementComparable:
// Comparable[A]): Comparable[List[A]] =
// new Comparable[List[A]] {
// override def cmp(a1: List[A], a2: List[A]): Boolean =
// elementComparable.cmp(a1.head, a2.head)
// }
// implicit def listComparable[A: Comparable]: Comparable[List[A]] =
// (a1: List[A], a2: List[A]) =>
// implicitly[Comparable[A]].cmp(a1.head, a2.head)
Dmytro Mitin Type classes
Type classes in Scala
implicit class ComparableList[A: Comparable](list: List[A]) {
def cmp(other: List[A]): Boolean =
implicitly[Comparable[List[A]]].cmp(list, other)
}
implicitly[Comparable[Int]].cmp(1, 2)
1.cmp(2)
1 cmp 2
implicitly[Comparable[List[Int]]].cmp(List(1, 2), List(2, 0))
List(1, 2).cmp(List(2, 0))
> true
Dmytro Mitin Type classes
Simulacrum library
build.sbt
libraryDependencies += "com.github.mpilquist" %%
"simulacrum" % "0.10.0"
addCompilerPlugin("org.scalameta" % "paradise" %
"3.0.0-beta4" cross CrossVersion.full)
import simulacrum.
@typeclass trait Comparable[A] {
@op("<<<") def cmp(a1: A, a2: A): Boolean
}
implicit object intComparable extends Comparable[Int] {
override def cmp(a1: Int, a2: Int): Boolean = a1 < a2
}
Dmytro Mitin Type classes
Simulacrum library
implicit def listComparable[A: Comparable]: Comparable[List[A]] =
new Comparable[List[A]] {
override def cmp(a1: List[A], a2: List[A]): Boolean =
implicitly[Comparable[A]].cmp(a1.head, a2.head)
}
import Comparable.ops.
1 <<< 2
List(1, 2) <<< List(2, 0)
> true
Dmytro Mitin Type classes

More Related Content

What's hot

Intro to functional programming - Confoo
Intro to functional programming - ConfooIntro to functional programming - Confoo
Intro to functional programming - Confoofelixtrepanier
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class PatternsJohn De Goes
 
Type class survival guide
Type class survival guideType class survival guide
Type class survival guideMark Canlas
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With ScalaMeetu Maltiar
 
Introduction to idris
Introduction to idrisIntroduction to idris
Introduction to idrisConor Farrell
 
Introduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkIntroduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkAngelo Leto
 
Scalaz 8: A Whole New Game
Scalaz 8: A Whole New GameScalaz 8: A Whole New Game
Scalaz 8: A Whole New GameJohn De Goes
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala Knoldus Inc.
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java ProgrammersEric Pederson
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final TaglessJohn De Goes
 
Functional programming with_scala
Functional programming with_scalaFunctional programming with_scala
Functional programming with_scalaRaymond Tay
 
Type classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritanceType classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritanceAlexey Raga
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
 

What's hot (19)

Introducing scala
Introducing scalaIntroducing scala
Introducing scala
 
Intro to functional programming - Confoo
Intro to functional programming - ConfooIntro to functional programming - Confoo
Intro to functional programming - Confoo
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
 
Scala cheatsheet
Scala cheatsheetScala cheatsheet
Scala cheatsheet
 
Type class survival guide
Type class survival guideType class survival guide
Type class survival guide
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Introduction to idris
Introduction to idrisIntroduction to idris
Introduction to idris
 
Introduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkIntroduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with spark
 
Scalaz 8: A Whole New Game
Scalaz 8: A Whole New GameScalaz 8: A Whole New Game
Scalaz 8: A Whole New Game
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final Tagless
 
Functional programming with_scala
Functional programming with_scalaFunctional programming with_scala
Functional programming with_scala
 
SacalaZa #1
SacalaZa #1SacalaZa #1
SacalaZa #1
 
Type classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritanceType classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritance
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 

Similar to 08 - Scala. Type classes. Simulacrum

Type Classes in Scala and Haskell
Type Classes in Scala and HaskellType Classes in Scala and Haskell
Type Classes in Scala and HaskellHermann Hueck
 
05 - Scala. List type
05 - Scala. List type05 - Scala. List type
05 - Scala. List typeRoman Brovko
 
13 - Scala. Dependent pair type (Σ-type)
13 - Scala. Dependent pair type (Σ-type)13 - Scala. Dependent pair type (Σ-type)
13 - Scala. Dependent pair type (Σ-type)Roman Brovko
 
List-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic LanguagesList-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic LanguagesWim Vanderbauwhede
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed worldDebasish Ghosh
 
Practical scalaz
Practical scalazPractical scalaz
Practical scalazoxbow_lakes
 
Inheritance And Traits
Inheritance And TraitsInheritance And Traits
Inheritance And TraitsPiyush Mishra
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functionsNIKET CHAURASIA
 
Introduction to programming with dependent types in Scala
Introduction to programming with dependent types in ScalaIntroduction to programming with dependent types in Scala
Introduction to programming with dependent types in ScalaDmytro Mitin
 
Introduction to scala
Introduction to scalaIntroduction to scala
Introduction to scalaMichel Perez
 
TI1220 Lecture 8: Traits & Type Parameterization
TI1220 Lecture 8: Traits & Type ParameterizationTI1220 Lecture 8: Traits & Type Parameterization
TI1220 Lecture 8: Traits & Type ParameterizationEelco Visser
 
15 - Scala. Dependent function type (Π-type)
15 - Scala. Dependent function type (Π-type)15 - Scala. Dependent function type (Π-type)
15 - Scala. Dependent function type (Π-type)Roman Brovko
 
Legacy lambda code
Legacy lambda codeLegacy lambda code
Legacy lambda codePeter Lawrey
 

Similar to 08 - Scala. Type classes. Simulacrum (20)

Type Classes in Scala and Haskell
Type Classes in Scala and HaskellType Classes in Scala and Haskell
Type Classes in Scala and Haskell
 
05 - Scala. List type
05 - Scala. List type05 - Scala. List type
05 - Scala. List type
 
Sigma type
Sigma typeSigma type
Sigma type
 
13 - Scala. Dependent pair type (Σ-type)
13 - Scala. Dependent pair type (Σ-type)13 - Scala. Dependent pair type (Σ-type)
13 - Scala. Dependent pair type (Σ-type)
 
Cats in Scala
Cats in ScalaCats in Scala
Cats in Scala
 
List-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic LanguagesList-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic Languages
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed world
 
Practical scalaz
Practical scalazPractical scalaz
Practical scalaz
 
Practical cats
Practical catsPractical cats
Practical cats
 
Inheritance And Traits
Inheritance And TraitsInheritance And Traits
Inheritance And Traits
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functions
 
Introduction to programming with dependent types in Scala
Introduction to programming with dependent types in ScalaIntroduction to programming with dependent types in Scala
Introduction to programming with dependent types in Scala
 
Introduction to scala
Introduction to scalaIntroduction to scala
Introduction to scala
 
Data transformation-cheatsheet
Data transformation-cheatsheetData transformation-cheatsheet
Data transformation-cheatsheet
 
Introduction to-scala
Introduction to-scalaIntroduction to-scala
Introduction to-scala
 
TI1220 Lecture 8: Traits & Type Parameterization
TI1220 Lecture 8: Traits & Type ParameterizationTI1220 Lecture 8: Traits & Type Parameterization
TI1220 Lecture 8: Traits & Type Parameterization
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 
Built-in Classes in JAVA
Built-in Classes in JAVABuilt-in Classes in JAVA
Built-in Classes in JAVA
 
15 - Scala. Dependent function type (Π-type)
15 - Scala. Dependent function type (Π-type)15 - Scala. Dependent function type (Π-type)
15 - Scala. Dependent function type (Π-type)
 
Legacy lambda code
Legacy lambda codeLegacy lambda code
Legacy lambda code
 

More from Roman Brovko

Individual task Networking
Individual task NetworkingIndividual task Networking
Individual task NetworkingRoman Brovko
 
Networking essentials lect3
Networking essentials lect3Networking essentials lect3
Networking essentials lect3Roman Brovko
 
Gl embedded starterkit_ethernet
Gl embedded starterkit_ethernetGl embedded starterkit_ethernet
Gl embedded starterkit_ethernetRoman Brovko
 
Networking essentials lect2
Networking essentials lect2Networking essentials lect2
Networking essentials lect2Roman Brovko
 
Networking essentials lect1
Networking essentials lect1Networking essentials lect1
Networking essentials lect1Roman Brovko
 
Bare metal training_07_spi_flash
Bare metal training_07_spi_flashBare metal training_07_spi_flash
Bare metal training_07_spi_flashRoman Brovko
 
Bare metal training_06_I2C
Bare metal training_06_I2CBare metal training_06_I2C
Bare metal training_06_I2CRoman Brovko
 
Bare metal training_05_uart
Bare metal training_05_uartBare metal training_05_uart
Bare metal training_05_uartRoman Brovko
 
Bare metal training_04_adc_temp_sensor
Bare metal training_04_adc_temp_sensorBare metal training_04_adc_temp_sensor
Bare metal training_04_adc_temp_sensorRoman Brovko
 
Bare metal training_03_timers_pwm
Bare metal training_03_timers_pwmBare metal training_03_timers_pwm
Bare metal training_03_timers_pwmRoman Brovko
 
Bare metal training_02_le_ds_and_buttons
Bare metal training_02_le_ds_and_buttonsBare metal training_02_le_ds_and_buttons
Bare metal training_02_le_ds_and_buttonsRoman Brovko
 
Bare metal training_01_hello_world
Bare metal training_01_hello_worldBare metal training_01_hello_world
Bare metal training_01_hello_worldRoman Brovko
 
Bare metal training_00_prerequisites
Bare metal training_00_prerequisitesBare metal training_00_prerequisites
Bare metal training_00_prerequisitesRoman Brovko
 
C language lect_23_advanced
C language lect_23_advancedC language lect_23_advanced
C language lect_23_advancedRoman Brovko
 
C language lect_22_advanced
C language lect_22_advancedC language lect_22_advanced
C language lect_22_advancedRoman Brovko
 
C language lect_21_advanced
C language lect_21_advancedC language lect_21_advanced
C language lect_21_advancedRoman Brovko
 
подготовка рабочего окружения
подготовка рабочего окруженияподготовка рабочего окружения
подготовка рабочего окруженияRoman Brovko
 
C language lect_20_advanced
C language lect_20_advancedC language lect_20_advanced
C language lect_20_advancedRoman Brovko
 
C language lect_19_basics
C language lect_19_basicsC language lect_19_basics
C language lect_19_basicsRoman Brovko
 

More from Roman Brovko (20)

Individual task Networking
Individual task NetworkingIndividual task Networking
Individual task Networking
 
Networking essentials lect3
Networking essentials lect3Networking essentials lect3
Networking essentials lect3
 
Gl embedded starterkit_ethernet
Gl embedded starterkit_ethernetGl embedded starterkit_ethernet
Gl embedded starterkit_ethernet
 
Networking essentials lect2
Networking essentials lect2Networking essentials lect2
Networking essentials lect2
 
Networking essentials lect1
Networking essentials lect1Networking essentials lect1
Networking essentials lect1
 
Bare metal training_07_spi_flash
Bare metal training_07_spi_flashBare metal training_07_spi_flash
Bare metal training_07_spi_flash
 
Bare metal training_06_I2C
Bare metal training_06_I2CBare metal training_06_I2C
Bare metal training_06_I2C
 
Glesk worshop
Glesk worshopGlesk worshop
Glesk worshop
 
Bare metal training_05_uart
Bare metal training_05_uartBare metal training_05_uart
Bare metal training_05_uart
 
Bare metal training_04_adc_temp_sensor
Bare metal training_04_adc_temp_sensorBare metal training_04_adc_temp_sensor
Bare metal training_04_adc_temp_sensor
 
Bare metal training_03_timers_pwm
Bare metal training_03_timers_pwmBare metal training_03_timers_pwm
Bare metal training_03_timers_pwm
 
Bare metal training_02_le_ds_and_buttons
Bare metal training_02_le_ds_and_buttonsBare metal training_02_le_ds_and_buttons
Bare metal training_02_le_ds_and_buttons
 
Bare metal training_01_hello_world
Bare metal training_01_hello_worldBare metal training_01_hello_world
Bare metal training_01_hello_world
 
Bare metal training_00_prerequisites
Bare metal training_00_prerequisitesBare metal training_00_prerequisites
Bare metal training_00_prerequisites
 
C language lect_23_advanced
C language lect_23_advancedC language lect_23_advanced
C language lect_23_advanced
 
C language lect_22_advanced
C language lect_22_advancedC language lect_22_advanced
C language lect_22_advanced
 
C language lect_21_advanced
C language lect_21_advancedC language lect_21_advanced
C language lect_21_advanced
 
подготовка рабочего окружения
подготовка рабочего окруженияподготовка рабочего окружения
подготовка рабочего окружения
 
C language lect_20_advanced
C language lect_20_advancedC language lect_20_advanced
C language lect_20_advanced
 
C language lect_19_basics
C language lect_19_basicsC language lect_19_basics
C language lect_19_basics
 

Recently uploaded

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 

Recently uploaded (20)

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 

08 - Scala. Type classes. Simulacrum

  • 2. Inheritance in Java (implementing interface) public interface Comparable<A> { boolean cmp(A a1, A a2); } public class MyClass implements Comparable<MyClass> { private int x; @Override public boolean cmp(MyClass a1, MyClass a2) { return a1.x < a2.x; } } public class OtherClass implements Comparable<OtherClass> { private List<Integer> xs; @Override public boolean cmp(OtherClass a1, OtherClass a2) { return a1.xs.get(0) < a2.xs.get(0); } } Dmytro Mitin Type classes
  • 3. Inheritance in Scala (extending trait) trait Comparable[A] { def cmp(a1: A, a2: A): Boolean } class MyClass(val x: Int) extends Comparable[MyClass] { override def cmp(a1: MyClass, a2: MyClass): Boolean = a1.x < a2.x } class OtherClass(val xs: List[Int]) extends Comparable[OtherClass] { override def cmp(a1: OtherClass, a2: OtherClass): Boolean = a1.xs.head < a2.xs.head } Dmytro Mitin Type classes
  • 4. Type classes in Haskell class Comparable a where cmp :: a -> a -> Bool instance Comparable Int where cmp x y = x < y instance Comparable a => Comparable [a] where cmp xs ys = cmp (head xs) (head ys) cmp (1 :: Int) 2 cmp [1 :: Int, 2] [2, 0] > True Dmytro Mitin Type classes
  • 5. Implicits in Scala Implicit parameters def f(x: Int)(implicit y: String): String = x.toString + y implicit val s: String = "abc" f(10) > 10abc Implicit objects class MyClass def f(x: Int)(implicit y: MyClass): Int = x + y.hashCode // implicit val obj = new MyClass implicit object obj extends MyClass f(10) > ... /* some number */ Dmytro Mitin Type classes
  • 6. Implicits in Scala Implicit conversions implicit def f(x: Int): String = x.toString + "abc" def g(s: String): List[Char] = s.toList g(123) > List(1, 2, 3, a, b, c) Implicit classes implicit class MyClass(x: Int) { def myMethod: Int = x + 1 } 123.myMethod > 124 Dmytro Mitin Type classes
  • 7. Type classes in Scala trait Comparable[A] { def cmp(a1: A, a2: A): Boolean } implicit object intComparable extends Comparable[Int] { override def cmp(a1: Int, a2: Int): Boolean = a1 < a2 } implicit class ComparableInt(x: Int) { def cmp(y: Int): Boolean = implicitly[Comparable[Int]].cmp(x, y) } Dmytro Mitin Type classes
  • 8. Type classes in Scala implicit def listComparable[A: Comparable]: Comparable[List[A]] = new Comparable[List[A]] { override def cmp(a1: List[A], a2: List[A]): Boolean = implicitly[Comparable[A]].cmp(a1.head, a2.head) } // implicit def listComparable[A](implicit elementComparable: // Comparable[A]): Comparable[List[A]] = // new Comparable[List[A]] { // override def cmp(a1: List[A], a2: List[A]): Boolean = // elementComparable.cmp(a1.head, a2.head) // } // implicit def listComparable[A: Comparable]: Comparable[List[A]] = // (a1: List[A], a2: List[A]) => // implicitly[Comparable[A]].cmp(a1.head, a2.head) Dmytro Mitin Type classes
  • 9. Type classes in Scala implicit class ComparableList[A: Comparable](list: List[A]) { def cmp(other: List[A]): Boolean = implicitly[Comparable[List[A]]].cmp(list, other) } implicitly[Comparable[Int]].cmp(1, 2) 1.cmp(2) 1 cmp 2 implicitly[Comparable[List[Int]]].cmp(List(1, 2), List(2, 0)) List(1, 2).cmp(List(2, 0)) > true Dmytro Mitin Type classes
  • 10. Simulacrum library build.sbt libraryDependencies += "com.github.mpilquist" %% "simulacrum" % "0.10.0" addCompilerPlugin("org.scalameta" % "paradise" % "3.0.0-beta4" cross CrossVersion.full) import simulacrum. @typeclass trait Comparable[A] { @op("<<<") def cmp(a1: A, a2: A): Boolean } implicit object intComparable extends Comparable[Int] { override def cmp(a1: Int, a2: Int): Boolean = a1 < a2 } Dmytro Mitin Type classes
  • 11. Simulacrum library implicit def listComparable[A: Comparable]: Comparable[List[A]] = new Comparable[List[A]] { override def cmp(a1: List[A], a2: List[A]): Boolean = implicitly[Comparable[A]].cmp(a1.head, a2.head) } import Comparable.ops. 1 <<< 2 List(1, 2) <<< List(2, 0) > true Dmytro Mitin Type classes