SlideShare a Scribd company logo
Pa$ern 
Matching 
in 
Scala
First, 
an 
example 
• Users 
have 
accounts 
• Accounts 
are 
either 
Paypal 
or 
Bitcoin 
• Paypal 
accounts 
have 
an 
email 
• Bitcoin 
accounts 
have 
a 
key
case class User(name: String, account: Account) 
sealed class Account 
case class Paypal(email: String) extends Account 
case class Bitcoin(key: String) extends Account
Iden@fy 
account 
type 
private String whatis(Object obj) { 
if(obj instanceof User) { 
User user = (User) obj; 
if(user.account instanceof Paypal) { 
Paypal paypal = (Paypal) user.account; 
return "Paypal for email " + paypal.email; 
} else if(user.account instanceof Bitcoin) { 
Bitcoin bitcoin = (Bitcoin) user.account; 
return "Bitcoin with key " + bitcoin.key; 
} else { 
return "Unknown account type" 
} 
} else { 
return "Unknown object type"; 
} 
}
Iden@fy 
account 
type 
def whatis(obj: Any) { 
obj match { 
case User(name, Paypal(email)) => 
"Paypal for email " + email 
case User(name, Bitcoin(key)) => 
"Bitcoin with key " + key 
case _ => "Unknown object type" 
} 
}
Lots 
more 
power 
def whatis(obj: Any) { 
obj match { 
case User(name, Paypal("bilbo@shire.com")) 
if(!name.isEmpty) => 
"Bilbo's got Paypal" 
... 
} 
}
Pa$erns 
on 
val 
crea@on 
def getTuple: (User, Account) = ... 
// Decompose the tuple, no need for _1 and _2 
accessors 
val (user,account) = getTuple
Pattern Matching in Scala

More Related Content

Viewers also liked

Functional Programming - Worth the Effort
Functional Programming - Worth the EffortFunctional Programming - Worth the Effort
Functional Programming - Worth the Effort
BoldRadius Solutions
 
Curriculum
CurriculumCurriculum
Curriculumthipik
 
Paul Partlow Highlighted Portfolio
Paul Partlow Highlighted PortfolioPaul Partlow Highlighted Portfolio
Paul Partlow Highlighted Portfolio
Paul Partlow
 
Value Classes in Scala | BoldRadius
Value Classes in Scala | BoldRadiusValue Classes in Scala | BoldRadius
Value Classes in Scala | BoldRadius
BoldRadius Solutions
 
Scala: Collections API
Scala: Collections APIScala: Collections API
Scala: Collections API
BoldRadius Solutions
 
Empirics of standard deviation
Empirics of standard deviationEmpirics of standard deviation
Empirics of standard deviation
Adebanji Ayeni
 
Taste of korea
Taste of koreaTaste of korea
Taste of korea
Marta Garcia Trincado
 
Code Brevity in Scala
Code Brevity in ScalaCode Brevity in Scala
Code Brevity in Scala
BoldRadius Solutions
 
What Are For Expressions in Scala?
What Are For Expressions in Scala?What Are For Expressions in Scala?
What Are For Expressions in Scala?
BoldRadius Solutions
 
Scala Days Highlights | BoldRadius
Scala Days Highlights | BoldRadiusScala Days Highlights | BoldRadius
Scala Days Highlights | BoldRadius
BoldRadius Solutions
 
Punishment Driven Development #agileinthecity
Punishment Driven Development   #agileinthecityPunishment Driven Development   #agileinthecity
Punishment Driven Development #agileinthecity
Louise Elliott
 
Why Not Make the Transition from Java to Scala?
Why Not Make the Transition from Java to Scala?Why Not Make the Transition from Java to Scala?
Why Not Make the Transition from Java to Scala?
BoldRadius Solutions
 
Immutability in Scala
Immutability in ScalaImmutability in Scala
Immutability in Scala
BoldRadius Solutions
 
How You Convince Your Manager To Adopt Scala.js in Production
How You Convince Your Manager To Adopt Scala.js in ProductionHow You Convince Your Manager To Adopt Scala.js in Production
How You Convince Your Manager To Adopt Scala.js in Production
BoldRadius Solutions
 
scientific method and the research process
scientific method and the research processscientific method and the research process
scientific method and the research process
Adebanji Ayeni
 
GSLV MARK 3
GSLV MARK 3GSLV MARK 3
GSLV MARK 3
APOORVA
 
Domain Driven Design Through Onion Architecture
Domain Driven Design Through Onion ArchitectureDomain Driven Design Through Onion Architecture
Domain Driven Design Through Onion Architecture
BoldRadius Solutions
 
Haptens
HaptensHaptens

Viewers also liked (20)

Functional Programming - Worth the Effort
Functional Programming - Worth the EffortFunctional Programming - Worth the Effort
Functional Programming - Worth the Effort
 
Curriculum
CurriculumCurriculum
Curriculum
 
Test
TestTest
Test
 
Paul Partlow Highlighted Portfolio
Paul Partlow Highlighted PortfolioPaul Partlow Highlighted Portfolio
Paul Partlow Highlighted Portfolio
 
Value Classes in Scala | BoldRadius
Value Classes in Scala | BoldRadiusValue Classes in Scala | BoldRadius
Value Classes in Scala | BoldRadius
 
Scala: Collections API
Scala: Collections APIScala: Collections API
Scala: Collections API
 
Empirics of standard deviation
Empirics of standard deviationEmpirics of standard deviation
Empirics of standard deviation
 
Taste of korea
Taste of koreaTaste of korea
Taste of korea
 
Code Brevity in Scala
Code Brevity in ScalaCode Brevity in Scala
Code Brevity in Scala
 
