SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
intro match {
!case PatternMatching()
!!!=> “Welcome”
}
Friday, September 6, 13
Who am I?
•Java & Scala developer at Schantz A/S
•Polyglot curious, Coursera junkie
•Interested in HCI and Usability
•https://github.com/JKrag
@jankrag
• Geek, builder and flyer of kites, reptile & cat breeder, Rubik's puzzle fan
Friday, September 6, 13
Pattern Matching
• A very powerful feature of Scala
• Java’s “switch” on steroids?
Friday, September 6, 13
Java’s switch
• lets you match a ‘value’ agains a number of
cases, and conditionally executes code
• basically only switch on numeric values
• int, byte, etc....
• (Yes, Java 7 has switch on String, but only
syntactic sugar.)
• Performance > nested if’s
Friday, September 6, 13
Scala’s ‘match’
• Lets you match a ‘value’ agains complex
patterns
• Can switch on multiple types
• Can match most kind of types by matching
agains the “creation form” of an object
(patience...)
Friday, September 6, 13
Scala’s match (cont.)
• Each “case” is a Scala expression, and thus
Each “match” block is an expression
• Can be used as a full function body...
Friday, September 6, 13
History
• Pattern matching is nothing new
• Has existed way back in functional
languages
• Most notable early example: ML
• Also found in Haskell, Erlang, OCaml etc.
Friday, September 6, 13
Lets get on with it...
Friday, September 6, 13
Syntax - simple “java like”
def weather(code: Int): String = {
code match {
case 1 => "sun"
case 0 => "rain"
case _ => "error"
}
}
“_” is used as wildcard for the “default” case, when we don’t
need the matched value...
Friday, September 6, 13
Syntax
def weather(code: Int) = code match {
case 1 => "sun"
case 0 => "rain"
case _ => "error"
}
match used directly as full function body
Friday, September 6, 13
multiple matches
def myPlans(weekday: Int) = weekday match {
! case 1 | 2 | 3 | 4 | 5 => "work"
! case 6 | 7 => "relax"
! case _ => "unknown weekday"
}
Friday, September 6, 13
Matching literals
• A literal pattern L matches any value that is
equal (in terms of ==) to the literal L.
Friday, September 6, 13
matching strings
def parseArgument(arg: String) = arg match {
case "-h" | "--help" => displayHelp
case "-v" | "--version" => displayVerion
case whatever => unknownArgument(whatever)
}
Friday, September 6, 13
Mixed stuff
def handle(msg: Any) = msg match {
! "QUIT" => "recieved stop request"
! 42 => "The answer to the ultimate question"
! _ => "something else"
}
Friday, September 6, 13
matching tuples
def moveTo(coord: Any) = coord match {
! case (_, _) => println("received good 2D coord.")
! case (_, _ , _) => println("3D not supported")
! case _ => println("unexpected stuff")
}
Friday, September 6, 13
matching on types
• In java, if you need to “detect” types, you
typically use “instance of” and typecast
• In scala, we can match on types, using
variables, and these know the type
Friday, September 6, 13
types and variables
def handle(msg: Any) = msg match {
case i:Int => "Int'eresting: " + i
case _:Double => "Doubly so!"
case s:String => "You really want't me to do " + s
}
• introduced variables (typed of course)
• order can be important. Cases checked in
order
Friday, September 6, 13
matching tuples -
revisited
def moveTo(coord: Any) = coord match {
! case (a: Int, b: Int) => updateCoordinate(a, b)
! case (_, _ , _) => println("3D not supported")
! case _ => println("unexpected coordinate")
}
Friday, September 6, 13
variables
• In general, everything in lowercase is
treated as a “variable”
• constants should start with uppercase
Friday, September 6, 13
case matters
class Sample {
! val max = 100
! val MIN = 0
! def process(input: Int) {
! ! input match {
! ! ! case max => println("aaargh.")
! ! ! case MIN => println("matched MIN")
! ! ! case _ => println("unreachable")
! ! }
! }
}
What happens?
Friday, September 6, 13
case matters
class Sample {
! val max = 100
! val MIN = 0
! def process(input: Int) {
! ! input match {
! ! ! case max => println("aaargh.")
! ! ! case MIN => println("matched MIN")
! ! ! case _ => println("unreachable")
! ! }
! }
}
What happens?
Compile error
Friday, September 6, 13
case matters
class Sample {
! val max = 100
! val MIN = 0
! def process(input: Int) {
! ! input match {
! ! ! case max => println("aaargh.")
! ! ! case MIN => println("matched MIN")
! ! ! case _ => println("unreachable")
! ! }
! }
}
What happens?
Compile error
unreachable code
Friday, September 6, 13
case matters
class Sample {
! val max = 100
! val MIN = 0
! def process(input: Int) {
! ! input match {
! ! ! case max => println("aaargh.")
! ! ! case MIN => println("matched MIN")
! ! ! case _ => println("unreachable")
! ! }
! }
}
What happens?
Compile error
unreachable code
fix with: this.max
Friday, September 6, 13
Practical example:
Recursive factorial
• Without pattern matching:
def fact(n: Int): Int =
if (n == 0) 1
else n * fact(n - 1)
• With pattern matching:
def fact(n: Int): Int = n match {
case 0 => 1
case n => n * fact(n - 1)
}
• Note: n matches “everything else”
• Note: scope
Friday, September 6, 13
Matching List
• List("Apple", "Microsoft")
• List("Scala", "Clojure", "Groovy", _*)
• "array explosion symbol"
Friday, September 6, 13
look-alike
Friday, September 6, 13
look-alike
list match {
! ! case Nil => "was an empty list"
!! case x :: xs =>
"head was " + x + ", tail was " + xs
}
//remember a list in scala is either Nil, or “something
and a tail”
Friday, September 6, 13
look-alike
def length[A](list : List[A]) : Int = list match {
case _ :: tail => 1 + length(tail)
case Nil => 0
}
list match {
! ! case Nil => "was an empty list"
!! case x :: xs =>
"head was " + x + ", tail was " + xs
}
//remember a list in scala is either Nil, or “something
and a tail”
Friday, September 6, 13
Guards
• Powerful addition.
• Allows for “conditional” matching
Friday, September 6, 13
Guards - example
case msg : Int if (msg < 0) =>
printf("Execution problem. Return code: %d")
case msg : Int if (msg > 0) =>
printf("Succes. Return code: %d")
case _ : Int => printf("Boring")
Friday, September 6, 13
More advanced topics?
Quick breeze through
Friday, September 6, 13
Nested patterns
• case List(Cat(name), Owner(first, last))
Friday, September 6, 13
Nested - example
object Role extends Enumeration {
! type Role = Value
! val DEV, PM, CEO = Value
}
case class Name(first:String, last:String)
case class Person(name:Name, age:Int, role:Role)
val person = Person(Name("Jan", "Krag"), 42, Role.DEV)
person match {
! case Person(Name(first, last), age:Int, r: Role) =>
first + " " + last + " (aged " + age + ")"
! case _ => "something else"
}
//> res4: java.lang.String = "Jan Krag (aged 42) "
Friday, September 6, 13
Pattern binders
• We can assign a binder to part of a pattern
during a match using the @ notation.
• Often usefull when we want a larger part
of a pattern to use on the expression side
• e.g. case pers @ Person(“Dude”, _, _) =>
println(pers)
• binds pers to the whole Person object
Friday, September 6, 13
Regular expressions
val Name = """(w+)s+(w+)""".r
"Jan Krag" match {
case Name(first,last) => println("found: ", first, last)
case _ => println("oh no!")
}
(found: ,Jan,Krag)
Friday, September 6, 13
case classes
• Very common use case
• If case class is sealed abstract, compiler can
verify that match is exhaustive
• works because case classes have an auto-
generated unapply method.
Friday, September 6, 13
exhaustive match
sealed abstract class Expr
case class Var(name: String) extends Expr
case class Number(num: Double) extends Expr
case class UnOp(operator: String, arg: Expr) extends Expr
case class BinOp(operator: String, left: Expr, right: Expr)
extends Expr
def describe(e: Expr): String = e match {
case Number(_) => "a number"
case Var(_) => "a variable"
}
//warning: match is not exhaustive!
//missing combination UnOp
//missing combination BinOp
Friday, September 6, 13
Matching XML fragments
• As Scala has first class support for XML, we
can also match XML fragments:
case <price>{itemPrice}</price> =>
println("price was: " + itemPrice)
Friday, September 6, 13
Stable identifiers
def f(x: Int, y: Int) = x match {
case y => ...
}
def f(x: Int, y: Int) = x match {
case `y` => ...
}
Friday, September 6, 13
anomymous functions
• case classes can also be used as anonymous
functions, i..e. without a “match” clause:
• { case p1 => b1 ... case pn => bn }
Friday, September 6, 13
in Exception handling
try {
throw new j.i.IOException("no such file")
} catch {
case e @ (_ : RuntimeException | _ : j.i.IOException)
=> println(e)
}
// prints out "java.io.IOException: no such file"
Example also demonstrates binding of “alternatives”
expression
Friday, September 6, 13
Deconstructing
BillVenners: You said a pattern looks like an expression, but it
seems kind of like a backwards expression. Instead of
plugging values in and getting one result out, you put in one
value, and when it matches, a bunch of values pop back out.
Martin Odersky: Yes. It's really the exact reversal of
construction. I can construct objects with nested
constructors, and maybe I also have some parameters. Let's
say I have a method that takes some parameters and
constructs some complicated object structure from those
parameters. Pattern matching does the reverse. It takes a
complicated object structure and pulls out the parameters
that were used to construct the same structure.
Friday, September 6, 13
apply(...) / unapply(...)
• And this is a whole new (albeit important)
subject best left for next time...
Friday, September 6, 13

