SlideShare a Scribd company logo
1 of 25
SCALA
PATTERN MATCHING, CONCEPTS AND
IMPLEMENTATIONS
Mustapha Michrafy PhD
M. MICHRAFY & C. VALLIEZ
Contact the authors :
datascience.km@gmail.com
datascience.km@gmail.com1
Cyril Valliez PhD
- We thank Oussama Lachiri for helpful discussions.
Context
This study was presented at the seminar
« Data Science Principales, Tools and
Applications » at Cermsem laboratory
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com2
Goal
This study aims to present the pattern
matching and its implementation with
Scala
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com3
Prerequisite
Knowledge of
• Scala programming language
• Functional programming notion
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com4
Agenda
1. Basic pattern matching
2. Pattern matching and for loop
3. Pattern alternative: operator « | »
4. Pattern guards
5. Pattern guards and logical operator OR
6. Pattern matching and recursive function
7. Pattern matching on type
8. Pattern matching on tuple
9. Pattern matching on Option
10. Pattern matching on Immutable Collection
11. Pattern matching on Immutable Map
12. Pattern matching on Immutable Set
13. Pattern matching on List
14. Pattern matching on case class
15. Nested Pattern Matching in Case Classes
16. Matching on regular expression
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com5
Basic pattern matching : switch-case
def getChoicegetChoicegetChoicegetChoice(choicechoicechoicechoice: Int): String = choicechoicechoicechoice matchmatchmatchmatch {
case 0 => "no"
case 1 => "yes"
case ____ => "error"
}
if choice = 0 then "no"
else if choice = 1 then "yes"
else then "error"
Pattern matching
getChoice(1) //> res0: String = yes
getChoice(0) //> res1: String = no
getChoice(3) //> res2: String = error
Testing
The wildcard pattern “_”
matches any object whatsoever.
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com6
Pattern matching and initialization
val choicechoicechoicechoice = 0
var mesg ==== choicechoicechoicechoice match {
case 0 => "no"
case 1 => "yes"
case _ => "error"
}
println(mesg) //> no
We can use pattern matching for initializing a variable.
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com7
Pattern matching and for loop
val choiceschoiceschoiceschoices = List(0,1,10)
for( choicechoicechoicechoice <- choices) {
choice match {
case 0 => println("no")
case 1 => println("yes")
case _ => println("error")
}
}
//> no
//> yes
//>error
We can use pattern matching with for loop
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com8
Pattern alternative: operator « | »
def getChoicegetChoicegetChoicegetChoice(choicechoicechoicechoice: Int): String = choicechoicechoicechoice matchmatchmatchmatch {
casecasecasecase 0000 =>=>=>=> """"no"no"no"no"
casecasecasecase 1111 => "no"=> "no"=> "no"=> "no"
case 2 => "yes"
case ____ => "error"
}
if choice in {0,1} then "no"
else if choice = 2 then "oui"
else then "error"
Pattern matching
« | » : OR
With “|” , we can have multiple tests on a single line
def getChoicegetChoicegetChoicegetChoice(choicechoicechoicechoice: Int): String = choicechoicechoicechoice matchmatchmatchmatch {
casecasecasecase 0000 | 1 =>| 1 =>| 1 =>| 1 => """"no"no"no"no"
case 2 => "yes"
case ____ => "error"
}
getChoice(0) //>res1: String = no
getChoice(1) //>res2: String = no
Testing
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com9
Pattern guards
def sign(x:Double) : Byte = x match {
case x if x < 0if x < 0if x < 0if x < 0 => -1
case x if x > 0if x > 0if x > 0if x > 0 => 1
case _ => 0
}
-1 if x < 0
sing(x) = 1 if x > 0
0 sinon
Pattern matching
sign(0) //> res1: Byte = 0
sign(2) //> res2: Byte = 1
sign(-2) //> res3: Byte = -1
Testing
A pattern guard comes after a
pattern and starts with an if.
If a pattern guard is present, the
match succeeds only if the
guard evaluates to true
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com10
Pattern guards and logical operator OR
def OR(p:Boolean, q: Boolean) : Boolean = (p, q)(p, q)(p, q)(p, q) match {
case (x, y) if x==true | y==trueif x==true | y==trueif x==true | y==trueif x==true | y==true => true
case _ => false
}
OR(false, false) //> res1: Boolean = false
OR(false, true) //> res2: Boolean = true
p q p OR q
T T T
T F T
F T T
F F F
If (p=T | q=T) then OR(p,q)=true
else OR(p,q) =false
Testing
Pattern matching
Transform multiple parameters into one tuple (p,q)
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com11
1
1
Pattern matching and recursive function
1 if n = 0
n*(n-1)! otherwise
n! ====
def factorialfactorialfactorialfactorial(n: Int): Int = n match {
case 0 => 1
case _ => n*factorialfactorialfactorialfactorial(n-1)
}
def factorialfactorialfactorialfactorial(n:Int) : Int = {
def fcfcfcfc(n:Int, p:Int) : Int = ((((n,pn,pn,pn,p)))) match {
case (0,c) => c
case (m,c) => fc(m-1,p*m)
}
fcfcfcfc(n,1)
}
factorial(0) //> res 1: Int = 1
factorial(4) //> res 2: Int = 24
1 Using a tuple (n,p)
2 Initialize an accumulator p to 1
Tail-recursive function
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com12
Pattern matching on type
def matchByTypematchByTypematchByTypematchByType(z: Any): String = z match {
case b:Boolean => s"A boolean : $b"
case d:Double => s"A double : $d"
case _ => " Unknown type "
}
1
2
3
1 If z is Boolean type, b takes its value.
2 If z is the type Double , d takes its value.
3 Use wildcard when z is neither a Boolean type nor a Double type
A typed pattern x:T consists of a pattern variable x and a type pattern T.
The type of x is the type pattern T, where each type variable and wildcard
is replaced by a fresh, unknown type.
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com13
Pattern matching on tuple
def detectZeroInTupledetectZeroInTupledetectZeroInTupledetectZeroInTuple(x: Tuple2[Any,Any]):String = x match {
case (0,0|(0,0)) => "Both values zero."
case (0,b) => s"First value is 0 in (0,$b)"
case (a, 0) => s"Second value is 0 in ($a, 0)"
case _ => "Both nonzero : " + x
}
1
The tuple pattern matches input in tuple form and enables the tuple to be
decomposed into its constituent elements by using pattern matching
variables for each position in the tuple.
detectZeroInTuple((0,(0,0))) //> res1: String = Both values zero.
detectZeroInTuple((0,0)) //> res2: String = Both values zero.
detectZeroInTuple((0,11)) //> res3: String = First value is 0 in (0,11)
1 Match a first pattern if x=(0,0) or x=(0,(0,0)).
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com14
Pattern matching on Option
def getValueOptiongetValueOptiongetValueOptiongetValueOption[Any](x:Option[Any]) : String = x match{
case Some(x) => s"Some $x"
case None => "None "
}
1
Option -sealed abstract class- represents optional values.
Instances of Option are either an instance of SomeSomeSomeSome or the
object None.None.None.None.
getValueOption(Option("hello")) //> Some hello
getValueOption(Option(null)) //> None
getValueOption(Option(())) //> Some ()
getValueOption(Option(List(10,11))) //Some List(10, 11))
1 Match a Some(x) if x represents an existing value.
Option
Some None
c
c o
2 Match a None if x represents a non-existing value.
2
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com15
Pattern matching on immutable collection
import scala.collection.immutable._
def getTypegetTypegetTypegetType(x:Traversable[Any]) : String = x match{
case s:Set[Any] => s"A Immutable Set : $s"
case q:Seq[Any] => s"A Immutable Seq : $q"
case _ => "A Immutable Map : $x"
}
getType(Set(0,2,4)) //> A Immutable Set : Set(0, 2, 4)
getType(1 to 3) //> Immutable Seq : Range(1, 2, 3)
getType(HashMap[Int,Int](1->13)) //> A Immutable Map : Map(1 -> 13)
2 Match a pattern then the type of x is returned
Iterable
Set
Map
Seq
t
t
t
t
traversable
t
1
1
2
3
3 Match wildcard then type map is returned
This hierarchy is valid for mutable and immutable collections
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com16
Pattern matching on Immutable Map
import scala.collection.immutable._
def getTypeOfMapgetTypeOfMapgetTypeOfMapgetTypeOfMap(x:Map[Int,Int]) : String = x match{
case _:HashMap[Int,Int] => "A HashMap "
case _:TreeMap[Int,Int] => "A TreeMap "
case _ => "A ListMap "
}
getTypeOfMap(HashMap[Int,Int](1->12,2->10)) //>A HashMap
getTypeOfMap(TreeMap[Int,Int](1->3,6->6)) //> A TreeMap
getTypeOfMap(ListMap[Int,Int](1->13,6->16)) //> A ListMap
1 if x is neither a HashMap nor a TreeMap then x is a ListMap
Map
HashMap
ListMap
TreeMap
t
c
c
c
This hierarchy is valid only for the immutable Map
Map is a Trait and all (children) are implementation
classes
1
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com17
Pattern matching on immutable Set
import scala.collection.immutable._
import scala.collection.immutable.BitSet._
def getTypeOfSetgetTypeOfSetgetTypeOfSetgetTypeOfSet(x:Set[Int]) : String = x match{
case h:HashSet[Int] => "A HashSet " + (h.size,h.sum)
case t:TreeSet[Int] => "A TreeSet " + (t.size,t.sum)
case l:ListSet[Int] => "A ListSet " + (l.size,l.sum)
case z:AnyRef => "type : " + z.getClass.getName
}
getTypeOfSet(new BitSet1(10)) //> type : scala.collection.immutable.BitSet$BitSet1
getTypeOfSet(HashSet(1,10,12)) //> A HashSet (3,23)
getTypeOfSet(TreeSet(1,10)) //> A TreeSet (2,11)
getTypeOfSet(ListSet(1,11,12)) //> A ListSet (3,24)
Set
HashSet
ListSet
BitSet
t
c
c
c
This hierarchy is valid only for the immutable Set.
The Set is a Trait. BitSet is an abstract class and all children are
the class.
TreeSet
c
SortedSet
t
The subclasses of the abstract class BitSet are BitSet1, BitSet2 and BitSetN
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com18
Pattern matching on List 1/2
def sizeListsizeListsizeListsizeList[A](ls : List[A]) : Int = ls match{
case Nil => 0
case _::q => sizeList(q) + 1
}
The following function computes the length of a list.
It is based on two constructors NilNilNilNil and “::”.
sizeList(List()) //> res40: Int = 0
sizeList(List(1)) //> res41: Int = 1
sizeList(List(1,(1,2),3,4)) //> res42: Int = 4
1 If the list is empty, Nil, then we return 0.
2 If ls represents a no-empty list then the length of list is
(1+length(q)), where q represents the tail of the list.
1
2
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com19
Pattern matching on List 2/2
def identifyListeidentifyListeidentifyListeidentifyListe(ls: List[Any]): String = ls match {
case Nil => "empty list"
case _::Nil => "list with single element"
case _::_::Nil => "list has two elements"
case _ => "list has at least tree elements"
}
All lists are built from two constructors : NilNilNilNil and “::”....
NilNilNilNil represents a empty list
:::::::: builds a non empty list characterized by a head and a tail.
identifyListe(Nil) //> empty list
identifyListe(List((1,2))) //> list with single element
identifyListe(List("Scala", 3)) //> list has two elements
identifyListe(List(1,2,3)) //> list has at least tree elements
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com20
Pattern matching on case class
case class PersonPersonPersonPerson(name: String, age: Int)
def validateAgevalidateAgevalidateAgevalidateAge(p: Person): Option[String] = p match {
case Person(name, age) if age > 18 => Some(s"Hi $name! age $age")
case _ => None
}
Case classes are classes that get to String, hashCode, and equals methods
automatically. It provide a recursive decomposition mechanism via pattern
matching
validateAge(Person("Alice", 19)) //> Some(Hi Alice! age 19)
validateAge(Person("Bill", 15)) //> None
2 If the age is greater than18 , then we return the name and age of the person.
3 If the age of the person is less than 18 , then we return None.
2
3
1
1 We define a case class of a person with name and age.
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com21
Nested Pattern Matching in Case Classes
case class Address(street: String, city: String, country: String)
case class Person(name: String, age: Int, address:Address)
def checkPrscheckPrscheckPrscheckPrs(p: Person): Option[String] = p match {
case Person(name, age, null) if age > 18 => Some(s"Hi $name! age $age")
case Person(name, age, Address(_,_,ctr)) => Some(s"Hi $name! age $age $ctr")
case _ => None
}
checkPrs(Person("Alice", 19,null)) //> Some(Hi Alice! age 19)
checkPrs(Person("Bill", 15, Address("Scala Lane", "Chicago", "USA"))) //> Some(Hi Bill! age 15 USA)
2 If age > 18 & address = null, then we return the name and age of the person.
3 If the address != null then we return the name, age and the city of the person.
3
2
1 We define two case classes Address and Person. Person contains a subclass Address.
1
Case classes can contain other case classes, and the pattern matching can be nested.
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com22
Matching on regular expression
val email = """(.*)@(.*).(.*)""".r
val date = """(dddd)-(dd)-(dd)""".r
def checkPattern(x:String) : Option[String] = x match{
case email(name,domain,extension) => Some(s"$name $domain $extension")
case date(year, month, day) => Some(s"$year $month $day")
case _ => None
}
A regular expression is used to determine whether a string matches a pattern and,
if it does, to extract or transform the parts that match. Scala supports regular
expressions through Regex class available in the scala.util.matching package.
2
3
1
4
1 We define a regular expressions for email and date by using the method rmethod rmethod rmethod r2
3 If regex of email is matched then extract name, domain and extension
4 If regex of date is matched then extract year, month and day
checkPattern("info@scala.fr") //> Some(info scala fr)
checkPattern("2016-03-26") //> Some(2016 03 26)
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com23
References
• Scala Language Specification, http://www.scala-
lang.org/files/archive/spec/2.11/
• Documentation for the Scala standard library,
http://www.scala-lang.org/api/current/
• Beginning Scala, by Vishal Layka, David Pollak, 2015
• Programming in Scala, by Martin Odersky and al., 2011
• Programming Scala, by Dean Wampler, Alex Payne, 2014
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com24
Mustapha Michrafy PhD
Data Scientist and Expert Optim
at ATOS
Contact : datascience.km@gmail.com
Cyril Valliez PhD
Software Architect at Bouygues
M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com25

