SlideShare una empresa de Scribd logo
1 de 4
Descargar para leer sin conexión
Healthy Code
April - 2015
10
In 2013, Dhaval Dalal was inspired by Jugalbandi, an Indian classical music
form in which musicians have an improvised musical conversation, each using a
different instrument, while exploring a shared musical theme together akin to Jazz
in some ways. Dhaval thought, “What if we could have a conversation about some
programming concepts in this way? Instead of musical instruments, what if we use
different programming languages?”
Code
Jugalbandi
Article
Ryan and Dhaval
Healthy Code
April - 2015
11
Creator and Listener
We met regularly with over a period of several months,
exploring some key issues related to Functional
Programming(FP).ThisculminatedinaliveCodeJugalbandi
at the Functional Conference in Bengaluru, 2014. During
the FP Code Jugalbandi, we explored nine themes, which
became melodies. Similar to the musical Jugalbandi, there
are two roles: creator and listener. We called the creator of
the melody Bramha and the one who listens and responds
with a different instrument, Krishna. + In the last Code
Jugalbandi Article, we looked at the melody, the Currying
and Partial Function problem. This time we will look at De-
Structuring and Pattern Matching.
De-structuring
BRAMHA: In FP, we favour simple data structures over
classes, e.g. instead of a collection of people objects, we might
have a collection of person tuples, and containing only the
fields we’re interested in:
//Tuple with 2 elements - name and age
type Person = (String, Int)
val p = ("alan", 30)
BRAMHA: … and then we have some functions acting on
this person data type. Whatever we’ll do with person, we’ll
need code to get at the innards of the person tuple.
def showPerson(p: Person) = p match {
case (name, age) => println(s"$name - $age")
}
showPerson(p) //alan - 30
BRAMHA: How does this look in Clojure?
KRISHNA: Most functional languages provide some level
of de-structuring:
(def a-person ["alan" 30])
(defn print-person [p]
(println (first p) " " (second p)))
(defn print-person2 [[name age]]
(println name " - " age))
KRISHNA: De-structuring gives you a shorthand way of
naming parts of a structure. If we were using a hash-map to
represent our person:
(def h-person {:name "alan"
:age 30 })
;; de-structuring hash-maps looks like this:
(defn print-person3 [{:keys [name age]}]
(println name " - " age))
KRISHNA: There are libraries that allow for even more
compact and sophisticated de-structuring syntax, but let’s
leave it at that for de-structuring in Clojure.
BRAMHA: This is interesting, I am not aware of de-
structuring Maps in Scala, out-of-the-box. However, you can
de-structure Lists, Vectors and Streams in Scala. If Person was
a Vector like you showed in Clojure, I could do the following
val p = List("alan", 30)
def showPerson(p: List[Any]) = p match {
case List(name, age) => println(s"$name - $age")
}
showPerson(p) //alan - 30
This led to the first Code Jugalbandi between us. Code
Jugalbandi inspired and fuelled our learning by focusing
on dialogue and exploration. More languages in the
room meant more perspectives too. Exploring a paradigm
through many languages gave us a better map of the
territory than any single language could. Above all,
Code Jugalbandi was a playful way to learn together!
Healthy Code
April - 2015
12
Pattern Matching
BRAMHA: So far we’ve just talked about picking apart the
structure of things, but we can generalize de-structuring into
something even more powerful: Pattern Matching (not to be
confused with string pattern matching)
BRAMHA: Let’s explore this by example. Say we have a
journal with some fields.
case class Journal(op: String, amt: Double)
BRAMHA: Let’s say, we want to save the journal entries
like debit and credit.
def save(j: Journal) = j match {
case Journal("debit" , _) => println("Debit")
case Journal("credit", _) => println("Credit")
case Journal(_ , _) => println("Unknown")
}
save(Journal("debit", 23)) //Debit
save(Journal("credit", 10)) //Credit
save(Journal("transfer", 50)) //Unknown
KRISHNA: Does the underscore "_" here mean wildcard?
BRAMHA: Yes, it means that we don’t care about it and is
used as a placeholder during pattern matching.
BRAMHA: We get pattern matching in Scala for free by
usingcaseclasses.Scalacompilerimplicitlyaddsacompanion
object for the case class and provides an implementation for
the apply() method (basically factory method to create the
classes) and an unapply() method to get the constituents
back.
KRISHNA: Yes, I see, we want to pick only those journals
where the first value is debit or credit. So, you can’t do this
kind of thing in Clojure.
Pattern Matching - Clojure
KRISHNA: Idiomatic Clojure doesn’t have "Pattern
Matching" in that way. But, there is a way to achieve this
(there are libraries that implement sophisticated Pattern
Matching)
(def debit-journal [:debit 25.4])
(def credit-journal [:credit 26.5])
Then one can use multi-methods in Clojure like this.
(defmulti save-j (fn [j] (first j)))
(defmethod save-j :debit [j]
(println "DEBIT: " j))
(defmethod save-j :credit [j]
(println "CREDIT: " j))
(save-j debit-journal)
(save-j credit-journal)
KRISHNA: Multi-methods allow dispatching on the
arguments in a very flexible way.
Pattern Matching - Haskell
KRISHNA: In Haskell, this would have been expressed
much more directly.
data Journal = Journal String Float
save_j :: Journal -> IO()
save_j (Journal "debit" _ _)
= print ("Debit")
save_j (Journal "credit" _ _)
= print ("Credit")
save_j (Journal _ _ _)
= print ("Unknown")
save_j (Journal "debit" 20.0) —— Debit
save_j (Journal "credit" 100.0) —— Credit
save_j (Journal "transfer" 50.0) —— Unknown
KRISHNA: So, we use pattern matching when we define
a function, that is, embed pattern matching in function
definitions. However, you can also do case distinctions
explicitly, similar to Scala.
save_j :: Journal -> IO()
save_j jrnl = case jrnl of
Journal "debit" _
-> print ("Debit")
Journal "credit" _
-> print ("Credit")
Journal _ _
-> print ("Unknown")
KRISHNA: In Haskell, we can pattern match on tuples as
well.
showPerson :: (String, Int) -> IO()
showPerson (name, age)
= print (name ++ " - " ++ (show age))
showPerson ("alan", 30)
KRISHNA: In Haskell, pattern matching is a deep part of
the language and we have just scratched the surface here.
Reflections
KRISHNA: There is a difference between De-structuring
and Pattern Matching. In pattern matching, we’re not
only matching on relative position of elements in the data
structure, we’re trying to dispatch to different functions
based on a value inside the data structure! This dispatch is
a kind of conditional construct, while with de-structuring
there is nothing conditional about it.
Healthy Code
April - 2015
13
BRAMHA: In Clojure, we saw basic de-structuring, and
then more sophisticated pattern matching made possible
by Multi-method dispatch. In Scala and Haskell, de-
structuring and pattern matching are unified using
the case expressions.
KRISHNA: Pattern matching with all the case distinctions is
like generalization of switch statements in Java/C#, where we
used Ints, Enums etc., but we can now use class hierarchies
or data types. It so happens that the syntax is different.
Ryan is a software developer,
coach and advisor, based in Cape
Town. He has been working with
code for more than 15 years. Ryan
assists individuals and teams
to manage the evolution and
complexity of their software
systems. Ryan is a passionate
learner and enjoys facilitating
learning in others.
Dhavalahands-ondeveloperand
mentor, believes that software
development is first an art and
then a craft. For him writing
software brings his creativity
to the fore. His interests are in
architecting applications ground
up, estabilishing environments,
transitioning and orienting
teams to Agile way of working
by embedding himself within
teams.
You can follow them on Twitter @
CodeJugalbandi
“In the old days, people
robbed stagecoaches and
knocked off armored
trucks. Now they’re
knocking off servers.”
- Richard Power
“The inside of a computer
is as dumb as hell but it
goes like mad!”
- Richard Feynman
“The question of whether
computers can think is
just like the question
of whether submarines
can swim.”
- Edsger W. Dijkstra
Pragmatic
Programmer Quote