Weitere ähnliche Inhalte

Was ist angesagt?

Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Sanjeev_Knoldus
 
Operator Overloading In Scala
Operator Overloading In ScalaOperator Overloading In Scala
Operator Overloading In ScalaJoey Gibson
 
Descriptor Protocol
Descriptor ProtocolDescriptor Protocol
Descriptor Protocolrocketcircus
 
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...Andrew Phillips
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Bryan O'Sullivan
 
Object Equality in Scala
Object Equality in ScalaObject Equality in Scala
Object Equality in ScalaKnoldus Inc.
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Bryan O'Sullivan
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Bryan O'Sullivan
 
Understanding JavaScript
Understanding JavaScriptUnderstanding JavaScript
Understanding JavaScriptnodejsbcn
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummiesknutmork
 
Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!Kel Cecil
 
Real World Haskell: Lecture 4
Real World Haskell: Lecture 4Real World Haskell: Lecture 4
Real World Haskell: Lecture 4Bryan O'Sullivan
 

Was ist angesagt? (19)

Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
 
Operator Overloading In Scala
Operator Overloading In ScalaOperator Overloading In Scala
Operator Overloading In Scala
 
Why Haskell
Why HaskellWhy Haskell
Why Haskell
 
Descriptor Protocol
Descriptor ProtocolDescriptor Protocol
Descriptor Protocol
 
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
 