More Related Content

What's hot

Perform brute force
Perform brute forcePerform brute force
Perform brute forceSHC
 
Pattern matching programs
Pattern matching programsPattern matching programs
Pattern matching programsakruthi k
 
Pattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistancePattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistanceBenjamin Sach
 
Pattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesPattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesBenjamin Sach
 
Computability, turing machines and lambda calculus
Computability, turing machines and lambda calculusComputability, turing machines and lambda calculus
Computability, turing machines and lambda calculusEdward Blurock
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular ExpressionMasudul Haque
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4Philip Schwarz
 
String in python lecture (3)
String in python lecture (3)String in python lecture (3)
String in python lecture (3)Ali ٍSattar
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regexwayn
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5Philip Schwarz
 
functions
 functions  functions
functions Gaditek
 
Pattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesPattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesBenjamin Sach
 
Monad Laws Must be Checked
Monad Laws Must be CheckedMonad Laws Must be Checked
Monad Laws Must be CheckedPhilip Schwarz
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 

What's hot (19)

Perform brute force
Perform brute forcePerform brute force
Perform brute force
 
Python data handling
Python data handlingPython data handling
Python data handling
 
Pattern matching programs
Pattern matching programsPattern matching programs
Pattern matching programs
 
Pattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistancePattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming Distance
 
Pattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesPattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatches
 
Kmp
KmpKmp
Kmp
 
Team 1
Team 1Team 1
Team 1
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Computability, turing machines and lambda calculus
Computability, turing machines and lambda calculusComputability, turing machines and lambda calculus
Computability, turing machines and lambda calculus
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular Expression
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
 
String in python lecture (3)
String in python lecture (3)String in python lecture (3)
String in python lecture (3)
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
 
functions
 functions  functions
functions
 
Pattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesPattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix Trees
 
Monad Laws Must be Checked
Monad Laws Must be CheckedMonad Laws Must be Checked
Monad Laws Must be Checked
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
smtlecture.4
smtlecture.4smtlecture.4
smtlecture.4
 

Viewers also liked

Spark SQL principes et fonctions
Spark SQL principes et fonctionsSpark SQL principes et fonctions
Spark SQL principes et fonctionsMICHRAFY MUSTAFA
 
Apache SPARK ML : principes, concepts et mise en œuvre
Apache SPARK  ML : principes, concepts et  mise en œuvre Apache SPARK  ML : principes, concepts et  mise en œuvre
Apache SPARK ML : principes, concepts et mise en œuvre MICHRAFY MUSTAFA
 
Interface fonctionnelle, Lambda expression, méthode par défaut, référence de...
Interface fonctionnelle, Lambda expression, méthode par défaut,  référence de...Interface fonctionnelle, Lambda expression, méthode par défaut,  référence de...
Interface fonctionnelle, Lambda expression, méthode par défaut, référence de...MICHRAFY MUSTAFA
 