Más contenido relacionado

Similar a 4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015

The Geometry of Learning
The Geometry of LearningThe Geometry of Learning
The Geometry of Learning
fridolin.wild
 
Introducing natural language processing(NLP) with r
Introducing natural language processing(NLP) with rIntroducing natural language processing(NLP) with r
Introducing natural language processing(NLP) with r
Vivian S. Zhang
 

Similar a 4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015 (20)

CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-IssueCodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
 
DSLs for fun and profit by Jukka Välimaa
DSLs for fun and profit by Jukka VälimaaDSLs for fun and profit by Jukka Välimaa
DSLs for fun and profit by Jukka Välimaa
 
Poetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePoetry with R -- Dissecting the code
Poetry with R -- Dissecting the code
 
More expressive types for spark with frameless
More expressive types for spark with framelessMore expressive types for spark with frameless
More expressive types for spark with frameless
 
03 introduction to graph databases
03   introduction to graph databases03   introduction to graph databases
03 introduction to graph databases
 
Four Languages From Forty Years Ago
Four Languages From Forty Years AgoFour Languages From Forty Years Ago
Four Languages From Forty Years Ago
 
Beyond the Style Guides
Beyond the Style GuidesBeyond the Style Guides
Beyond the Style Guides
 
Seminar
SeminarSeminar
Seminar
 