Scala vs java 8
Scala vs java 8Scala vs java 8
Scala vs java 8
 
Grammarware Memes
Grammarware MemesGrammarware Memes
Grammarware Memes
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Object Equality in Scala
Object Equality in ScalaObject Equality in Scala
Object Equality in Scala
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2
 
Understanding JavaScript
Understanding JavaScriptUnderstanding JavaScript
Understanding JavaScript
 
Basic swift
Basic swiftBasic swift
Basic swift
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummies
 
Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!
 
Short intro to ECMAScript
Short intro to ECMAScriptShort intro to ECMAScript
Short intro to ECMAScript
 
Real World Haskell: Lecture 4
Real World Haskell: Lecture 4Real World Haskell: Lecture 4
Real World Haskell: Lecture 4
 

Andere mochten auch

Case class scala
Case class scalaCase class scala
Case class scalaMatt Hicks
 
Jedi Mind Tricks for Git
Jedi Mind Tricks for GitJedi Mind Tricks for Git
Jedi Mind Tricks for GitJan Krag
 
The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)
The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)
The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)Eugene Yokota
 
Demonstrating Case Classes in Scala
Demonstrating Case Classes in ScalaDemonstrating Case Classes in Scala
Demonstrating Case Classes in ScalaBoldRadius Solutions
 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java DevelopersMichael Galpin
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 

Andere mochten auch (7)

scala
scalascala
scala
 
Case class scala
Case class scalaCase class scala
Case class scala
 
Jedi Mind Tricks for Git
Jedi Mind Tricks for GitJedi Mind Tricks for Git
Jedi Mind Tricks for Git
 