What Are For Expressions in Scala?
What Are For Expressions in Scala?What Are For Expressions in Scala?
What Are For Expressions in Scala?
 
Earth moon 1
Earth moon 1Earth moon 1
Earth moon 1
 
Scala Days Highlights | BoldRadius
Scala Days Highlights | BoldRadiusScala Days Highlights | BoldRadius
Scala Days Highlights | BoldRadius
 
Punishment Driven Development #agileinthecity
Punishment Driven Development   #agileinthecityPunishment Driven Development   #agileinthecity
Punishment Driven Development #agileinthecity
 
Why Not Make the Transition from Java to Scala?
Why Not Make the Transition from Java to Scala?Why Not Make the Transition from Java to Scala?
Why Not Make the Transition from Java to Scala?
 
Immutability in Scala
Immutability in ScalaImmutability in Scala
Immutability in Scala
 
How You Convince Your Manager To Adopt Scala.js in Production
How You Convince Your Manager To Adopt Scala.js in ProductionHow You Convince Your Manager To Adopt Scala.js in Production
How You Convince Your Manager To Adopt Scala.js in Production
 
scientific method and the research process
scientific method and the research processscientific method and the research process
scientific method and the research process
 
GSLV MARK 3
GSLV MARK 3GSLV MARK 3
GSLV MARK 3
 
Domain Driven Design Through Onion Architecture
Domain Driven Design Through Onion ArchitectureDomain Driven Design Through Onion Architecture
Domain Driven Design Through Onion Architecture
 
Haptens
HaptensHaptens
Haptens
 

More from BoldRadius Solutions

Introduction to the Typesafe Reactive Platform
Introduction to the Typesafe Reactive PlatformIntroduction to the Typesafe Reactive Platform
Introduction to the Typesafe Reactive Platform
BoldRadius Solutions
 
Towards Reliable Lookups - Scala By The Bay
Towards Reliable Lookups - Scala By The BayTowards Reliable Lookups - Scala By The Bay
Towards Reliable Lookups - Scala By The Bay
BoldRadius Solutions
 
Introduction to the Actor Model
Introduction to the Actor ModelIntroduction to the Actor Model
Introduction to the Actor Model
BoldRadius Solutions
 
What are Sealed Classes in Scala?
What are Sealed Classes in Scala?What are Sealed Classes in Scala?
What are Sealed Classes in Scala?
BoldRadius Solutions
 
How To Use Higher Order Functions in Scala
How To Use Higher Order Functions in ScalaHow To Use Higher Order Functions in Scala
How To Use Higher Order Functions in Scala
BoldRadius Solutions
 
Scala Days 2014: Pitching Typesafe
Scala Days 2014: Pitching TypesafeScala Days 2014: Pitching Typesafe
Scala Days 2014: Pitching Typesafe
BoldRadius Solutions
 
Demonstrating Case Classes in Scala
Demonstrating Case Classes in ScalaDemonstrating Case Classes in Scala
Demonstrating Case Classes in Scala
BoldRadius Solutions
 

More from BoldRadius Solutions (7)

Introduction to the Typesafe Reactive Platform
Introduction to the Typesafe Reactive PlatformIntroduction to the Typesafe Reactive Platform
Introduction to the Typesafe Reactive Platform
 
Towards Reliable Lookups - Scala By The Bay
Towards Reliable Lookups - Scala By The BayTowards Reliable Lookups - Scala By The Bay
Towards Reliable Lookups - Scala By The Bay
 
Introduction to the Actor Model
Introduction to the Actor ModelIntroduction to the Actor Model
Introduction to the Actor Model
 
What are Sealed Classes in Scala?
What are Sealed Classes in Scala?What are Sealed Classes in Scala?
What are Sealed Classes in Scala?
 
How To Use Higher Order Functions in Scala
How To Use Higher Order Functions in ScalaHow To Use Higher Order Functions in Scala
How To Use Higher Order Functions in Scala
 
Scala Days 2014: Pitching Typesafe
Scala Days 2014: Pitching TypesafeScala Days 2014: Pitching Typesafe
Scala Days 2014: Pitching Typesafe
 
Demonstrating Case Classes in Scala
Demonstrating Case Classes in ScalaDemonstrating Case Classes in Scala
Demonstrating Case Classes in Scala
 

Pattern Matching in Scala

  • 2. First, an example • Users have accounts • Accounts are either Paypal or Bitcoin • Paypal accounts have an email • Bitcoin accounts have a key
  • 3. case class User(name: String, account: Account) sealed class Account case class Paypal(email: String) extends Account case class Bitcoin(key: String) extends Account
  • 4. Iden@fy account type private String whatis(Object obj) { if(obj instanceof User) { User user = (User) obj; if(user.account instanceof Paypal) { Paypal paypal = (Paypal) user.account; return "Paypal for email " + paypal.email; } else if(user.account instanceof Bitcoin) { Bitcoin bitcoin = (Bitcoin) user.account; return "Bitcoin with key " + bitcoin.key; } else { return "Unknown account type" } } else { return "Unknown object type"; } }
  • 5. Iden@fy account type def whatis(obj: Any) { obj match { case User(name, Paypal(email)) => "Paypal for email " + email case User(name, Bitcoin(key)) => "Bitcoin with key " + key case _ => "Unknown object type" } }
  • 6. Lots more power def whatis(obj: Any) { obj match { case User(name, Paypal("bilbo@shire.com")) if(!name.isEmpty) => "Bilbo's got Paypal" ... } }
  • 7. Pa$erns on val crea@on def getTuple: (User, Account) = ... // Decompose the tuple, no need for _1 and _2 accessors val (user,account) = getTuple