SEMINAR
SEMINARSEMINAR
SEMINAR
 
Oo ps exam answer2
Oo ps exam answer2Oo ps exam answer2
Oo ps exam answer2
 
LISP: назад в будущее, Микола Мозговий
LISP: назад в будущее, Микола МозговийLISP: назад в будущее, Микола Мозговий
LISP: назад в будущее, Микола Мозговий
 
Why is Haskell so hard! (And how to deal with it?)
Why is Haskell so hard! (And how to deal with it?)Why is Haskell so hard! (And how to deal with it?)
Why is Haskell so hard! (And how to deal with it?)
 
The Geometry of Learning
The Geometry of LearningThe Geometry of Learning
The Geometry of Learning
 
Presentation on use of r statistics
Presentation on use of r statisticsPresentation on use of r statistics
Presentation on use of r statistics
 
Introducing natural language processing(NLP) with r
Introducing natural language processing(NLP) with rIntroducing natural language processing(NLP) with r
Introducing natural language processing(NLP) with r
 
Implementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with SlickImplementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with Slick
 
A fast introduction to PySpark with a quick look at Arrow based UDFs
A fast introduction to PySpark with a quick look at Arrow based UDFsA fast introduction to PySpark with a quick look at Arrow based UDFs
A fast introduction to PySpark with a quick look at Arrow based UDFs
 
Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)
 
Ch2
Ch2Ch2
Ch2
 
The good parts in scalaz
The good parts in scalazThe good parts in scalaz
The good parts in scalaz
 

Más de Dhaval Dalal

Más de Dhaval Dalal (20)

Test Pyramid in Microservices Context
Test Pyramid in Microservices ContextTest Pyramid in Microservices Context
Test Pyramid in Microservices Context
 
Code Retreat
Code RetreatCode Retreat
Code Retreat
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programming
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)
 
Creating Lazy stream in CSharp
Creating Lazy stream in CSharpCreating Lazy stream in CSharp
Creating Lazy stream in CSharp
 
Json Viewer Stories
Json Viewer StoriesJson Viewer Stories
Json Viewer Stories
 
Value Objects
Value ObjectsValue Objects
Value Objects
 
Mars rover-extension
Mars rover-extensionMars rover-extension
Mars rover-extension
 
How Is Homeopathy Near To Yoga?
How Is Homeopathy Near To Yoga?How Is Homeopathy Near To Yoga?
How Is Homeopathy Near To Yoga?
 
Approaching ATDD/BDD
Approaching ATDD/BDDApproaching ATDD/BDD
Approaching ATDD/BDD
 
Paradigms Code jugalbandi
Paradigms Code jugalbandiParadigms Code jugalbandi
Paradigms Code jugalbandi
 
Data Reconciliation
Data ReconciliationData Reconciliation
Data Reconciliation
 
DRYing to Monad in Java8
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8
 