The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)
The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)
The state of sbt 0.13, sbt server, and sbt 1.0 (ScalaSphere ver)
 
Demonstrating Case Classes in Scala
Demonstrating Case Classes in ScalaDemonstrating Case Classes in Scala
Demonstrating Case Classes in Scala
 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java Developers
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 

Ähnlich wie Intro to pattern matching in scala

An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)William Narmontas
 
Threequals - Case Equality in Ruby
Threequals - Case Equality in RubyThreequals - Case Equality in Ruby
Threequals - Case Equality in RubyLouis Scoras
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameAntony Stubbs
 
Day 5a iteration and functions if().pptx
Day 5a   iteration and functions  if().pptxDay 5a   iteration and functions  if().pptx
Day 5a iteration and functions if().pptxAdrien Melquiond
 
Ruby 2.0 / Rails 4.0, A selection of new features.
Ruby 2.0 / Rails 4.0, A selection of new features.Ruby 2.0 / Rails 4.0, A selection of new features.
Ruby 2.0 / Rails 4.0, A selection of new features.lrdesign
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. ExperienceMike Fogus
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB jhchabran
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)HamletDRC
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
The Not Java That's Not Scala
The Not Java That's Not ScalaThe Not Java That's Not Scala
The Not Java That's Not ScalaJustin Lee
 
1.1 motivation
1.1 motivation1.1 motivation
1.1 motivationwpgreenway
 

Ähnlich wie Intro to pattern matching in scala (20)

Effective Scala @ Jfokus
Effective Scala @ JfokusEffective Scala @ Jfokus
Effective Scala @ Jfokus
 
Invitation to Scala
Invitation to ScalaInvitation to Scala
Invitation to Scala
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
 
Threequals - Case Equality in Ruby
Threequals - Case Equality in RubyThreequals - Case Equality in Ruby
Threequals - Case Equality in Ruby
 
Js in the open
Js in the openJs in the open
Js in the open
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
 
JavaScript 1.8.5: New Features Explored
JavaScript 1.8.5:  New Features ExploredJavaScript 1.8.5:  New Features Explored
JavaScript 1.8.5: New Features Explored
 
Day 5a iteration and functions if().pptx
Day 5a   iteration and functions  if().pptxDay 5a   iteration and functions  if().pptx
Day 5a iteration and functions if().pptx
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala 101-bcndevcon
Scala 101-bcndevconScala 101-bcndevcon
Scala 101-bcndevcon
 
Ruby 2.0 / Rails 4.0, A selection of new features.
Ruby 2.0 / Rails 4.0, A selection of new features.Ruby 2.0 / Rails 4.0, A selection of new features.
Ruby 2.0 / Rails 4.0, A selection of new features.
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. Experience
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
Java Tips, Tricks and Pitfalls
Java Tips, Tricks and PitfallsJava Tips, Tricks and Pitfalls
Java Tips, Tricks and Pitfalls
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
 
Scala taxonomy
Scala taxonomyScala taxonomy
Scala taxonomy
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
The Not Java That's Not Scala
The Not Java That's Not ScalaThe Not Java That's Not Scala
The Not Java That's Not Scala
 
1.1 motivation
1.1 motivation1.1 motivation
1.1 motivation
 

Kürzlich hochgeladen

What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 

Kürzlich hochgeladen (20)

What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 

