SlideShare une entreprise Scribd logo
1  sur  42
Télécharger pour lire hors ligne
Type classes 101
Classification beyond inheritance
Alexey Raga
String
Int
List[Boolean]
case class Name(value: String)
case class Age(value: Int)
case class Person(name: Name, age: Age)
case class Gang(leader: Person, members: List[Person])
Types classify data
String
Int
List[Boolean]
case class Name(value: String)
case class Age(value: Int)
case class Person(name: Name, age: Age)
case class Gang(leader: Person, members: List[Person])
Types classify data
public class Person
implements ISerialisable
{
public String name;
public String address;
...
}
public void saveToDisk(ISerialisable obj) { … }
Types classify data
Types classify data
public class Person
implements ISerialisable, IJsonSerialisable, IXmlSerialisable, IPrettyPrint
{
public String name;
public String address;
...
}
Expression problem
trait Expr
case class Lit(value: Int) extends Expr
case class Add(x: Expr, y: Expr) extends Expr
val expr = Add(Lit(15), Lit(6))
Expression problem
● Operation extension
add new operations: eval, prettyPrint, etc.
● Data extension
add new expressions: Mul, Pow, Neg, ec.
● Static type safety
no isInstanceOf / asInstanceOf
Expression problem
● Operation extension
add new operations: eval, prettyPrint, etc.
● Data extension
add new expressions: Mul, Pow, Neg, ec.
● Static type safety
no isInstanceOf / asInstanceOf
Expression problem
trait Expr {
def eval: Int
def print: String
}
case class Lit(value: Int) extends Expr {
def eval = ???
def print = ???
}
case class Add(x: Expr, y: Expr) extends Expr {
def eval = ???
def print = ???
}
Expression problem
trait Expr {
def eval: Int = this match {
case Lit => ???
case Add => ???
}
def print: String = this match {
case Lit => ???
case Add => ???
}
}
case class Lit(value: Int) extends Expr
case class Add(x: Expr, y: Expr) extends Expr
We can do better
Classification
Plants Animals
?
Classification
Fungi
Classification
Classification
Classification
Classifying types
trait Serialisable[A] {
def serialise(obj: A) : Array[Byte]
}
object PersonSerialisable extends Serialisable[Person] {
def serialise(obj: Person): Array[Byte] = ???
}
def saveToDisk[A](obj: A, ser: Serialisable[A]) = {
val data = ser.serialise(obj)
???
}
saveToDisk(Person("john", 99), PersonSerialisable)
Type classes classify types
trait Serialisable[A] {
def serialise(obj: A) : Array[Byte]
}
implicit object PersonSerialisable extends Serialisable[Person] {
def serialise(obj: Person): Array[Byte] = ???
}
def saveToDisk[A](obj: A)(implicit ser: Serialisable[A]) = {
val data = ser.serialise(obj)
???
}
saveToDisk(Person("john", 99))
Type classes classify types
// already defined in Scala
// def implicitly[T](implicit e: T) = e
def saveToDisk[A: Serialisable](obj: A) = {
val ser = implicitly[Serialisable[A]]
val data = ser.serialise(obj)
...
}
saveToDisk(Person("john", 99))
Testimony ;)
Just saying...
import serialisation.json._
//import serialisation.csv._
//import serialisation.xml._
def saveToDisk[A](obj: A)(implicit ser: Serialisable[A]) = {
val data = ser.serialise(obj)
???
}
saveToDisk(Person("john", 99))
Type classes in Scala
trait Ordering[T] {
def compare(x : T, y : T) : Int
def lteq(x : T, y : T) : Boolean = compare(x, y) <= 0
def gteq(x : T, y : T) : Boolean = compare(x, y) => 0
...
}
trait Numeric[T] extends Ordering[T] {
def plus(x : T, y : T) : T
def minus(x : T, y : T) : T
def negate(x : T) : T
...
}
Type classes in Scala
trait Ordering[T] {
def compare(x : T, y : T) : Int
def lteq(x : T, y : T) : Boolean = compare(x, y) <= 0
def gteq(x : T, y : T) : Boolean = compare(x, y) => 0
}
trait Numeric[T] extends Ordering[T] {
def plus(x : T, y : T) : T
def minus(x : T, y : T) : T
def negate(x : T) : T
}
trait TraversableOnce[+A] {
def sum[B >: A](implicit num : scala.Numeric[B]) : B = ???
def min[B >: A](implicit cmp : scala.Ordering[B]) : A = ???
def max[B >: A](implicit cmp : scala.Ordering[B]) : A = ???
}
Type classes in Scala
trait Ordering[T] {
def compare(x : T, y : T) : Int
def lteq(x : T, y : T) : Boolean = compare(x, y) <= 0
def gteq(x : T, y : T) : Boolean = compare(x, y) => 0
}
trait Numeric[T] extends Ordering[T] {
def plus(x : T, y : T) : T
def minus(x : T, y : T) : T
def negate(x : T) : T
}
trait TraversableOnce[+A] {
def sum[B >: A](implicit num : scala.Numeric[B]) : B = ???
def min[B >: A](implicit cmp : scala.Ordering[B]) : A = ???
def max[B >: A](implicit cmp : scala.Ordering[B]) : A = ???
}
val sum = List(1,2,3).sum
val min = List(1,2,3).min
Type classes in Scalaz
trait Equal[A] {
def equal(a1 : A, a2 : A) : Boolean
}
trait Show[A] {
def shows(a : A) : String
}
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
trait Semigroup[A] {
def append(a1 : A, a2 : => A) : A
}
trait Monoid[A] extends Semigroup[A] {
def zero : A
}
Deriving proofs
//tuple of Equals is also an Equal
implicit def tuple2Equal[A: Equal, B: Equal]: Equal[(A, B)] =
new Equal[(A, B)] {
def equal(a1: (A, B), a2: (A, B)) : Boolean =
a1._1 === a2._1 && a1._2 === a2._2
}
//tuple of Semigroups is also a Semigroup
implicit def tuple2Semigroup[A: Semigroup, B: Semigroup]: Semigroup[(A, B)] = {
new Semigroup[(A, B)] {
def append(p1: (A, B), p2: => (A, B)) =
((p1._1 |+| p2._1), (p1._2 |+| p2._2))
}
}
Expression problem
package ep
trait Expr
case class Lit(value: Int) extends Expr
case class Add[A <: Expr, B <: Expr](x: A, y: B) extends Expr
Expression problem
● Operation extension
add new operations: eval, prettyPrint, etc.
● Data extension
add new expressions: Mul, Pow, Neg, ec.
● Static type safety
no isInstanceOf / asInstanceOf
Expression problem
package ep.evaluate
import ep._
trait Eval[A <: Expr] {
def eval(expr: A) : Int
}
object Eval {
def evaluate[A <: Expr](expr: A)(implicit evA: Eval[A]) = evA.eval(expr)
implicit object LitEval extends Eval[Lit] { def eval(expr: Lit) = expr.value }
implicit def addEval[A <: Expr, B <: Expr](implicit evA: Eval[A], evB: Eval[B]) = {
new Eval[Add[A, B]] {
def eval(expr: Add[A, B]) = evA.eval(expr.x) + evB.eval(expr.y)
}
}
}
Expression problem
package ep
import evaluate._
Eval.evaluate( Add(Lit(15), Lit(6)) ) === 21
Expression problem
package ep.expressions
import ep._
import evaluate._
case class Mul[A <: Expr, B <: Expr](x: A, y: B) extends Expr
object Mul {
implicit def mulEval[A <: Expr, B <: Expr](implicit evA: Eval[A], evB: Eval[B]) = {
new Eval[Mul[A, B]] {
def eval(expr: Mul[A, B]) = evA.eval(expr.x) * evB.eval(expr.y)
}
}
}
Expression problem
import evaluate._
import expressions._
Eval.evaluate( Mul(Lit(2), Add(Lit(15), Lit(6))) ) === 42
Expression problem
package ep.operations
import ep._
import ep.expressions._
trait PPrint[A <: Expr] {
def print(expr: A) : String
}
object PPrint {
def prettyPrint[A <: Expr](expr: A)(implicit pa: PPrint[A]) = pa.print(expr)
implicit object LitPrint extends PPrint[Lit] {
def print(expr: Lit) = expr.value.toString
}
implicit def mulPrint[A <: Expr, B <: Expr](implicit pA: PPrint[A], pB: PPrint[B]) = {
new PPrint[Mul[A, B]] {
def print(expr: Mul[A, B]) = pA.print(expr.x) + " * " + pB.print(expr.y)
}
}
Expression problem
import expressions._
import operations._
import evaluate._
PPrint.prettyPrint( Mul(Lit(2), Add(Lit(15), Lit(6))) ) === "2 * (15 + 6) = 42"
Expression problem
● Operation extension
add new operations: eval, prettyPrint, etc.
● Data extension
add new expressions: Mul, Pow, Neg, ec.
● Static type safety
no isInstanceOf / asInstanceOf
● Add behaviours retroactively
No need to change existing data types
● Solution to the Expression problem
Operation and data extension with static type safety
● Different kinds of operations
“instance” (A => String), “factory” (String => A), etc.
What about us?
Isn't it enough?
No we're not in paradise
This is who we are
This is what we've got
No it's not our paradise

Contenu connexe

Tendances

JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersTikal Knowledge
 
学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキストOpt Technologies
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional ProgrammingLuka Jacobowitz
 
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...Philip Schwarz
 
λ | Lenses
λ | Lensesλ | Lenses
λ | LensesOpen-IT
 
Optics with monocle - Modeling the part and the whole
Optics with monocle - Modeling the part and the wholeOptics with monocle - Modeling the part and the whole
Optics with monocle - Modeling the part and the wholeIlan Godik
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoidsLuka Jacobowitz
 
Principled Error Handling with FP
Principled Error Handling with FPPrincipled Error Handling with FP
Principled Error Handling with FPLuka Jacobowitz
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala Knoldus Inc.
 
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...PROIDEA
 
The Ring programming language version 1.9 book - Part 41 of 210
The Ring programming language version 1.9 book - Part 41 of 210The Ring programming language version 1.9 book - Part 41 of 210
The Ring programming language version 1.9 book - Part 41 of 210Mahmoud Samir Fayed
 
Collections In Scala
Collections In ScalaCollections In Scala
Collections In ScalaKnoldus Inc.
 
Scala for ruby programmers
Scala for ruby programmersScala for ruby programmers
Scala for ruby programmerstymon Tobolski
 

Tendances (20)

JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
 
学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト
 
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional Programming
 
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
 
Beyond Scala Lens
Beyond Scala LensBeyond Scala Lens
Beyond Scala Lens
 
λ | Lenses
λ | Lensesλ | Lenses
λ | Lenses
 
Scalaz
ScalazScalaz
Scalaz
 
Hammurabi
HammurabiHammurabi
Hammurabi
 
Optics with monocle - Modeling the part and the whole
Optics with monocle - Modeling the part and the wholeOptics with monocle - Modeling the part and the whole
Optics with monocle - Modeling the part and the whole
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoids
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Scala collection
Scala collectionScala collection
Scala collection
 
Principled Error Handling with FP
Principled Error Handling with FPPrincipled Error Handling with FP
Principled Error Handling with FP
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
 
The Ring programming language version 1.9 book - Part 41 of 210
The Ring programming language version 1.9 book - Part 41 of 210The Ring programming language version 1.9 book - Part 41 of 210
The Ring programming language version 1.9 book - Part 41 of 210
 
Collections In Scala
Collections In ScalaCollections In Scala
Collections In Scala
 
Scala collections
Scala collectionsScala collections
Scala collections
 
Scala for ruby programmers
Scala for ruby programmersScala for ruby programmers
Scala for ruby programmers
 

En vedette

Type classification-ebook
Type classification-ebookType classification-ebook
Type classification-ebookXuan Le
 
Scientific table type classification in digital library (DocEng 2012)
Scientific table type classification in digital library (DocEng 2012)Scientific table type classification in digital library (DocEng 2012)
Scientific table type classification in digital library (DocEng 2012)Seongchan Kim
 
The shape-of-design
The shape-of-designThe shape-of-design
The shape-of-designM Pardo
 
Chs basic tools and equipment
Chs basic tools and equipmentChs basic tools and equipment
Chs basic tools and equipmentanjgulf
 

En vedette (7)

Pixel Perfect Precision Handbook (Graphic E-book)
Pixel Perfect Precision Handbook (Graphic E-book)Pixel Perfect Precision Handbook (Graphic E-book)
Pixel Perfect Precision Handbook (Graphic E-book)
 
Perfect pixel
Perfect pixelPerfect pixel
Perfect pixel
 
Type classification-ebook
Type classification-ebookType classification-ebook
Type classification-ebook
 
Scientific table type classification in digital library (DocEng 2012)
Scientific table type classification in digital library (DocEng 2012)Scientific table type classification in digital library (DocEng 2012)
Scientific table type classification in digital library (DocEng 2012)
 
The shape-of-design
The shape-of-designThe shape-of-design
The shape-of-design
 
The pirate book
The pirate bookThe pirate book
The pirate book
 
Chs basic tools and equipment
Chs basic tools and equipmentChs basic tools and equipment
Chs basic tools and equipment
 

Similaire à Type classes 101 - classification beyond inheritance

High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scaladjspiewak
 
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
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverseLuka Jacobowitz
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patternsleague
 
Scala-对Java的修正和超越
Scala-对Java的修正和超越Scala-对Java的修正和超越
Scala-对Java的修正和超越Caoyuan Deng
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class PatternsJohn De Goes
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kirill Rozov
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
 
Generic Functional Programming with Type Classes
Generic Functional Programming with Type ClassesGeneric Functional Programming with Type Classes
Generic Functional Programming with Type ClassesTapio Rautonen
 
Monadologie
MonadologieMonadologie
Monadologieleague
 

Similaire à Type classes 101 - classification beyond inheritance (20)

High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
 
Scala jargon cheatsheet
Scala jargon cheatsheetScala jargon cheatsheet
Scala jargon cheatsheet
 
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
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverse
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
 
Scala
ScalaScala
Scala
 
Scala-对Java的修正和超越
Scala-对Java的修正和超越Scala-对Java的修正和超越
Scala-对Java的修正和超越
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
Generic Functional Programming with Type Classes
Generic Functional Programming with Type ClassesGeneric Functional Programming with Type Classes
Generic Functional Programming with Type Classes
 
Monadologie
MonadologieMonadologie
Monadologie
 

Dernier

Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineeringssuserb3a23b
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 

Dernier (20)

Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineering
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 

Type classes 101 - classification beyond inheritance