Scala : programmation fonctionnelle
Scala : programmation fonctionnelleScala : programmation fonctionnelle
Scala : programmation fonctionnelleMICHRAFY MUSTAFA
 
Spark RDD : Transformations & Actions
Spark RDD : Transformations & ActionsSpark RDD : Transformations & Actions
Spark RDD : Transformations & ActionsMICHRAFY MUSTAFA
 
Base de données graphe, Noe4j concepts et mise en oeuvre
Base de données graphe, Noe4j concepts et mise en oeuvreBase de données graphe, Noe4j concepts et mise en oeuvre
Base de données graphe, Noe4j concepts et mise en oeuvreMICHRAFY MUSTAFA
 
Les systèmes de représentations sensorielles de la PNL
Les systèmes de représentations sensorielles de la PNLLes systèmes de représentations sensorielles de la PNL
Les systèmes de représentations sensorielles de la PNLkhalil michrafy
 
Big Data: Concepts, techniques et démonstration de Apache Hadoop
Big Data: Concepts, techniques et démonstration de Apache HadoopBig Data: Concepts, techniques et démonstration de Apache Hadoop
Big Data: Concepts, techniques et démonstration de Apache Hadoophajlaoui jaleleddine
 
Introduction to scala for a c programmer
Introduction to scala for a c programmerIntroduction to scala for a c programmer
Introduction to scala for a c programmerGirish Kumar A L
 
Jump Start into Apache Spark (Seattle Spark Meetup)
Jump Start into Apache Spark (Seattle Spark Meetup)Jump Start into Apache Spark (Seattle Spark Meetup)
Jump Start into Apache Spark (Seattle Spark Meetup)Denny Lee
 
Spark - Alexis Seigneurin (Français)
Spark - Alexis Seigneurin (Français)Spark - Alexis Seigneurin (Français)
Spark - Alexis Seigneurin (Français)Alexis Seigneurin
 
Performance Optimization Case Study: Shattering Hadoop's Sort Record with Spa...
Performance Optimization Case Study: Shattering Hadoop's Sort Record with Spa...Performance Optimization Case Study: Shattering Hadoop's Sort Record with Spa...
Performance Optimization Case Study: Shattering Hadoop's Sort Record with Spa...Databricks
 
Scala - A Scalable Language
Scala - A Scalable LanguageScala - A Scalable Language
Scala - A Scalable LanguageMario Gleichmann
 

Viewers also liked (20)

Spark SQL principes et fonctions
Spark SQL principes et fonctionsSpark SQL principes et fonctions
Spark SQL principes et fonctions
 
Apache SPARK ML : principes, concepts et mise en œuvre
Apache SPARK  ML : principes, concepts et  mise en œuvre Apache SPARK  ML : principes, concepts et  mise en œuvre
Apache SPARK ML : principes, concepts et mise en œuvre
 
Interface fonctionnelle, Lambda expression, méthode par défaut, référence de...
Interface fonctionnelle, Lambda expression, méthode par défaut,  référence de...Interface fonctionnelle, Lambda expression, méthode par défaut,  référence de...
Interface fonctionnelle, Lambda expression, méthode par défaut, référence de...
 
Scala : programmation fonctionnelle
Scala : programmation fonctionnelleScala : programmation fonctionnelle
Scala : programmation fonctionnelle
 
Spark RDD : Transformations & Actions
Spark RDD : Transformations & ActionsSpark RDD : Transformations & Actions
Spark RDD : Transformations & Actions
 
Base de données graphe, Noe4j concepts et mise en oeuvre
Base de données graphe, Noe4j concepts et mise en oeuvreBase de données graphe, Noe4j concepts et mise en oeuvre
Base de données graphe, Noe4j concepts et mise en oeuvre
 
Les systèmes de représentations sensorielles de la PNL
Les systèmes de représentations sensorielles de la PNLLes systèmes de représentations sensorielles de la PNL
Les systèmes de représentations sensorielles de la PNL
 
Big Data: Concepts, techniques et démonstration de Apache Hadoop
Big Data: Concepts, techniques et démonstration de Apache HadoopBig Data: Concepts, techniques et démonstration de Apache Hadoop
Big Data: Concepts, techniques et démonstration de Apache Hadoop
 
spark_intro_1208
spark_intro_1208spark_intro_1208
spark_intro_1208
 
0712_Seigneurin
0712_Seigneurin0712_Seigneurin
0712_Seigneurin
 
Introduction to scala for a c programmer
Introduction to scala for a c programmerIntroduction to scala for a c programmer
Introduction to scala for a c programmer
 
Zaharia spark-scala-days-2012
Zaharia spark-scala-days-2012Zaharia spark-scala-days-2012
Zaharia spark-scala-days-2012
 