CodeRetreat
CodeRetreatCodeRetreat
CodeRetreat
 
The tao-of-transformation-workshop
The tao-of-transformation-workshopThe tao-of-transformation-workshop
The tao-of-transformation-workshop
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
 
Language portfolio
Language portfolioLanguage portfolio
Language portfolio
 
Code jugalbandi
Code jugalbandiCode jugalbandi
Code jugalbandi
 
A case-for-graph-db
A case-for-graph-dbA case-for-graph-db
A case-for-graph-db
 
Transition contours
Transition contoursTransition contours
Transition contours
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015

  • 1. Healthy Code April - 2015 10 In 2013, Dhaval Dalal was inspired by Jugalbandi, an Indian classical music form in which musicians have an improvised musical conversation, each using a different instrument, while exploring a shared musical theme together akin to Jazz in some ways. Dhaval thought, “What if we could have a conversation about some programming concepts in this way? Instead of musical instruments, what if we use different programming languages?” Code Jugalbandi Article Ryan and Dhaval
  • 2. Healthy Code April - 2015 11 Creator and Listener We met regularly with over a period of several months, exploring some key issues related to Functional Programming(FP).ThisculminatedinaliveCodeJugalbandi at the Functional Conference in Bengaluru, 2014. During the FP Code Jugalbandi, we explored nine themes, which became melodies. Similar to the musical Jugalbandi, there are two roles: creator and listener. We called the creator of the melody Bramha and the one who listens and responds with a different instrument, Krishna. + In the last Code Jugalbandi Article, we looked at the melody, the Currying and Partial Function problem. This time we will look at De- Structuring and Pattern Matching. De-structuring BRAMHA: In FP, we favour simple data structures over classes, e.g. instead of a collection of people objects, we might have a collection of person tuples, and containing only the fields we’re interested in: //Tuple with 2 elements - name and age type Person = (String, Int) val p = ("alan", 30) BRAMHA: … and then we have some functions acting on this person data type. Whatever we’ll do with person, we’ll need code to get at the innards of the person tuple. def showPerson(p: Person) = p match { case (name, age) => println(s"$name - $age") } showPerson(p) //alan - 30 BRAMHA: How does this look in Clojure? KRISHNA: Most functional languages provide some level of de-structuring: (def a-person ["alan" 30]) (defn print-person [p] (println (first p) " " (second p))) (defn print-person2 [[name age]] (println name " - " age)) KRISHNA: De-structuring gives you a shorthand way of naming parts of a structure. If we were using a hash-map to represent our person: (def h-person {:name "alan" :age 30 }) ;; de-structuring hash-maps looks like this: (defn print-person3 [{:keys [name age]}] (println name " - " age)) KRISHNA: There are libraries that allow for even more compact and sophisticated de-structuring syntax, but let’s leave it at that for de-structuring in Clojure. BRAMHA: This is interesting, I am not aware of de- structuring Maps in Scala, out-of-the-box. However, you can de-structure Lists, Vectors and Streams in Scala. If Person was a Vector like you showed in Clojure, I could do the following val p = List("alan", 30) def showPerson(p: List[Any]) = p match { case List(name, age) => println(s"$name - $age") } showPerson(p) //alan - 30 This led to the first Code Jugalbandi between us. Code Jugalbandi inspired and fuelled our learning by focusing on dialogue and exploration. More languages in the room meant more perspectives too. Exploring a paradigm through many languages gave us a better map of the territory than any single language could. Above all, Code Jugalbandi was a playful way to learn together!
  • 3. Healthy Code April - 2015 12 Pattern Matching BRAMHA: So far we’ve just talked about picking apart the structure of things, but we can generalize de-structuring into something even more powerful: Pattern Matching (not to be confused with string pattern matching) BRAMHA: Let’s explore this by example. Say we have a journal with some fields. case class Journal(op: String, amt: Double) BRAMHA: Let’s say, we want to save the journal entries like debit and credit. def save(j: Journal) = j match { case Journal("debit" , _) => println("Debit") case Journal("credit", _) => println("Credit") case Journal(_ , _) => println("Unknown") } save(Journal("debit", 23)) //Debit save(Journal("credit", 10)) //Credit save(Journal("transfer", 50)) //Unknown KRISHNA: Does the underscore "_" here mean wildcard? BRAMHA: Yes, it means that we don’t care about it and is used as a placeholder during pattern matching. BRAMHA: We get pattern matching in Scala for free by usingcaseclasses.Scalacompilerimplicitlyaddsacompanion object for the case class and provides an implementation for the apply() method (basically factory method to create the classes) and an unapply() method to get the constituents back. KRISHNA: Yes, I see, we want to pick only those journals where the first value is debit or credit. So, you can’t do this kind of thing in Clojure. Pattern Matching - Clojure KRISHNA: Idiomatic Clojure doesn’t have "Pattern Matching" in that way. But, there is a way to achieve this (there are libraries that implement sophisticated Pattern Matching) (def debit-journal [:debit 25.4]) (def credit-journal [:credit 26.5]) Then one can use multi-methods in Clojure like this. (defmulti save-j (fn [j] (first j))) (defmethod save-j :debit [j] (println "DEBIT: " j)) (defmethod save-j :credit [j] (println "CREDIT: " j)) (save-j debit-journal) (save-j credit-journal) KRISHNA: Multi-methods allow dispatching on the arguments in a very flexible way. Pattern Matching - Haskell KRISHNA: In Haskell, this would have been expressed much more directly. data Journal = Journal String Float save_j :: Journal -> IO() save_j (Journal "debit" _ _) = print ("Debit") save_j (Journal "credit" _ _) = print ("Credit") save_j (Journal _ _ _) = print ("Unknown") save_j (Journal "debit" 20.0) —— Debit save_j (Journal "credit" 100.0) —— Credit save_j (Journal "transfer" 50.0) —— Unknown KRISHNA: So, we use pattern matching when we define a function, that is, embed pattern matching in function definitions. However, you can also do case distinctions explicitly, similar to Scala. save_j :: Journal -> IO() save_j jrnl = case jrnl of Journal "debit" _ -> print ("Debit") Journal "credit" _ -> print ("Credit") Journal _ _ -> print ("Unknown") KRISHNA: In Haskell, we can pattern match on tuples as well. showPerson :: (String, Int) -> IO() showPerson (name, age) = print (name ++ " - " ++ (show age)) showPerson ("alan", 30) KRISHNA: In Haskell, pattern matching is a deep part of the language and we have just scratched the surface here. Reflections KRISHNA: There is a difference between De-structuring and Pattern Matching. In pattern matching, we’re not only matching on relative position of elements in the data structure, we’re trying to dispatch to different functions based on a value inside the data structure! This dispatch is a kind of conditional construct, while with de-structuring there is nothing conditional about it.
  • 4. Healthy Code April - 2015 13 BRAMHA: In Clojure, we saw basic de-structuring, and then more sophisticated pattern matching made possible by Multi-method dispatch. In Scala and Haskell, de- structuring and pattern matching are unified using the case expressions. KRISHNA: Pattern matching with all the case distinctions is like generalization of switch statements in Java/C#, where we used Ints, Enums etc., but we can now use class hierarchies or data types. It so happens that the syntax is different. Ryan is a software developer, coach and advisor, based in Cape Town. He has been working with code for more than 15 years. Ryan assists individuals and teams to manage the evolution and complexity of their software systems. Ryan is a passionate learner and enjoys facilitating learning in others. Dhavalahands-ondeveloperand mentor, believes that software development is first an art and then a craft. For him writing software brings his creativity to the fore. His interests are in architecting applications ground up, estabilishing environments, transitioning and orienting teams to Agile way of working by embedding himself within teams. You can follow them on Twitter @ CodeJugalbandi “In the old days, people robbed stagecoaches and knocked off armored trucks. Now they’re knocking off servers.” - Richard Power “The inside of a computer is as dumb as hell but it goes like mad!” - Richard Feynman “The question of whether computers can think is just like the question of whether submarines can swim.” - Edsger W. Dijkstra Pragmatic Programmer Quote