  • 1. Type classes 101 Classification beyond inheritance Alexey Raga
  • 2. String Int List[Boolean] case class Name(value: String) case class Age(value: Int) case class Person(name: Name, age: Age) case class Gang(leader: Person, members: List[Person]) Types classify data
  • 3. String Int List[Boolean] case class Name(value: String) case class Age(value: Int) case class Person(name: Name, age: Age) case class Gang(leader: Person, members: List[Person]) Types classify data
  • 4. public class Person implements ISerialisable { public String name; public String address; ... } public void saveToDisk(ISerialisable obj) { … } Types classify data
  • 5. Types classify data public class Person implements ISerialisable, IJsonSerialisable, IXmlSerialisable, IPrettyPrint { public String name; public String address; ... }
  • 6. Expression problem trait Expr case class Lit(value: Int) extends Expr case class Add(x: Expr, y: Expr) extends Expr val expr = Add(Lit(15), Lit(6))
  • 7. Expression problem ● Operation extension add new operations: eval, prettyPrint, etc. ● Data extension add new expressions: Mul, Pow, Neg, ec. ● Static type safety no isInstanceOf / asInstanceOf
  • 8. Expression problem ● Operation extension add new operations: eval, prettyPrint, etc. ● Data extension add new expressions: Mul, Pow, Neg, ec. ● Static type safety no isInstanceOf / asInstanceOf
  • 9. Expression problem trait Expr { def eval: Int def print: String } case class Lit(value: Int) extends Expr { def eval = ??? def print = ??? } case class Add(x: Expr, y: Expr) extends Expr { def eval = ??? def print = ??? }
  • 10. Expression problem trait Expr { def eval: Int = this match { case Lit => ??? case Add => ??? } def print: String = this match { case Lit => ??? case Add => ??? } } case class Lit(value: Int) extends Expr case class Add(x: Expr, y: Expr) extends Expr
  • 11. We can do better
  • 17. Classifying types trait Serialisable[A] { def serialise(obj: A) : Array[Byte] } object PersonSerialisable extends Serialisable[Person] { def serialise(obj: Person): Array[Byte] = ??? } def saveToDisk[A](obj: A, ser: Serialisable[A]) = { val data = ser.serialise(obj) ??? } saveToDisk(Person("john", 99), PersonSerialisable)
  • 18. Type classes classify types trait Serialisable[A] { def serialise(obj: A) : Array[Byte] } implicit object PersonSerialisable extends Serialisable[Person] { def serialise(obj: Person): Array[Byte] = ??? } def saveToDisk[A](obj: A)(implicit ser: Serialisable[A]) = { val data = ser.serialise(obj) ??? } saveToDisk(Person("john", 99))
  • 19. Type classes classify types // already defined in Scala // def implicitly[T](implicit e: T) = e def saveToDisk[A: Serialisable](obj: A) = { val ser = implicitly[Serialisable[A]] val data = ser.serialise(obj) ... } saveToDisk(Person("john", 99))
  • 21.
  • 22.
  • 23. Just saying... import serialisation.json._ //import serialisation.csv._ //import serialisation.xml._ def saveToDisk[A](obj: A)(implicit ser: Serialisable[A]) = { val data = ser.serialise(obj) ??? } saveToDisk(Person("john", 99))
  • 24. Type classes in Scala trait Ordering[T] { def compare(x : T, y : T) : Int def lteq(x : T, y : T) : Boolean = compare(x, y) <= 0 def gteq(x : T, y : T) : Boolean = compare(x, y) => 0 ... } trait Numeric[T] extends Ordering[T] { def plus(x : T, y : T) : T def minus(x : T, y : T) : T def negate(x : T) : T ... }
  • 25. Type classes in Scala trait Ordering[T] { def compare(x : T, y : T) : Int def lteq(x : T, y : T) : Boolean = compare(x, y) <= 0 def gteq(x : T, y : T) : Boolean = compare(x, y) => 0 } trait Numeric[T] extends Ordering[T] { def plus(x : T, y : T) : T def minus(x : T, y : T) : T def negate(x : T) : T } trait TraversableOnce[+A] { def sum[B >: A](implicit num : scala.Numeric[B]) : B = ??? def min[B >: A](implicit cmp : scala.Ordering[B]) : A = ??? def max[B >: A](implicit cmp : scala.Ordering[B]) : A = ??? }
  • 26. Type classes in Scala trait Ordering[T] { def compare(x : T, y : T) : Int def lteq(x : T, y : T) : Boolean = compare(x, y) <= 0 def gteq(x : T, y : T) : Boolean = compare(x, y) => 0 } trait Numeric[T] extends Ordering[T] { def plus(x : T, y : T) : T def minus(x : T, y : T) : T def negate(x : T) : T } trait TraversableOnce[+A] { def sum[B >: A](implicit num : scala.Numeric[B]) : B = ??? def min[B >: A](implicit cmp : scala.Ordering[B]) : A = ??? def max[B >: A](implicit cmp : scala.Ordering[B]) : A = ??? } val sum = List(1,2,3).sum val min = List(1,2,3).min
  • 27. Type classes in Scalaz trait Equal[A] { def equal(a1 : A, a2 : A) : Boolean } trait Show[A] { def shows(a : A) : String } trait Functor[F[_]] { def map[A, B](fa: F[A])(f: A => B): F[B] } trait Semigroup[A] { def append(a1 : A, a2 : => A) : A } trait Monoid[A] extends Semigroup[A] { def zero : A }
  • 28. Deriving proofs //tuple of Equals is also an Equal implicit def tuple2Equal[A: Equal, B: Equal]: Equal[(A, B)] = new Equal[(A, B)] { def equal(a1: (A, B), a2: (A, B)) : Boolean = a1._1 === a2._1 && a1._2 === a2._2 } //tuple of Semigroups is also a Semigroup implicit def tuple2Semigroup[A: Semigroup, B: Semigroup]: Semigroup[(A, B)] = { new Semigroup[(A, B)] { def append(p1: (A, B), p2: => (A, B)) = ((p1._1 |+| p2._1), (p1._2 |+| p2._2)) } }
  • 29.
  • 30. Expression problem package ep trait Expr case class Lit(value: Int) extends Expr case class Add[A <: Expr, B <: Expr](x: A, y: B) extends Expr
  • 31. Expression problem ● Operation extension add new operations: eval, prettyPrint, etc. ● Data extension add new expressions: Mul, Pow, Neg, ec. ● Static type safety no isInstanceOf / asInstanceOf
  • 32. Expression problem package ep.evaluate import ep._ trait Eval[A <: Expr] { def eval(expr: A) : Int } object Eval { def evaluate[A <: Expr](expr: A)(implicit evA: Eval[A]) = evA.eval(expr) implicit object LitEval extends Eval[Lit] { def eval(expr: Lit) = expr.value } implicit def addEval[A <: Expr, B <: Expr](implicit evA: Eval[A], evB: Eval[B]) = { new Eval[Add[A, B]] { def eval(expr: Add[A, B]) = evA.eval(expr.x) + evB.eval(expr.y) } } }
  • 33. Expression problem package ep import evaluate._ Eval.evaluate( Add(Lit(15), Lit(6)) ) === 21
  • 34. Expression problem package ep.expressions import ep._ import evaluate._ case class Mul[A <: Expr, B <: Expr](x: A, y: B) extends Expr object Mul { implicit def mulEval[A <: Expr, B <: Expr](implicit evA: Eval[A], evB: Eval[B]) = { new Eval[Mul[A, B]] { def eval(expr: Mul[A, B]) = evA.eval(expr.x) * evB.eval(expr.y) } } }
  • 35. Expression problem import evaluate._ import expressions._ Eval.evaluate( Mul(Lit(2), Add(Lit(15), Lit(6))) ) === 42
  • 36. Expression problem package ep.operations import ep._ import ep.expressions._ trait PPrint[A <: Expr] { def print(expr: A) : String } object PPrint { def prettyPrint[A <: Expr](expr: A)(implicit pa: PPrint[A]) = pa.print(expr) implicit object LitPrint extends PPrint[Lit] { def print(expr: Lit) = expr.value.toString } implicit def mulPrint[A <: Expr, B <: Expr](implicit pA: PPrint[A], pB: PPrint[B]) = { new PPrint[Mul[A, B]] { def print(expr: Mul[A, B]) = pA.print(expr.x) + " * " + pB.print(expr.y) } }
  • 37. Expression problem import expressions._ import operations._ import evaluate._ PPrint.prettyPrint( Mul(Lit(2), Add(Lit(15), Lit(6))) ) === "2 * (15 + 6) = 42"
  • 38. Expression problem ● Operation extension add new operations: eval, prettyPrint, etc. ● Data extension add new expressions: Mul, Pow, Neg, ec. ● Static type safety no isInstanceOf / asInstanceOf
  • 39. ● Add behaviours retroactively No need to change existing data types ● Solution to the Expression problem Operation and data extension with static type safety ● Different kinds of operations “instance” (A => String), “factory” (String => A), etc.
  • 40.
  • 41.
  • 42. What about us? Isn't it enough? No we're not in paradise This is who we are This is what we've got No it's not our paradise