Jump Start into Apache Spark (Seattle Spark Meetup)
Jump Start into Apache Spark (Seattle Spark Meetup)Jump Start into Apache Spark (Seattle Spark Meetup)
Jump Start into Apache Spark (Seattle Spark Meetup)
 
Apache hive
Apache hiveApache hive
Apache hive
 
Spark - Alexis Seigneurin (Français)
Spark - Alexis Seigneurin (Français)Spark - Alexis Seigneurin (Français)
Spark - Alexis Seigneurin (Français)
 
Performance Optimization Case Study: Shattering Hadoop's Sort Record with Spa...
Performance Optimization Case Study: Shattering Hadoop's Sort Record with Spa...Performance Optimization Case Study: Shattering Hadoop's Sort Record with Spa...
Performance Optimization Case Study: Shattering Hadoop's Sort Record with Spa...
 
Python to scala
Python to scalaPython to scala
Python to scala
 
Scala - A Scalable Language
Scala - A Scalable LanguageScala - A Scalable Language
Scala - A Scalable Language
 
Indexed Hive
Indexed HiveIndexed Hive
Indexed Hive
 
Fun[ctional] spark with scala
Fun[ctional] spark with scalaFun[ctional] spark with scala
Fun[ctional] spark with scala
 

Similar to SCALA PATTERN MATCHING GUIDE

Similar to SCALA PATTERN MATCHING GUIDE (20)

Clone Refactoring with Lambda Expressions
Clone Refactoring with Lambda ExpressionsClone Refactoring with Lambda Expressions
Clone Refactoring with Lambda Expressions
 
1.Array and linklst definition
1.Array and linklst definition1.Array and linklst definition
1.Array and linklst definition
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in Scala
 
R for Statistical Computing
R for Statistical ComputingR for Statistical Computing
R for Statistical Computing
 
Advanced matlab codigos matematicos
Advanced matlab codigos matematicosAdvanced matlab codigos matematicos
Advanced matlab codigos matematicos
 
20170509 rand db_lesugent
20170509 rand db_lesugent20170509 rand db_lesugent
20170509 rand db_lesugent
 
ML MODULE 2.pdf
ML MODULE 2.pdfML MODULE 2.pdf
ML MODULE 2.pdf
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
R Basics
R BasicsR Basics
R Basics
 
Rcommands-for those who interested in R.
Rcommands-for those who interested in R.Rcommands-for those who interested in R.
Rcommands-for those who interested in R.
 
Array
ArrayArray
Array
 
Reference card for R
Reference card for RReference card for R
Reference card for R
 
Short Reference Card for R users.
Short Reference Card for R users.Short Reference Card for R users.
Short Reference Card for R users.
 
Scala Introduction
Scala IntroductionScala Introduction
Scala Introduction
 
3_MLE_printable.pdf
3_MLE_printable.pdf3_MLE_printable.pdf
3_MLE_printable.pdf
 
Hands-On Algorithms for Predictive Modeling
Hands-On Algorithms for Predictive ModelingHands-On Algorithms for Predictive Modeling
Hands-On Algorithms for Predictive Modeling
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Ai_Project_report
Ai_Project_reportAi_Project_report
Ai_Project_report
 
Statistics lab 1
Statistics lab 1Statistics lab 1
Statistics lab 1
 

Recently uploaded

Non Text Magic Studio Magic Design for Presentations L&P.pdf
Non Text Magic Studio Magic Design for Presentations L&P.pdfNon Text Magic Studio Magic Design for Presentations L&P.pdf
Non Text Magic Studio Magic Design for Presentations L&P.pdfPratikPatil591646
 
Digital Indonesia Report 2024 by We Are Social .pdf
Digital Indonesia Report 2024 by We Are Social .pdfDigital Indonesia Report 2024 by We Are Social .pdf
Digital Indonesia Report 2024 by We Are Social .pdfNicoChristianSunaryo
 
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...Dr Arash Najmaei ( Phd., MBA, BSc)
 
Bank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis ProjectBank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis ProjectBoston Institute of Analytics
 
IBEF report on the Insurance market in India
IBEF report on the Insurance market in IndiaIBEF report on the Insurance market in India
IBEF report on the Insurance market in IndiaManalVerma4
 
Presentation of project of business person who are success
Presentation of project of business person who are successPresentation of project of business person who are success
Presentation of project of business person who are successPratikSingh115843
 
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...Jack Cole
 
Digital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksDigital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksdeepakthakur548787
 
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...Boston Institute of Analytics
 
Statistics For Management by Richard I. Levin 8ed.pdf
Statistics For Management by Richard I. Levin 8ed.pdfStatistics For Management by Richard I. Levin 8ed.pdf
Statistics For Management by Richard I. Levin 8ed.pdfnikeshsingh56
 
Role of Consumer Insights in business transformation
Role of Consumer Insights in business transformationRole of Consumer Insights in business transformation
Role of Consumer Insights in business transformationAnnie Melnic
 
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfEnglish-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfblazblazml
 
DATA ANALYSIS using various data sets like shoping data set etc
DATA ANALYSIS using various data sets like shoping data set etcDATA ANALYSIS using various data sets like shoping data set etc
DATA ANALYSIS using various data sets like shoping data set etclalithasri22
 
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis model
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis modelDecoding Movie Sentiments: Analyzing Reviews with Data Analysis model
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis modelBoston Institute of Analytics
 

Recently uploaded (17)

Non Text Magic Studio Magic Design for Presentations L&P.pdf
Non Text Magic Studio Magic Design for Presentations L&P.pdfNon Text Magic Studio Magic Design for Presentations L&P.pdf
Non Text Magic Studio Magic Design for Presentations L&P.pdf
 