Intro to pattern matching in scala

  • 1. intro match { !case PatternMatching() !!!=> “Welcome” } Friday, September 6, 13
  • 2. Who am I? •Java & Scala developer at Schantz A/S •Polyglot curious, Coursera junkie •Interested in HCI and Usability •https://github.com/JKrag @jankrag • Geek, builder and flyer of kites, reptile & cat breeder, Rubik's puzzle fan Friday, September 6, 13
  • 3. Pattern Matching • A very powerful feature of Scala • Java’s “switch” on steroids? Friday, September 6, 13
  • 4. Java’s switch • lets you match a ‘value’ agains a number of cases, and conditionally executes code • basically only switch on numeric values • int, byte, etc.... • (Yes, Java 7 has switch on String, but only syntactic sugar.) • Performance > nested if’s Friday, September 6, 13
  • 5. Scala’s ‘match’ • Lets you match a ‘value’ agains complex patterns • Can switch on multiple types • Can match most kind of types by matching agains the “creation form” of an object (patience...) Friday, September 6, 13
  • 6. Scala’s match (cont.) • Each “case” is a Scala expression, and thus Each “match” block is an expression • Can be used as a full function body... Friday, September 6, 13
  • 7. History • Pattern matching is nothing new • Has existed way back in functional languages • Most notable early example: ML • Also found in Haskell, Erlang, OCaml etc. Friday, September 6, 13
  • 8. Lets get on with it... Friday, September 6, 13
  • 9. Syntax - simple “java like” def weather(code: Int): String = { code match { case 1 => "sun" case 0 => "rain" case _ => "error" } } “_” is used as wildcard for the “default” case, when we don’t need the matched value... Friday, September 6, 13
  • 10. Syntax def weather(code: Int) = code match { case 1 => "sun" case 0 => "rain" case _ => "error" } match used directly as full function body Friday, September 6, 13
  • 11. multiple matches def myPlans(weekday: Int) = weekday match { ! case 1 | 2 | 3 | 4 | 5 => "work" ! case 6 | 7 => "relax" ! case _ => "unknown weekday" } Friday, September 6, 13
  • 12. Matching literals • A literal pattern L matches any value that is equal (in terms of ==) to the literal L. Friday, September 6, 13
  • 13. matching strings def parseArgument(arg: String) = arg match { case "-h" | "--help" => displayHelp case "-v" | "--version" => displayVerion case whatever => unknownArgument(whatever) } Friday, September 6, 13
  • 14. Mixed stuff def handle(msg: Any) = msg match { ! "QUIT" => "recieved stop request" ! 42 => "The answer to the ultimate question" ! _ => "something else" } Friday, September 6, 13
  • 15. matching tuples def moveTo(coord: Any) = coord match { ! case (_, _) => println("received good 2D coord.") ! case (_, _ , _) => println("3D not supported") ! case _ => println("unexpected stuff") } Friday, September 6, 13
  • 16. matching on types • In java, if you need to “detect” types, you typically use “instance of” and typecast • In scala, we can match on types, using variables, and these know the type Friday, September 6, 13
  • 17. types and variables def handle(msg: Any) = msg match { case i:Int => "Int'eresting: " + i case _:Double => "Doubly so!" case s:String => "You really want't me to do " + s } • introduced variables (typed of course) • order can be important. Cases checked in order Friday, September 6, 13
  • 18. matching tuples - revisited def moveTo(coord: Any) = coord match { ! case (a: Int, b: Int) => updateCoordinate(a, b) ! case (_, _ , _) => println("3D not supported") ! case _ => println("unexpected coordinate") } Friday, September 6, 13
  • 19. variables • In general, everything in lowercase is treated as a “variable” • constants should start with uppercase Friday, September 6, 13
  • 20. case matters class Sample { ! val max = 100 ! val MIN = 0 ! def process(input: Int) { ! ! input match { ! ! ! case max => println("aaargh.") ! ! ! case MIN => println("matched MIN") ! ! ! case _ => println("unreachable") ! ! } ! } } What happens? Friday, September 6, 13
  • 21. case matters class Sample { ! val max = 100 ! val MIN = 0 ! def process(input: Int) { ! ! input match { ! ! ! case max => println("aaargh.") ! ! ! case MIN => println("matched MIN") ! ! ! case _ => println("unreachable") ! ! } ! } } What happens? Compile error Friday, September 6, 13
  • 22. case matters class Sample { ! val max = 100 ! val MIN = 0 ! def process(input: Int) { ! ! input match { ! ! ! case max => println("aaargh.") ! ! ! case MIN => println("matched MIN") ! ! ! case _ => println("unreachable") ! ! } ! } } What happens? Compile error unreachable code Friday, September 6, 13
  • 23. case matters class Sample { ! val max = 100 ! val MIN = 0 ! def process(input: Int) { ! ! input match { ! ! ! case max => println("aaargh.") ! ! ! case MIN => println("matched MIN") ! ! ! case _ => println("unreachable") ! ! } ! } } What happens? Compile error unreachable code fix with: this.max Friday, September 6, 13
  • 24. Practical example: Recursive factorial • Without pattern matching: def fact(n: Int): Int = if (n == 0) 1 else n * fact(n - 1) • With pattern matching: def fact(n: Int): Int = n match { case 0 => 1 case n => n * fact(n - 1) } • Note: n matches “everything else” • Note: scope Friday, September 6, 13
  • 25. Matching List • List("Apple", "Microsoft") • List("Scala", "Clojure", "Groovy", _*) • "array explosion symbol" Friday, September 6, 13
  • 27. look-alike list match { ! ! case Nil => "was an empty list" !! case x :: xs => "head was " + x + ", tail was " + xs } //remember a list in scala is either Nil, or “something and a tail” Friday, September 6, 13
  • 28. look-alike def length[A](list : List[A]) : Int = list match { case _ :: tail => 1 + length(tail) case Nil => 0 } list match { ! ! case Nil => "was an empty list" !! case x :: xs => "head was " + x + ", tail was " + xs } //remember a list in scala is either Nil, or “something and a tail” Friday, September 6, 13
  • 29. Guards • Powerful addition. • Allows for “conditional” matching Friday, September 6, 13
  • 30. Guards - example case msg : Int if (msg < 0) => printf("Execution problem. Return code: %d") case msg : Int if (msg > 0) => printf("Succes. Return code: %d") case _ : Int => printf("Boring") Friday, September 6, 13
  • 31. More advanced topics? Quick breeze through Friday, September 6, 13
  • 32. Nested patterns • case List(Cat(name), Owner(first, last)) Friday, September 6, 13
  • 33. Nested - example object Role extends Enumeration { ! type Role = Value ! val DEV, PM, CEO = Value } case class Name(first:String, last:String) case class Person(name:Name, age:Int, role:Role) val person = Person(Name("Jan", "Krag"), 42, Role.DEV) person match { ! case Person(Name(first, last), age:Int, r: Role) => first + " " + last + " (aged " + age + ")" ! case _ => "something else" } //> res4: java.lang.String = "Jan Krag (aged 42) " Friday, September 6, 13
  • 34. Pattern binders • We can assign a binder to part of a pattern during a match using the @ notation. • Often usefull when we want a larger part of a pattern to use on the expression side • e.g. case pers @ Person(“Dude”, _, _) => println(pers) • binds pers to the whole Person object Friday, September 6, 13
  • 36. case classes • Very common use case • If case class is sealed abstract, compiler can verify that match is exhaustive • works because case classes have an auto- generated unapply method. Friday, September 6, 13
  • 37. exhaustive match sealed abstract class Expr case class Var(name: String) extends Expr case class Number(num: Double) extends Expr case class UnOp(operator: String, arg: Expr) extends Expr case class BinOp(operator: String, left: Expr, right: Expr) extends Expr def describe(e: Expr): String = e match { case Number(_) => "a number" case Var(_) => "a variable" } //warning: match is not exhaustive! //missing combination UnOp //missing combination BinOp Friday, September 6, 13
  • 38. Matching XML fragments • As Scala has first class support for XML, we can also match XML fragments: case <price>{itemPrice}</price> => println("price was: " + itemPrice) Friday, September 6, 13
  • 39. Stable identifiers def f(x: Int, y: Int) = x match { case y => ... } def f(x: Int, y: Int) = x match { case `y` => ... } Friday, September 6, 13
  • 40. anomymous functions • case classes can also be used as anonymous functions, i..e. without a “match” clause: • { case p1 => b1 ... case pn => bn } Friday, September 6, 13
  • 41. in Exception handling try { throw new j.i.IOException("no such file") } catch { case e @ (_ : RuntimeException | _ : j.i.IOException) => println(e) } // prints out "java.io.IOException: no such file" Example also demonstrates binding of “alternatives” expression Friday, September 6, 13
  • 42. Deconstructing BillVenners: You said a pattern looks like an expression, but it seems kind of like a backwards expression. Instead of plugging values in and getting one result out, you put in one value, and when it matches, a bunch of values pop back out. Martin Odersky: Yes. It's really the exact reversal of construction. I can construct objects with nested constructors, and maybe I also have some parameters. Let's say I have a method that takes some parameters and constructs some complicated object structure from those parameters. Pattern matching does the reverse. It takes a complicated object structure and pulls out the parameters that were used to construct the same structure. Friday, September 6, 13
  • 43. apply(...) / unapply(...) • And this is a whole new (albeit important) subject best left for next time... Friday, September 6, 13