10: introduction$to$functional$programmingumass-cs-220.github.io › weeks › 06 ›...
TRANSCRIPT
![Page 1: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/1.jpg)
CMPSCI 220 Programming Methodology
10: Introduction to Functional Programming
Based on Head First Design Patterns
![Page 2: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/2.jpg)
What is functional programming?
Functional Programming (FP)
The construction of programs using only pure functions
What is a pure function?
![Page 3: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/3.jpg)
What is a pure function?
A pure function is a function that…
has no side effects!
What is a side effect?
![Page 4: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/4.jpg)
What is a side effect?
A function has a side effect if it does something other than simply returning a result.
• Modifying a variable• Modifying a data structure in place• Setting a field on an object• Throwing an exception or halting with an error• Printing to the console or reading user input• Reading from or writing to a file• Drawing on the screen
![Page 5: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/5.jpg)
![Page 6: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/6.jpg)
![Page 7: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/7.jpg)
Programs without side effects?
Consider what programming would be like without the ability to do these things…
![Page 8: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/8.jpg)
Programs without side effects?
Consider what programming would be like without the ability to do these things…
Or, with significant restrictions on when and how these actions can occur…
![Page 9: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/9.jpg)
Programs without side effects?
Consider what programming would be like without the ability to do these things…
Or, with significant restrictions on when and how these actions can occur…
![Page 10: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/10.jpg)
Programs without side effects?
It may be difficult to
imagine…
![Page 11: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/11.jpg)
Programs without side effects?
How is it even possible to write useful programs at all?
![Page 12: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/12.jpg)
Programs without side effects?
How is it even possible to write useful programs at all?
If we can’t reassign variables,
![Page 13: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/13.jpg)
Programs without side effects?
How is it even possible to write useful programs at all?
If we can’t reassign variables,
• How would write simple programs with loops?
![Page 14: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/14.jpg)
Programs without side effects?
How is it even possible to write useful programs at all?
If we can’t reassign variables,
• How would write simple programs with loops?
• Handle data that changes?
![Page 15: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/15.jpg)
Programs without side effects?
How is it even possible to write useful programs at all?
If we can’t reassign variables,
• How would write simple programs with loops?
• Handle data that changes?• Handle errors/exceptions?• Performing IO?
![Page 16: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/16.jpg)
The answer…
Functional programming is a restriction on how we write programs…
![Page 17: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/17.jpg)
The answer…
Functional programming is a restriction on how we write programs…
But, not on whatprograms we can express.
![Page 18: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/18.jpg)
The answer…
Functional programming is a restriction on how we write programs…
But, not on whatprograms we can express.
FP is a programming paradigm – a pattern in how you write programs without side effects.
![Page 19: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/19.jpg)
What does FP give us?
FP is tremendously beneficial because of the increase in modularity.
Pure functions are easier to…• Test• Reuse• Parallelize• Generalize• Reason About
![Page 20: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/20.jpg)
What does FP give us?
FP is tremendously beneficial because of the increase in modularity.
Pure functions are easier to…• Test• Reuse• Parallelize• Generalize• Reason About
Much less prone to bugs!
![Page 21: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/21.jpg)
A program with side effects
1 class Cafe {2 def buyCoffee(cc: CreditCard): Coffee = {3 val cup = new Coffee()4 cc.charge(cup.price)5 cup6 }7 }
Where is the side effect?
a) line 3b) line 4c) line 5d) none of these
![Page 22: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/22.jpg)
A program with side effects
1 class Cafe {2 def buyCoffee(cc: CreditCard): Coffee = {3 val cup = new Coffee()4 cc.charge(cup.price)5 cup6 }7 }
Where is the side effect?
a) line 3b) line 4c) line 5d) none of these
![Page 23: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/23.jpg)
A program with side effects
1 class Cafe {2 def buyCoffee(cc: CreditCard): Coffee = {3 val cup = new Coffee()4 cc.charge(cup.price)5 cup6 }7 }
Where is the side effect?
As a result of this side effect, the code is difficult to test.
Why?
![Page 24: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/24.jpg)
A program that is better?
1 class Cafe {2 def buyCoffee(cc: CreditCard, p: Payments): Coffee = {3 val cup = new Coffee()4 p.charge(cc, cup.price)5 cup6 }7 }
This is a little better…Why?
But, we still have issues…
![Page 25: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/25.jpg)
A program that is better?
1 class Cafe {2 def buyCoffee(cc: CreditCard, p: Payments): Coffee = {3 val cup = new Coffee()4 p.charge(cc, cup.price)5 cup6 }7 }
We could create a “mock” implementation, but we are likely to modify some internal state. Additionally, creating an interface just so we can test a function is overkill.
![Page 26: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/26.jpg)
A program that is better?
1 class Cafe {2 def buyCoffee(cc: CreditCard, p: Payments): Coffee = {3 val cup = new Coffee()4 p.charge(cc, cup.price)5 cup6 }7 } Furthermore, it is difficult to reuse buyCoffee.
Why?
Imagine a customer orders 12 cups of coffee.How might we reuse buyCoffee?
![Page 27: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/27.jpg)
A program that is better?
How can we fix this?
1 class Cafe {2 def buyCoffee(cc: CreditCard, p: Payments): Coffee = {3 val cup = new Coffee()4 p.charge(cc, cup.price)5 cup6 }7 }
![Page 28: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/28.jpg)
A program that is better?
buyCoffee
Credit Card Cup
Credit Card Server
side effectsend
transaction
Can’t test buyCoffee without credit card server.
Can’t combine two transactions into one.
![Page 29: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/29.jpg)
A program that is better?
buyCoffee
Credit Card Cup
Credit Card Server
side effectsend
transaction
buyCoffee
Credit Card Cup
Charge
If buyCoffee returns a charge object instead of performing a side effect, then…
![Page 30: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/30.jpg)
A program that is better?
buyCoffee
Credit Card Cup
Credit Card Server
side effectsend
transaction
buyCoffee
Credit Card Cup
Charge
list of charges
Coalesce
Charge
![Page 31: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/31.jpg)
The functional solution is to eliminate side effects
buyCoffee returns the charge as a value in addition to returning the Coffee.
buyCoffee
Credit Card Cup
Charge
![Page 32: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/32.jpg)
A functional solution.
buyCoffee now returns a pair of a Coffee and a Charge.
class Cafe {def buyCoffee(cc: CreditCard): (Coffee, Charge) = {
val cup = new Coffee()(cup, Charge(cc, cup.price))
}}
![Page 33: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/33.jpg)
A functional solution.
buyCoffee now returns a pair of a Coffee and a Charge.
Here we have separated the concern of creating a charge from the processing or interpretation of that charge. 0 side effects.
class Cafe {def buyCoffee(cc: CreditCard): (Coffee, Charge) = {
val cup = new Coffee()(cup, Charge(cc, cup.price))
}}
![Page 34: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/34.jpg)
A functional solution.
How can we reuse this version to easily purchase multiple coffees with a single transaction?
What is a Charge?
class Cafe {def buyCoffee(cc: CreditCard): (Coffee, Charge) = {
val cup = new Coffee()(cup, Charge(cc, cup.price))
}}
![Page 35: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/35.jpg)
A Charge Class
case class Charge(cc: CreditCard, amount: Double) {def combine(other: Charge): Charge =
if (cc == other.cc) Charge(cc, amount + other.amount)else
throw new Exception(“Can’t combine different cards”)}
What is a case class?
A case class defines an algebraic data type.It combines a class and a companion object into one.Even better – it allows pattern matching.
![Page 36: 10: Introduction$to$Functional$Programmingumass-cs-220.github.io › weeks › 06 › 10-intro-fp.pdf · CMPSCI&220&Programming&Methodology 10: Introduction$to$Functional$Programming](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c5ba57e708231d435012f/html5/thumbnails/36.jpg)
Buying multiple cups…class Cafe {
def buyCoffee(cc: CreditCard): (Coffee, Charge) = …
def buyCoffees(cc: CreditCard, n: Int): (List[Coffee], Charge) = {
val purchases: List[(Coffee, Charge)] = List.fill(n)(buyCoffee(cc))
val (coffees, charges) = purchases.unzip
(coffees, charges.reduce((c1,c2) => c1.combine(c2)))}
}