Digital Indonesia Report 2024 by We Are Social .pdf
Digital Indonesia Report 2024 by We Are Social .pdfDigital Indonesia Report 2024 by We Are Social .pdf
Digital Indonesia Report 2024 by We Are Social .pdf
 
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
 
Bank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis ProjectBank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis Project
 
IBEF report on the Insurance market in India
IBEF report on the Insurance market in IndiaIBEF report on the Insurance market in India
IBEF report on the Insurance market in India
 
Presentation of project of business person who are success
Presentation of project of business person who are successPresentation of project of business person who are success
Presentation of project of business person who are success
 
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
why-transparency-and-traceability-are-essential-for-sustainable-supply-chains...
 
Digital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksDigital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing works
 
Data Analysis Project: Stroke Prediction
Data Analysis Project: Stroke PredictionData Analysis Project: Stroke Prediction
Data Analysis Project: Stroke Prediction
 
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
Data Analysis Project Presentation: Unveiling Your Ideal Customer, Bank Custo...
 
Statistics For Management by Richard I. Levin 8ed.pdf
Statistics For Management by Richard I. Levin 8ed.pdfStatistics For Management by Richard I. Levin 8ed.pdf
Statistics For Management by Richard I. Levin 8ed.pdf
 
Insurance Churn Prediction Data Analysis Project
Insurance Churn Prediction Data Analysis ProjectInsurance Churn Prediction Data Analysis Project
Insurance Churn Prediction Data Analysis Project
 
Role of Consumer Insights in business transformation
Role of Consumer Insights in business transformationRole of Consumer Insights in business transformation
Role of Consumer Insights in business transformation
 
2023 Survey Shows Dip in High School E-Cigarette Use
2023 Survey Shows Dip in High School E-Cigarette Use2023 Survey Shows Dip in High School E-Cigarette Use
2023 Survey Shows Dip in High School E-Cigarette Use
 
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfEnglish-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
 
DATA ANALYSIS using various data sets like shoping data set etc
DATA ANALYSIS using various data sets like shoping data set etcDATA ANALYSIS using various data sets like shoping data set etc
DATA ANALYSIS using various data sets like shoping data set etc
 
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis model
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis modelDecoding Movie Sentiments: Analyzing Reviews with Data Analysis model
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis model
 

