functional programming with immutable data structures
DESCRIPTION
author: Ivar Thorsongreat slide!!! congratulations.TRANSCRIPT
![Page 1: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/1.jpg)
Functional Programming withImmutable Data Structures
Why Imperative Languages are
Fundamentally Broken in a
Multi-Threaded Environment
Ivar Thorson
Italian Institute of Technology
November 2, 2010
![Page 2: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/2.jpg)
Ivar Thorson
![Page 3: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/3.jpg)
![Page 4: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/4.jpg)
Research interests:
![Page 5: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/5.jpg)
Compliant actuation
![Page 6: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/6.jpg)
Hopping Robots
![Page 7: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/7.jpg)
Rigid Body Dynamics Simulations
![Page 8: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/8.jpg)
The Next 37 minutes:
![Page 9: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/9.jpg)
Important abstractions of functional
programming
![Page 10: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/10.jpg)
particularly for a 4-year old language
![Page 11: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/11.jpg)
![Page 12: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/12.jpg)
Rich Hickey
![Page 13: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/13.jpg)
What I have learned from his work
![Page 14: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/14.jpg)
Clojure: Blending theoretical abstractions +
practical know-how
![Page 15: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/15.jpg)
Target Audience: C, C++, Java, Matlab
Programmers
![Page 16: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/16.jpg)
Tempting to start with a feature tour!
![Page 17: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/17.jpg)
But you won’t understand why Clojure is
cool without context
![Page 18: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/18.jpg)
![Page 19: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/19.jpg)
![Page 20: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/20.jpg)
Actually, I’m going to try to knock the cup
out of your hand.
![Page 21: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/21.jpg)
Three Outdated Concepts
1. Variables
2. Syntax
3. Object Orientation
![Page 22: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/22.jpg)
Three Outdated Concepts
1. Variables
2. Syntax
3. Object Orientation
![Page 23: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/23.jpg)
Three Outdated Concepts
1. Variables
2. Syntax
3. Object Orientation
![Page 24: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/24.jpg)
Three Outdated Concepts
1. Variables
2. Syntax
3. Object Orientation
![Page 25: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/25.jpg)
Goal is to convince you that
1. shared mutable data is now a
philosophically bankrupt idea.
2. code and data should be structured as
trees
3. OOP isn’t the best way to achieve
OOP’s goals
![Page 26: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/26.jpg)
Goal is to convince you that
1. shared mutable data is now a
philosophically bankrupt idea.
2. code and data should be structured as
trees
3. OOP isn’t the best way to achieve
OOP’s goals
![Page 27: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/27.jpg)
Goal is to convince you that
1. shared mutable data is now a
philosophically bankrupt idea.
2. code and data should be structured as
trees
3. OOP isn’t the best way to achieve
OOP’s goals
![Page 28: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/28.jpg)
Goal is to convince you that
1. shared mutable data is now a
philosophically bankrupt idea.
2. code and data should be structured as
trees
3. OOP isn’t the best way to achieve
OOP’s goals
![Page 29: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/29.jpg)
Speaking bluntly
1. everything you know is wrong (15 min)
2. lisp parentheses are better than syntax
(10 min)
3. OOP inheritance sucks (5 min)
![Page 30: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/30.jpg)
Speaking bluntly
1. everything you know is wrong (15 min)
2. lisp parentheses are better than syntax
(10 min)
3. OOP inheritance sucks (5 min)
![Page 31: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/31.jpg)
Speaking bluntly
1. everything you know is wrong (15 min)
2. lisp parentheses are better than syntax
(10 min)
3. OOP inheritance sucks (5 min)
![Page 32: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/32.jpg)
Speaking bluntly
1. everything you know is wrong (15 min)
2. lisp parentheses are better than syntax
(10 min)
3. OOP inheritance sucks (5 min)
![Page 33: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/33.jpg)
disclaimer: some hyperbole in previous
statements
![Page 34: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/34.jpg)
Oh noes, too many parentheses!
![Page 35: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/35.jpg)
Good reasons for parentheses
1. Lisp is homoiconic
2. Parentheses uniquely define tree-shaped
computations
3. Parentheses enable structural editing
![Page 36: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/36.jpg)
Good reasons for parentheses
1. Lisp is homoiconic
2. Parentheses uniquely define tree-shaped
computations
3. Parentheses enable structural editing
![Page 37: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/37.jpg)
Good reasons for parentheses
1. Lisp is homoiconic
2. Parentheses uniquely define tree-shaped
computations
3. Parentheses enable structural editing
![Page 38: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/38.jpg)
Good reasons for parentheses
1. Lisp is homoiconic
2. Parentheses uniquely define tree-shaped
computations
3. Parentheses enable structural editing
![Page 39: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/39.jpg)
For now, please be patient
![Page 40: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/40.jpg)
Introduction: Motivation for multi-threaded
programming
![Page 41: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/41.jpg)
1. Last 40 years: Moore’s Law
2. “Transistor count will double every 2
years”
![Page 42: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/42.jpg)
1. Last 40 years: Moore’s Law
2. “Transistor count will double every 2
years”
![Page 43: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/43.jpg)
1. Last 40 years: Moore’s Law
2. “Transistor count will double every 2
years”
![Page 44: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/44.jpg)
# of transistors ≈ CPU performance
![Page 45: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/45.jpg)
![Page 46: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/46.jpg)
Constraining physical relationship between
power density, swiching time, oxide thickness
![Page 47: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/47.jpg)
![Page 48: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/48.jpg)
The future of hardware is increasingly parallel
![Page 49: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/49.jpg)
The future of software will be ruled by
Amdahl’s law
![Page 50: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/50.jpg)
![Page 51: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/51.jpg)
Some things are sequential: Two women
cannot have a baby in 4.5 months.
![Page 52: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/52.jpg)
1. Dividing up work is already a hard
design task
2. Resource contention makes this problem
harder
![Page 53: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/53.jpg)
1. Dividing up work is already a hard
design task
2. Resource contention makes this problem
harder
![Page 54: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/54.jpg)
1. Dividing up work is already a hard
design task
2. Resource contention makes this problem
harder
![Page 55: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/55.jpg)
Common multi-threaded bugs:
I Invalid state
I Race conditions
I Deadlocks
I Livelocks
I Resource starvation
![Page 56: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/56.jpg)
Common multi-threaded bugs:
I Invalid state
I Race conditions
I Deadlocks
I Livelocks
I Resource starvation
![Page 57: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/57.jpg)
Common multi-threaded bugs:
I Invalid state
I Race conditions
I Deadlocks
I Livelocks
I Resource starvation
![Page 58: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/58.jpg)
Common multi-threaded bugs:
I Invalid state
I Race conditions
I Deadlocks
I Livelocks
I Resource starvation
![Page 59: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/59.jpg)
Common multi-threaded bugs:
I Invalid state
I Race conditions
I Deadlocks
I Livelocks
I Resource starvation
![Page 60: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/60.jpg)
Common multi-threaded bugs:
I Invalid state
I Race conditions
I Deadlocks
I Livelocks
I Resource starvation
![Page 61: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/61.jpg)
What if most bugs were merely due to the
imperative programming model?
![Page 62: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/62.jpg)
Part 1: The Functional Programming Style
![Page 63: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/63.jpg)
![Page 64: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/64.jpg)
Pure functions always return the same result
when they get the same input.
![Page 65: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/65.jpg)
Pure functions don’t...
I ...look outside their box
I ...modify anything, anywhere
I ...print messages to the user
I ...write to disk
![Page 66: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/66.jpg)
Pure functions don’t...
I ...look outside their box
I ...modify anything, anywhere
I ...print messages to the user
I ...write to disk
![Page 67: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/67.jpg)
Pure functions don’t...
I ...look outside their box
I ...modify anything, anywhere
I ...print messages to the user
I ...write to disk
![Page 68: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/68.jpg)
Pure functions don’t...
I ...look outside their box
I ...modify anything, anywhere
I ...print messages to the user
I ...write to disk
![Page 69: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/69.jpg)
Pure functions don’t...
I ...look outside their box
I ...modify anything, anywhere
I ...print messages to the user
I ...write to disk
![Page 70: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/70.jpg)
Pure functions have no side effects
![Page 71: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/71.jpg)
Nothing would change if you ran the function
again – anywhere!
![Page 72: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/72.jpg)
f (x) = x2 + 1
![Page 73: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/73.jpg)
Pure functions just return a value, and do
nothing more.
![Page 74: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/74.jpg)
Pure functions compose to other pure
functions
![Page 75: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/75.jpg)
![Page 76: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/76.jpg)
f (a, b, c) = (a + b)/(c ∗ 2)
![Page 77: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/77.jpg)
Languages that emphasize the use of pure
functions are called functional languages
![Page 78: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/78.jpg)
Imperative languages describe computation
in terms of changes to state.
![Page 79: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/79.jpg)
C, C++, Java, and most engineering
languages are imperative.
![Page 80: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/80.jpg)
Imperative languages describe memory
operations instead of purely functional
operations.
![Page 81: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/81.jpg)
![Page 82: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/82.jpg)
Imperative style: directly causing side effects
on memory.
![Page 83: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/83.jpg)
The assignment operator changes
memory...often a side effect!
![Page 84: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/84.jpg)
Could you write a C program...
I without any non-local variables?
I where = is only used for initialization?
![Page 85: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/85.jpg)
Could you write a C program...
I without any non-local variables?
I where = is only used for initialization?
![Page 86: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/86.jpg)
Could you write a C program...
I without any non-local variables?
I where = is only used for initialization?
![Page 87: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/87.jpg)
Next: Variables are fundamentally a bad
abstraction in multithreaded environments.
![Page 88: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/88.jpg)
Claim #1. Shared mutable data is now
philosophically bankrupt
![Page 89: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/89.jpg)
I x = x + 1
I x = 3 (...last time I checked!)
I In my universe, 3 = 3 + 1 is never true
![Page 90: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/90.jpg)
I x = x + 1
I x = 3 (...last time I checked!)
I In my universe, 3 = 3 + 1 is never true
![Page 91: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/91.jpg)
I x = x + 1
I x = 3 (...last time I checked!)
I In my universe, 3 = 3 + 1 is never true
![Page 92: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/92.jpg)
I x = x + 1
I x = 3 (...last time I checked!)
I In my universe, 3 = 3 + 1 is never true
![Page 93: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/93.jpg)
The Big Problem: the concept of variables
encourage us to forget about time.
![Page 94: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/94.jpg)
x [t] = x0
x [t + 1] = x [t] + 1
![Page 95: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/95.jpg)
The value of x for a given t is immutable
and unchanging!
![Page 96: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/96.jpg)
The Most Important Slide
I x is a name, an identity of a sequence of
values
I x has different values at different times
I The values of x are related by pure
functions
I In this case, by the increment function
![Page 97: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/97.jpg)
The Most Important Slide
I x is a name, an identity of a sequence of
values
I x has different values at different times
I The values of x are related by pure
functions
I In this case, by the increment function
![Page 98: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/98.jpg)
The Most Important Slide
I x is a name, an identity of a sequence of
values
I x has different values at different times
I The values of x are related by pure
functions
I In this case, by the increment function
![Page 99: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/99.jpg)
The Most Important Slide
I x is a name, an identity of a sequence of
values
I x has different values at different times
I The values of x are related by pure
functions
I In this case, by the increment function
![Page 100: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/100.jpg)
The Most Important Slide
I x is a name, an identity of a sequence of
values
I x has different values at different times
I The values of x are related by pure
functions
I In this case, by the increment function
![Page 101: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/101.jpg)
![Page 102: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/102.jpg)
![Page 103: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/103.jpg)
The idea of a variable confuses identity and
the most current value!
![Page 104: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/104.jpg)
![Page 105: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/105.jpg)
Locking: a tactic for winning a battle.
![Page 106: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/106.jpg)
What we need is a strategy to win the war.
![Page 107: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/107.jpg)
“What if all data was immutable?” – Rich
Hickey (not the first one to ask this question)
![Page 108: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/108.jpg)
Keeping Old Immutable Data
I x@(t=0) → 5
I x@(t=1) → 6
I x@(t=13) → 7
I x@(t=15) → 8
I ...
![Page 109: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/109.jpg)
Keeping Old Immutable Data
I x@(t=0) → 5
I x@(t=1) → 6
I x@(t=13) → 7
I x@(t=15) → 8
I ...
![Page 110: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/110.jpg)
Keeping Old Immutable Data
I x@(t=0) → 5
I x@(t=1) → 6
I x@(t=13) → 7
I x@(t=15) → 8
I ...
![Page 111: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/111.jpg)
Keeping Old Immutable Data
I x@(t=0) → 5
I x@(t=1) → 6
I x@(t=13) → 7
I x@(t=15) → 8
I ...
![Page 112: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/112.jpg)
Keeping Old Immutable Data
I x@(t=0) → 5
I x@(t=1) → 6
I x@(t=13) → 7
I x@(t=15) → 8
I ...
![Page 113: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/113.jpg)
Keeping Old Immutable Data
I x@(t=0) → 5
I x@(t=1) → 6
I x@(t=13) → 7
I x@(t=15) → 8
I ...
![Page 114: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/114.jpg)
I The old values of the data are kept and
indexed by time
I Data is immutable once created – we
cannot/will not change it!
I Values only destroyed when unneeded
![Page 115: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/115.jpg)
I The old values of the data are kept and
indexed by time
I Data is immutable once created – we
cannot/will not change it!
I Values only destroyed when unneeded
![Page 116: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/116.jpg)
I The old values of the data are kept and
indexed by time
I Data is immutable once created – we
cannot/will not change it!
I Values only destroyed when unneeded
![Page 117: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/117.jpg)
I The old values of the data are kept and
indexed by time
I Data is immutable once created – we
cannot/will not change it!
I Values only destroyed when unneeded
![Page 118: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/118.jpg)
Doesn’t keeping old copies of data consume
too much memory?
![Page 119: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/119.jpg)
I (a b c) + d = (a b c d)
I What if the input and output shared
structure?
I Sharing structure is dangerous for
mutable data
I ...but sharing structure is safe if the
data is immutable.
![Page 120: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/120.jpg)
I (a b c) + d = (a b c d)
I What if the input and output shared
structure?
I Sharing structure is dangerous for
mutable data
I ...but sharing structure is safe if the
data is immutable.
![Page 121: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/121.jpg)
I (a b c) + d = (a b c d)
I What if the input and output shared
structure?
I Sharing structure is dangerous for
mutable data
I ...but sharing structure is safe if the
data is immutable.
![Page 122: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/122.jpg)
I (a b c) + d = (a b c d)
I What if the input and output shared
structure?
I Sharing structure is dangerous for
mutable data
I ...but sharing structure is safe if the
data is immutable.
![Page 123: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/123.jpg)
I (a b c) + d = (a b c d)
I What if the input and output shared
structure?
I Sharing structure is dangerous for
mutable data
I ...but sharing structure is safe if the
data is immutable.
![Page 124: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/124.jpg)
The Trick: Represent the list as a tree
![Page 125: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/125.jpg)
input tree → pure function → output tree
![Page 126: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/126.jpg)
![Page 127: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/127.jpg)
both trees are immutable but distinct
![Page 128: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/128.jpg)
Same approach works also for insertions,
modifications, deletions, and all other list
operations.
![Page 129: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/129.jpg)
a million threads, a million trees, a million
references to trees, zero locks
![Page 130: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/130.jpg)
If you want a more current worldview, just
get its reference
![Page 131: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/131.jpg)
Advantages of immutable trees:
I No locking required
I No ’stopping the world’ to see (readers
don’t block writers)
I Worldview never becomes corrupted
I Minimizes memory use while
maintaining multiple copies
I Unused nodes are garbage-collected
![Page 132: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/132.jpg)
Advantages of immutable trees:
I No locking required
I No ’stopping the world’ to see (readers
don’t block writers)
I Worldview never becomes corrupted
I Minimizes memory use while
maintaining multiple copies
I Unused nodes are garbage-collected
![Page 133: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/133.jpg)
Advantages of immutable trees:
I No locking required
I No ’stopping the world’ to see (readers
don’t block writers)
I Worldview never becomes corrupted
I Minimizes memory use while
maintaining multiple copies
I Unused nodes are garbage-collected
![Page 134: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/134.jpg)
Advantages of immutable trees:
I No locking required
I No ’stopping the world’ to see (readers
don’t block writers)
I Worldview never becomes corrupted
I Minimizes memory use while
maintaining multiple copies
I Unused nodes are garbage-collected
![Page 135: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/135.jpg)
Advantages of immutable trees:
I No locking required
I No ’stopping the world’ to see (readers
don’t block writers)
I Worldview never becomes corrupted
I Minimizes memory use while
maintaining multiple copies
I Unused nodes are garbage-collected
![Page 136: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/136.jpg)
Advantages of immutable trees:
I No locking required
I No ’stopping the world’ to see (readers
don’t block writers)
I Worldview never becomes corrupted
I Minimizes memory use while
maintaining multiple copies
I Unused nodes are garbage-collected
![Page 137: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/137.jpg)
We’ve gone a long way, but we’re only half
way to real concurrency
![Page 138: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/138.jpg)
![Page 139: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/139.jpg)
Immutability lets us read concurrently, but
not write concurrently to a single piece of
data
![Page 140: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/140.jpg)
![Page 141: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/141.jpg)
![Page 142: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/142.jpg)
How can we coordinate the actions of
different threads working on the same data
at the same time?
![Page 143: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/143.jpg)
”Treat changes in an identity’s value as a
database transaction.” – Rich Hickey
![Page 144: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/144.jpg)
Database Details:
I Software Transactional Memory (STM)
I Multi-Version Concurrency Control
(MVCC)
![Page 145: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/145.jpg)
Database Details:
I Software Transactional Memory (STM)
I Multi-Version Concurrency Control
(MVCC)
![Page 146: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/146.jpg)
Database Details:
I Software Transactional Memory (STM)
I Multi-Version Concurrency Control
(MVCC)
![Page 147: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/147.jpg)
The STM Guarantees:
I Atomicity (All or nothing)
I Consistency (Validation before commits)
I Isolation (Transactions can’t see each
other)
![Page 148: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/148.jpg)
The STM Guarantees:
I Atomicity (All or nothing)
I Consistency (Validation before commits)
I Isolation (Transactions can’t see each
other)
![Page 149: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/149.jpg)
The STM Guarantees:
I Atomicity (All or nothing)
I Consistency (Validation before commits)
I Isolation (Transactions can’t see each
other)
![Page 150: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/150.jpg)
The STM Guarantees:
I Atomicity (All or nothing)
I Consistency (Validation before commits)
I Isolation (Transactions can’t see each
other)
![Page 151: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/151.jpg)
![Page 152: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/152.jpg)
Transactions are speculative and may be
retried if there is a collision.
![Page 153: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/153.jpg)
For even more concurrency:
I Sometimes you don’t care about the
order of function application
I Commutative writers won’t need to retry
I Writers don’t interfere with other
writers!
![Page 154: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/154.jpg)
For even more concurrency:
I Sometimes you don’t care about the
order of function application
I Commutative writers won’t need to retry
I Writers don’t interfere with other
writers!
![Page 155: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/155.jpg)
For even more concurrency:
I Sometimes you don’t care about the
order of function application
I Commutative writers won’t need to retry
I Writers don’t interfere with other
writers!
![Page 156: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/156.jpg)
For even more concurrency:
I Sometimes you don’t care about the
order of function application
I Commutative writers won’t need to retry
I Writers don’t interfere with other
writers!
![Page 157: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/157.jpg)
I STMs exist for other languages...
I ... but Clojure is first to have built-in
STM with pervasive immutability
![Page 158: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/158.jpg)
I STMs exist for other languages...
I ... but Clojure is first to have built-in
STM with pervasive immutability
![Page 159: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/159.jpg)
I STMs exist for other languages...
I ... but Clojure is first to have built-in
STM with pervasive immutability
![Page 160: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/160.jpg)
Part 1 Summary: In Clojure...
I ...Readers don’t block anybody
I ...Writers don’t block anybody
I ...Writers retry if conflicting
I ...Writers don’t retry if commutative
![Page 161: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/161.jpg)
Part 1 Summary: In Clojure...
I ...Readers don’t block anybody
I ...Writers don’t block anybody
I ...Writers retry if conflicting
I ...Writers don’t retry if commutative
![Page 162: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/162.jpg)
Part 1 Summary: In Clojure...
I ...Readers don’t block anybody
I ...Writers don’t block anybody
I ...Writers retry if conflicting
I ...Writers don’t retry if commutative
![Page 163: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/163.jpg)
Part 1 Summary: In Clojure...
I ...Readers don’t block anybody
I ...Writers don’t block anybody
I ...Writers retry if conflicting
I ...Writers don’t retry if commutative
![Page 164: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/164.jpg)
Part 1 Summary: In Clojure...
I ...Readers don’t block anybody
I ...Writers don’t block anybody
I ...Writers retry if conflicting
I ...Writers don’t retry if commutative
![Page 165: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/165.jpg)
Variables couldn’t do this because:
I Confuse identity and values
I Are mutable and can be corrupted
I Assume a single thread of control, no
interruptions
I Maintain only the last written copy
![Page 166: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/166.jpg)
Variables couldn’t do this because:
I Confuse identity and values
I Are mutable and can be corrupted
I Assume a single thread of control, no
interruptions
I Maintain only the last written copy
![Page 167: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/167.jpg)
Variables couldn’t do this because:
I Confuse identity and values
I Are mutable and can be corrupted
I Assume a single thread of control, no
interruptions
I Maintain only the last written copy
![Page 168: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/168.jpg)
Variables couldn’t do this because:
I Confuse identity and values
I Are mutable and can be corrupted
I Assume a single thread of control, no
interruptions
I Maintain only the last written copy
![Page 169: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/169.jpg)
Variables couldn’t do this because:
I Confuse identity and values
I Are mutable and can be corrupted
I Assume a single thread of control, no
interruptions
I Maintain only the last written copy
![Page 170: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/170.jpg)
”Mutable stateful objects are the new
spaghetti code” – Rich Hickey
![Page 171: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/171.jpg)
”We oppose the uncontrolled mutation of
variables.” – Stuart Halloway
![Page 172: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/172.jpg)
”Mutable objects are a concurrency
disaster.” – Rich Hickey
![Page 173: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/173.jpg)
”The future is a function of the past, but
doesn’t change it.” – Rich Hickey
![Page 174: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/174.jpg)
”Many people can watch a baseball game,
but only one can be at bat.” – Rich Hickey
![Page 175: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/175.jpg)
Part 2: Revenge of the Lisp
![Page 176: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/176.jpg)
Claim #2: Your language’s syntax is
unneccesarily complex
![Page 177: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/177.jpg)
Now we’ll explain why lisp has parentheses!
![Page 178: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/178.jpg)
![Page 179: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/179.jpg)
Pure functions represent computation as
trees
![Page 180: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/180.jpg)
Reason 1: The tree structure is made
explicitly visible by the parentheses
![Page 181: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/181.jpg)
Sorry, Haskell/Erlang/OCaml/ML/etc!
![Page 182: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/182.jpg)
Infix Notation
1 + 1
![Page 183: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/183.jpg)
Prefix Notation
(+ 1 1)
![Page 184: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/184.jpg)
(fn arg1 arg2 arg3 ...)
![Page 185: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/185.jpg)
1 + 2 + 3 + 4
![Page 186: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/186.jpg)
(+ 1 2 3 4)
![Page 187: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/187.jpg)
With prefix notation, you can forget about
the rules of precedence.
![Page 188: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/188.jpg)
6 + 12 / 2 * 3
![Page 189: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/189.jpg)
(6 + 12) / (2 * 3)
![Page 190: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/190.jpg)
(/ (+ 6 12) (* 2 3))
![Page 191: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/191.jpg)
(/ (+ 6p
12)
(* 2
3))
![Page 192: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/192.jpg)
Triangular Tree Structure
![Page 193: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/193.jpg)
![Page 194: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/194.jpg)
Lisp code’s tree of computation is
exceptionally visible and regular.
![Page 195: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/195.jpg)
Reason 2: Homoiconicity
![Page 196: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/196.jpg)
Homoiconic = Homo + icon = ”same” +
”representation”
![Page 197: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/197.jpg)
The property where code & a language
primitive look the same.
![Page 198: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/198.jpg)
An example: Writing C with XML
![Page 199: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/199.jpg)
for (i=0; i<100; i++) {
printf("%d\n", i);
dostuff();
}
![Page 200: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/200.jpg)
<for>
<init>i = 0</init>
<test>i < 100</test>
<count>i++</count>
<body>
<print format="%d" args="i"/>
<dostuff/>
</body>
</for>
![Page 201: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/201.jpg)
Imagine how simple it would be to use an
XML generator to emit compilable source
code.
![Page 202: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/202.jpg)
We could modify our code programmatically.
![Page 203: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/203.jpg)
In lisp, you can write programs that write
programs.
![Page 204: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/204.jpg)
(list 1 2 3) -> (1 2 3)
![Page 205: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/205.jpg)
(list ’+ 1 2 3) -> (+ 1 2 3)
(+ 1 2 3) -> 6
![Page 206: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/206.jpg)
(defmacro and
([] true)
([x] x)
([x & rest]
‘(let [and# ~x]
(if and# (and ~@rest) and#))))
![Page 207: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/207.jpg)
Why lispers go nuts:
I Macros in lisp are far more powerful
than in other languages.
I You can build new constructs that are
just as legitimate as existing constructs
like if
I You can abstract away boilerplate code
![Page 208: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/208.jpg)
Why lispers go nuts:
I Macros in lisp are far more powerful
than in other languages.
I You can build new constructs that are
just as legitimate as existing constructs
like if
I You can abstract away boilerplate code
![Page 209: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/209.jpg)
Why lispers go nuts:
I Macros in lisp are far more powerful
than in other languages.
I You can build new constructs that are
just as legitimate as existing constructs
like if
I You can abstract away boilerplate code
![Page 210: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/210.jpg)
Why lispers go nuts:
I Macros in lisp are far more powerful
than in other languages.
I You can build new constructs that are
just as legitimate as existing constructs
like if
I You can abstract away boilerplate code
![Page 211: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/211.jpg)
Aside
I If Java used parentheses properly, XML
wouldn’t exist
I Lisp parentheses describe structure
I Most languages use ad-hoc syntax, data
formats
I Simplicity is elegance
![Page 212: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/212.jpg)
Aside
I If Java used parentheses properly, XML
wouldn’t exist
I Lisp parentheses describe structure
I Most languages use ad-hoc syntax, data
formats
I Simplicity is elegance
![Page 213: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/213.jpg)
Aside
I If Java used parentheses properly, XML
wouldn’t exist
I Lisp parentheses describe structure
I Most languages use ad-hoc syntax, data
formats
I Simplicity is elegance
![Page 214: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/214.jpg)
Aside
I If Java used parentheses properly, XML
wouldn’t exist
I Lisp parentheses describe structure
I Most languages use ad-hoc syntax, data
formats
I Simplicity is elegance
![Page 215: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/215.jpg)
Aside
I If Java used parentheses properly, XML
wouldn’t exist
I Lisp parentheses describe structure
I Most languages use ad-hoc syntax, data
formats
I Simplicity is elegance
![Page 216: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/216.jpg)
Reason 3: Structural Editing
![Page 217: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/217.jpg)
Good editors let you work with code in
blocks and forget about the parentheses.
![Page 218: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/218.jpg)
Hard-to-show Examples
I When you type (, emacs adds the )
I Indentation is automatic
I You can easily navigate heirarchically
I Take next three expressions, apply them
to a function
![Page 219: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/219.jpg)
Hard-to-show Examples
I When you type (, emacs adds the )
I Indentation is automatic
I You can easily navigate heirarchically
I Take next three expressions, apply them
to a function
![Page 220: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/220.jpg)
Hard-to-show Examples
I When you type (, emacs adds the )
I Indentation is automatic
I You can easily navigate heirarchically
I Take next three expressions, apply them
to a function
![Page 221: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/221.jpg)
Hard-to-show Examples
I When you type (, emacs adds the )
I Indentation is automatic
I You can easily navigate heirarchically
I Take next three expressions, apply them
to a function
![Page 222: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/222.jpg)
Hard-to-show Examples
I When you type (, emacs adds the )
I Indentation is automatic
I You can easily navigate heirarchically
I Take next three expressions, apply them
to a function
![Page 223: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/223.jpg)
Summary of Lisp Parentheses
I Parentheses render explicit the
tree-structure of your program
I Homoiconicity lets you write programs
with programs
I Structural Editing is fun and easy
![Page 224: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/224.jpg)
Summary of Lisp Parentheses
I Parentheses render explicit the
tree-structure of your program
I Homoiconicity lets you write programs
with programs
I Structural Editing is fun and easy
![Page 225: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/225.jpg)
Summary of Lisp Parentheses
I Parentheses render explicit the
tree-structure of your program
I Homoiconicity lets you write programs
with programs
I Structural Editing is fun and easy
![Page 226: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/226.jpg)
Summary of Lisp Parentheses
I Parentheses render explicit the
tree-structure of your program
I Homoiconicity lets you write programs
with programs
I Structural Editing is fun and easy
![Page 227: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/227.jpg)
Syntax is bad because
I It hides the program’s structure
I It destroys homoiconicity
I It is needlessly complex
![Page 228: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/228.jpg)
Syntax is bad because
I It hides the program’s structure
I It destroys homoiconicity
I It is needlessly complex
![Page 229: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/229.jpg)
Syntax is bad because
I It hides the program’s structure
I It destroys homoiconicity
I It is needlessly complex
![Page 230: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/230.jpg)
Syntax is bad because
I It hides the program’s structure
I It destroys homoiconicity
I It is needlessly complex
![Page 231: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/231.jpg)
”Things should be made as simple as
possible – but no simpler.” – Albert Einstein
![Page 232: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/232.jpg)
Part 3: OOP isn’t the only path to
Polymorphism and Code Reuse
![Page 233: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/233.jpg)
OOP has good goals
1. to group objects together
2. to encapsulate
3. to dispatch polymorphically
4. to reuse code
![Page 234: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/234.jpg)
OOP has good goals
1. to group objects together
2. to encapsulate
3. to dispatch polymorphically
4. to reuse code
![Page 235: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/235.jpg)
OOP has good goals
1. to group objects together
2. to encapsulate
3. to dispatch polymorphically
4. to reuse code
![Page 236: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/236.jpg)
OOP has good goals
1. to group objects together
2. to encapsulate
3. to dispatch polymorphically
4. to reuse code
![Page 237: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/237.jpg)
OOP has good goals
1. to group objects together
2. to encapsulate
3. to dispatch polymorphically
4. to reuse code
![Page 238: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/238.jpg)
These are all good ideas and good goals.
![Page 239: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/239.jpg)
However, OOP is not the only way to reach
these goals.
![Page 240: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/240.jpg)
Claim #3: ”Functions compose better than
objects.”
![Page 241: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/241.jpg)
The fundamental mechanism of OOP – the
inheritance of data, interfaces, type, or
methods from a parent – is often more
difficult to use in practice than techniques
that use functions to achieve the same effect.
![Page 242: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/242.jpg)
Functions are simpler than objects.
![Page 243: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/243.jpg)
Objects are semantic compounds of types,
data, and methods.
![Page 244: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/244.jpg)
Implementation inheritance is bad:
I Forces “is-a” relationship instead of
“has-a”, and “has-a” is almost always
better
I Heirarchical nominalization is difficult
I Changes to a class affect all the
subclasses
![Page 245: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/245.jpg)
Implementation inheritance is bad:
I Forces “is-a” relationship instead of
“has-a”, and “has-a” is almost always
better
I Heirarchical nominalization is difficult
I Changes to a class affect all the
subclasses
![Page 246: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/246.jpg)
Implementation inheritance is bad:
I Forces “is-a” relationship instead of
“has-a”, and “has-a” is almost always
better
I Heirarchical nominalization is difficult
I Changes to a class affect all the
subclasses
![Page 247: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/247.jpg)
Implementation inheritance is bad:
I Forces “is-a” relationship instead of
“has-a”, and “has-a” is almost always
better
I Heirarchical nominalization is difficult
I Changes to a class affect all the
subclasses
![Page 248: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/248.jpg)
An example will help clarify.
![Page 249: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/249.jpg)
Balls
I Ball class (presumably round)
I rollingBall subclass
I bouncingBall subclass
![Page 250: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/250.jpg)
Balls
I Ball class (presumably round)
I rollingBall subclass
I bouncingBall subclass
![Page 251: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/251.jpg)
Balls
I Ball class (presumably round)
I rollingBall subclass
I bouncingBall subclass
![Page 252: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/252.jpg)
Balls
I Ball class (presumably round)
I rollingBall subclass
I bouncingBall subclass
![Page 253: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/253.jpg)
Problems
I What happens if we want to make a ball
that both rolls and bounces?
I Do/Can we inherit from both?
I What if our ball cracks and loses its
bounciness?
I Is a non-round rugby ball a subclass of
ball too?
![Page 254: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/254.jpg)
Problems
I What happens if we want to make a ball
that both rolls and bounces?
I Do/Can we inherit from both?
I What if our ball cracks and loses its
bounciness?
I Is a non-round rugby ball a subclass of
ball too?
![Page 255: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/255.jpg)
Problems
I What happens if we want to make a ball
that both rolls and bounces?
I Do/Can we inherit from both?
I What if our ball cracks and loses its
bounciness?
I Is a non-round rugby ball a subclass of
ball too?
![Page 256: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/256.jpg)
Problems
I What happens if we want to make a ball
that both rolls and bounces?
I Do/Can we inherit from both?
I What if our ball cracks and loses its
bounciness?
I Is a non-round rugby ball a subclass of
ball too?
![Page 257: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/257.jpg)
Problems
I What happens if we want to make a ball
that both rolls and bounces?
I Do/Can we inherit from both?
I What if our ball cracks and loses its
bounciness?
I Is a non-round rugby ball a subclass of
ball too?
![Page 258: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/258.jpg)
Interfaces are Simpler
I Define functional interfaces, but don’t
inherit the implementation
I If you want to use another object’s
function to accomplish a task, just use it
I No need to encapsulate their function in
an object
I Multiple interfaces are simpler than
multiple inheritance
![Page 259: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/259.jpg)
Interfaces are Simpler
I Define functional interfaces, but don’t
inherit the implementation
I If you want to use another object’s
function to accomplish a task, just use it
I No need to encapsulate their function in
an object
I Multiple interfaces are simpler than
multiple inheritance
![Page 260: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/260.jpg)
Interfaces are Simpler
I Define functional interfaces, but don’t
inherit the implementation
I If you want to use another object’s
function to accomplish a task, just use it
I No need to encapsulate their function in
an object
I Multiple interfaces are simpler than
multiple inheritance
![Page 261: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/261.jpg)
Interfaces are Simpler
I Define functional interfaces, but don’t
inherit the implementation
I If you want to use another object’s
function to accomplish a task, just use it
I No need to encapsulate their function in
an object
I Multiple interfaces are simpler than
multiple inheritance
![Page 262: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/262.jpg)
Interfaces are Simpler
I Define functional interfaces, but don’t
inherit the implementation
I If you want to use another object’s
function to accomplish a task, just use it
I No need to encapsulate their function in
an object
I Multiple interfaces are simpler than
multiple inheritance
![Page 263: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/263.jpg)
FREE THE VERBS!! Separate your object
methods from your objects!
![Page 264: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/264.jpg)
Example: Single vs Multiple Dispatch
![Page 265: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/265.jpg)
Making Drum Noises
I Drum, cymbal and stick classes
I When I hit something with the stick, it
makes a noise
I Single Dispatch:
drum.makeNoise(drumstick)
I cymbal.makeNoise(drumstick)
![Page 266: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/266.jpg)
Making Drum Noises
I Drum, cymbal and stick classes
I When I hit something with the stick, it
makes a noise
I Single Dispatch:
drum.makeNoise(drumstick)
I cymbal.makeNoise(drumstick)
![Page 267: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/267.jpg)
Making Drum Noises
I Drum, cymbal and stick classes
I When I hit something with the stick, it
makes a noise
I Single Dispatch:
drum.makeNoise(drumstick)
I cymbal.makeNoise(drumstick)
![Page 268: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/268.jpg)
Making Drum Noises
I Drum, cymbal and stick classes
I When I hit something with the stick, it
makes a noise
I Single Dispatch:
drum.makeNoise(drumstick)
I cymbal.makeNoise(drumstick)
![Page 269: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/269.jpg)
Making Drum Noises
I Drum, cymbal and stick classes
I When I hit something with the stick, it
makes a noise
I Single Dispatch:
drum.makeNoise(drumstick)
I cymbal.makeNoise(drumstick)
![Page 270: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/270.jpg)
The verbs are owned by the nouns.
![Page 271: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/271.jpg)
But what happens when I add a different
stick class?
![Page 272: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/272.jpg)
![Page 273: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/273.jpg)
![Page 274: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/274.jpg)
Now I will need to modify the drum and
cymbal classes and add new methods to
handle the mallets!
![Page 275: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/275.jpg)
When two objects hit, the sound is function
of both objects.
![Page 276: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/276.jpg)
With multi-method functions
I hit(drumstick, cymbal) = crash
I hit(mallet, cymbal) = roar
I hit(drumstick, drum) = bam
I hit(mallet, drum) = bom
I hit(drumstick, drumstick) = click
I hit(cymbal, cymbal) = loud crash
![Page 277: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/277.jpg)
With multi-method functions
I hit(drumstick, cymbal) = crash
I hit(mallet, cymbal) = roar
I hit(drumstick, drum) = bam
I hit(mallet, drum) = bom
I hit(drumstick, drumstick) = click
I hit(cymbal, cymbal) = loud crash
![Page 278: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/278.jpg)
With multi-method functions
I hit(drumstick, cymbal) = crash
I hit(mallet, cymbal) = roar
I hit(drumstick, drum) = bam
I hit(mallet, drum) = bom
I hit(drumstick, drumstick) = click
I hit(cymbal, cymbal) = loud crash
![Page 279: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/279.jpg)
With multi-method functions
I hit(drumstick, cymbal) = crash
I hit(mallet, cymbal) = roar
I hit(drumstick, drum) = bam
I hit(mallet, drum) = bom
I hit(drumstick, drumstick) = click
I hit(cymbal, cymbal) = loud crash
![Page 280: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/280.jpg)
With multi-method functions
I hit(drumstick, cymbal) = crash
I hit(mallet, cymbal) = roar
I hit(drumstick, drum) = bam
I hit(mallet, drum) = bom
I hit(drumstick, drumstick) = click
I hit(cymbal, cymbal) = loud crash
![Page 281: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/281.jpg)
With multi-method functions
I hit(drumstick, cymbal) = crash
I hit(mallet, cymbal) = roar
I hit(drumstick, drum) = bam
I hit(mallet, drum) = bom
I hit(drumstick, drumstick) = click
I hit(cymbal, cymbal) = loud crash
![Page 282: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/282.jpg)
With multi-method functions
I hit(drumstick, cymbal) = crash
I hit(mallet, cymbal) = roar
I hit(drumstick, drum) = bam
I hit(mallet, drum) = bom
I hit(drumstick, drumstick) = click
I hit(cymbal, cymbal) = loud crash
![Page 283: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/283.jpg)
IMPORTANT: hit() is not a single function,
but a collection of functions. (It is called a
multi-method or generic function)
![Page 284: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/284.jpg)
The particular function that is called is
determined by the type of both of its
arguments.
![Page 285: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/285.jpg)
As you add more classes, just add more
definitions of hit().
![Page 286: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/286.jpg)
Part 3 Conclusion
I Polymorphism is better done through
interfaces than subtype inheritance.
I Functions do not require a heirarchy
I Functions allow simple multiple dispatch
![Page 287: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/287.jpg)
Part 3 Conclusion
I Polymorphism is better done through
interfaces than subtype inheritance.
I Functions do not require a heirarchy
I Functions allow simple multiple dispatch
![Page 288: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/288.jpg)
Part 3 Conclusion
I Polymorphism is better done through
interfaces than subtype inheritance.
I Functions do not require a heirarchy
I Functions allow simple multiple dispatch
![Page 289: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/289.jpg)
Part 3 Conclusion
I Polymorphism is better done through
interfaces than subtype inheritance.
I Functions do not require a heirarchy
I Functions allow simple multiple dispatch
![Page 290: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/290.jpg)
The End
![Page 291: Functional Programming with Immutable Data Structures](https://reader034.vdocument.in/reader034/viewer/2022052310/5555506eb4c90530208b4bad/html5/thumbnails/291.jpg)
Any Questions?