SCALA PATTERN MATCHING GUIDE

  • 1. SCALA PATTERN MATCHING, CONCEPTS AND IMPLEMENTATIONS Mustapha Michrafy PhD M. MICHRAFY & C. VALLIEZ Contact the authors : datascience.km@gmail.com datascience.km@gmail.com1 Cyril Valliez PhD - We thank Oussama Lachiri for helpful discussions.
  • 2. Context This study was presented at the seminar « Data Science Principales, Tools and Applications » at Cermsem laboratory M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com2
  • 3. Goal This study aims to present the pattern matching and its implementation with Scala M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com3
  • 4. Prerequisite Knowledge of • Scala programming language • Functional programming notion M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com4
  • 5. Agenda 1. Basic pattern matching 2. Pattern matching and for loop 3. Pattern alternative: operator « | » 4. Pattern guards 5. Pattern guards and logical operator OR 6. Pattern matching and recursive function 7. Pattern matching on type 8. Pattern matching on tuple 9. Pattern matching on Option 10. Pattern matching on Immutable Collection 11. Pattern matching on Immutable Map 12. Pattern matching on Immutable Set 13. Pattern matching on List 14. Pattern matching on case class 15. Nested Pattern Matching in Case Classes 16. Matching on regular expression M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com5
  • 6. Basic pattern matching : switch-case def getChoicegetChoicegetChoicegetChoice(choicechoicechoicechoice: Int): String = choicechoicechoicechoice matchmatchmatchmatch { case 0 => "no" case 1 => "yes" case ____ => "error" } if choice = 0 then "no" else if choice = 1 then "yes" else then "error" Pattern matching getChoice(1) //> res0: String = yes getChoice(0) //> res1: String = no getChoice(3) //> res2: String = error Testing The wildcard pattern “_” matches any object whatsoever. M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com6
  • 7. Pattern matching and initialization val choicechoicechoicechoice = 0 var mesg ==== choicechoicechoicechoice match { case 0 => "no" case 1 => "yes" case _ => "error" } println(mesg) //> no We can use pattern matching for initializing a variable. M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com7
  • 8. Pattern matching and for loop val choiceschoiceschoiceschoices = List(0,1,10) for( choicechoicechoicechoice <- choices) { choice match { case 0 => println("no") case 1 => println("yes") case _ => println("error") } } //> no //> yes //>error We can use pattern matching with for loop M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com8
  • 9. Pattern alternative: operator « | » def getChoicegetChoicegetChoicegetChoice(choicechoicechoicechoice: Int): String = choicechoicechoicechoice matchmatchmatchmatch { casecasecasecase 0000 =>=>=>=> """"no"no"no"no" casecasecasecase 1111 => "no"=> "no"=> "no"=> "no" case 2 => "yes" case ____ => "error" } if choice in {0,1} then "no" else if choice = 2 then "oui" else then "error" Pattern matching « | » : OR With “|” , we can have multiple tests on a single line def getChoicegetChoicegetChoicegetChoice(choicechoicechoicechoice: Int): String = choicechoicechoicechoice matchmatchmatchmatch { casecasecasecase 0000 | 1 =>| 1 =>| 1 =>| 1 => """"no"no"no"no" case 2 => "yes" case ____ => "error" } getChoice(0) //>res1: String = no getChoice(1) //>res2: String = no Testing M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com9
  • 10. Pattern guards def sign(x:Double) : Byte = x match { case x if x < 0if x < 0if x < 0if x < 0 => -1 case x if x > 0if x > 0if x > 0if x > 0 => 1 case _ => 0 } -1 if x < 0 sing(x) = 1 if x > 0 0 sinon Pattern matching sign(0) //> res1: Byte = 0 sign(2) //> res2: Byte = 1 sign(-2) //> res3: Byte = -1 Testing A pattern guard comes after a pattern and starts with an if. If a pattern guard is present, the match succeeds only if the guard evaluates to true M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com10
  • 11. Pattern guards and logical operator OR def OR(p:Boolean, q: Boolean) : Boolean = (p, q)(p, q)(p, q)(p, q) match { case (x, y) if x==true | y==trueif x==true | y==trueif x==true | y==trueif x==true | y==true => true case _ => false } OR(false, false) //> res1: Boolean = false OR(false, true) //> res2: Boolean = true p q p OR q T T T T F T F T T F F F If (p=T | q=T) then OR(p,q)=true else OR(p,q) =false Testing Pattern matching Transform multiple parameters into one tuple (p,q) M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com11 1 1
  • 12. Pattern matching and recursive function 1 if n = 0 n*(n-1)! otherwise n! ==== def factorialfactorialfactorialfactorial(n: Int): Int = n match { case 0 => 1 case _ => n*factorialfactorialfactorialfactorial(n-1) } def factorialfactorialfactorialfactorial(n:Int) : Int = { def fcfcfcfc(n:Int, p:Int) : Int = ((((n,pn,pn,pn,p)))) match { case (0,c) => c case (m,c) => fc(m-1,p*m) } fcfcfcfc(n,1) } factorial(0) //> res 1: Int = 1 factorial(4) //> res 2: Int = 24 1 Using a tuple (n,p) 2 Initialize an accumulator p to 1 Tail-recursive function M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com12
  • 13. Pattern matching on type def matchByTypematchByTypematchByTypematchByType(z: Any): String = z match { case b:Boolean => s"A boolean : $b" case d:Double => s"A double : $d" case _ => " Unknown type " } 1 2 3 1 If z is Boolean type, b takes its value. 2 If z is the type Double , d takes its value. 3 Use wildcard when z is neither a Boolean type nor a Double type A typed pattern x:T consists of a pattern variable x and a type pattern T. The type of x is the type pattern T, where each type variable and wildcard is replaced by a fresh, unknown type. M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com13
  • 14. Pattern matching on tuple def detectZeroInTupledetectZeroInTupledetectZeroInTupledetectZeroInTuple(x: Tuple2[Any,Any]):String = x match { case (0,0|(0,0)) => "Both values zero." case (0,b) => s"First value is 0 in (0,$b)" case (a, 0) => s"Second value is 0 in ($a, 0)" case _ => "Both nonzero : " + x } 1 The tuple pattern matches input in tuple form and enables the tuple to be decomposed into its constituent elements by using pattern matching variables for each position in the tuple. detectZeroInTuple((0,(0,0))) //> res1: String = Both values zero. detectZeroInTuple((0,0)) //> res2: String = Both values zero. detectZeroInTuple((0,11)) //> res3: String = First value is 0 in (0,11) 1 Match a first pattern if x=(0,0) or x=(0,(0,0)). M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com14
  • 15. Pattern matching on Option def getValueOptiongetValueOptiongetValueOptiongetValueOption[Any](x:Option[Any]) : String = x match{ case Some(x) => s"Some $x" case None => "None " } 1 Option -sealed abstract class- represents optional values. Instances of Option are either an instance of SomeSomeSomeSome or the object None.None.None.None. getValueOption(Option("hello")) //> Some hello getValueOption(Option(null)) //> None getValueOption(Option(())) //> Some () getValueOption(Option(List(10,11))) //Some List(10, 11)) 1 Match a Some(x) if x represents an existing value. Option Some None c c o 2 Match a None if x represents a non-existing value. 2 M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com15
  • 16. Pattern matching on immutable collection import scala.collection.immutable._ def getTypegetTypegetTypegetType(x:Traversable[Any]) : String = x match{ case s:Set[Any] => s"A Immutable Set : $s" case q:Seq[Any] => s"A Immutable Seq : $q" case _ => "A Immutable Map : $x" } getType(Set(0,2,4)) //> A Immutable Set : Set(0, 2, 4) getType(1 to 3) //> Immutable Seq : Range(1, 2, 3) getType(HashMap[Int,Int](1->13)) //> A Immutable Map : Map(1 -> 13) 2 Match a pattern then the type of x is returned Iterable Set Map Seq t t t t traversable t 1 1 2 3 3 Match wildcard then type map is returned This hierarchy is valid for mutable and immutable collections M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com16
  • 17. Pattern matching on Immutable Map import scala.collection.immutable._ def getTypeOfMapgetTypeOfMapgetTypeOfMapgetTypeOfMap(x:Map[Int,Int]) : String = x match{ case _:HashMap[Int,Int] => "A HashMap " case _:TreeMap[Int,Int] => "A TreeMap " case _ => "A ListMap " } getTypeOfMap(HashMap[Int,Int](1->12,2->10)) //>A HashMap getTypeOfMap(TreeMap[Int,Int](1->3,6->6)) //> A TreeMap getTypeOfMap(ListMap[Int,Int](1->13,6->16)) //> A ListMap 1 if x is neither a HashMap nor a TreeMap then x is a ListMap Map HashMap ListMap TreeMap t c c c This hierarchy is valid only for the immutable Map Map is a Trait and all (children) are implementation classes 1 M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com17
  • 18. Pattern matching on immutable Set import scala.collection.immutable._ import scala.collection.immutable.BitSet._ def getTypeOfSetgetTypeOfSetgetTypeOfSetgetTypeOfSet(x:Set[Int]) : String = x match{ case h:HashSet[Int] => "A HashSet " + (h.size,h.sum) case t:TreeSet[Int] => "A TreeSet " + (t.size,t.sum) case l:ListSet[Int] => "A ListSet " + (l.size,l.sum) case z:AnyRef => "type : " + z.getClass.getName } getTypeOfSet(new BitSet1(10)) //> type : scala.collection.immutable.BitSet$BitSet1 getTypeOfSet(HashSet(1,10,12)) //> A HashSet (3,23) getTypeOfSet(TreeSet(1,10)) //> A TreeSet (2,11) getTypeOfSet(ListSet(1,11,12)) //> A ListSet (3,24) Set HashSet ListSet BitSet t c c c This hierarchy is valid only for the immutable Set. The Set is a Trait. BitSet is an abstract class and all children are the class. TreeSet c SortedSet t The subclasses of the abstract class BitSet are BitSet1, BitSet2 and BitSetN M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com18
  • 19. Pattern matching on List 1/2 def sizeListsizeListsizeListsizeList[A](ls : List[A]) : Int = ls match{ case Nil => 0 case _::q => sizeList(q) + 1 } The following function computes the length of a list. It is based on two constructors NilNilNilNil and “::”. sizeList(List()) //> res40: Int = 0 sizeList(List(1)) //> res41: Int = 1 sizeList(List(1,(1,2),3,4)) //> res42: Int = 4 1 If the list is empty, Nil, then we return 0. 2 If ls represents a no-empty list then the length of list is (1+length(q)), where q represents the tail of the list. 1 2 M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com19
  • 20. Pattern matching on List 2/2 def identifyListeidentifyListeidentifyListeidentifyListe(ls: List[Any]): String = ls match { case Nil => "empty list" case _::Nil => "list with single element" case _::_::Nil => "list has two elements" case _ => "list has at least tree elements" } All lists are built from two constructors : NilNilNilNil and “::”.... NilNilNilNil represents a empty list :::::::: builds a non empty list characterized by a head and a tail. identifyListe(Nil) //> empty list identifyListe(List((1,2))) //> list with single element identifyListe(List("Scala", 3)) //> list has two elements identifyListe(List(1,2,3)) //> list has at least tree elements M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com20
  • 21. Pattern matching on case class case class PersonPersonPersonPerson(name: String, age: Int) def validateAgevalidateAgevalidateAgevalidateAge(p: Person): Option[String] = p match { case Person(name, age) if age > 18 => Some(s"Hi $name! age $age") case _ => None } Case classes are classes that get to String, hashCode, and equals methods automatically. It provide a recursive decomposition mechanism via pattern matching validateAge(Person("Alice", 19)) //> Some(Hi Alice! age 19) validateAge(Person("Bill", 15)) //> None 2 If the age is greater than18 , then we return the name and age of the person. 3 If the age of the person is less than 18 , then we return None. 2 3 1 1 We define a case class of a person with name and age. M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com21
  • 22. Nested Pattern Matching in Case Classes case class Address(street: String, city: String, country: String) case class Person(name: String, age: Int, address:Address) def checkPrscheckPrscheckPrscheckPrs(p: Person): Option[String] = p match { case Person(name, age, null) if age > 18 => Some(s"Hi $name! age $age") case Person(name, age, Address(_,_,ctr)) => Some(s"Hi $name! age $age $ctr") case _ => None } checkPrs(Person("Alice", 19,null)) //> Some(Hi Alice! age 19) checkPrs(Person("Bill", 15, Address("Scala Lane", "Chicago", "USA"))) //> Some(Hi Bill! age 15 USA) 2 If age > 18 & address = null, then we return the name and age of the person. 3 If the address != null then we return the name, age and the city of the person. 3 2 1 We define two case classes Address and Person. Person contains a subclass Address. 1 Case classes can contain other case classes, and the pattern matching can be nested. M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com22
  • 23. Matching on regular expression val email = """(.*)@(.*).(.*)""".r val date = """(dddd)-(dd)-(dd)""".r def checkPattern(x:String) : Option[String] = x match{ case email(name,domain,extension) => Some(s"$name $domain $extension") case date(year, month, day) => Some(s"$year $month $day") case _ => None } A regular expression is used to determine whether a string matches a pattern and, if it does, to extract or transform the parts that match. Scala supports regular expressions through Regex class available in the scala.util.matching package. 2 3 1 4 1 We define a regular expressions for email and date by using the method rmethod rmethod rmethod r2 3 If regex of email is matched then extract name, domain and extension 4 If regex of date is matched then extract year, month and day checkPattern("info@scala.fr") //> Some(info scala fr) checkPattern("2016-03-26") //> Some(2016 03 26) M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com23
  • 24. References • Scala Language Specification, http://www.scala- lang.org/files/archive/spec/2.11/ • Documentation for the Scala standard library, http://www.scala-lang.org/api/current/ • Beginning Scala, by Vishal Layka, David Pollak, 2015 • Programming in Scala, by Martin Odersky and al., 2011 • Programming Scala, by Dean Wampler, Alex Payne, 2014 M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com24
  • 25. Mustapha Michrafy PhD Data Scientist and Expert Optim at ATOS Contact : datascience.km@gmail.com Cyril Valliez PhD Software Architect at Bouygues M. MICHRAFY & C. VALLIEZ datascience.km@gmail.com25