BetterProgrammingThroughFP PDF
BetterProgrammingThroughFP PDF
Dean Wampler
polyglotprogramming.com/
programmingscala.com
fpjava 2
Friday, April 12, 13
I got interested in FP about 5 years ago when everyone was talking about it. I decided it was time to learn myself and I expected
to pick up some good ideas, but otherwise remain primarily an object-oriented developer. Actually, it caused me to rethink my
views and now I tend to use FP more than OOP. This tutorial explains why.
The problems of our time.
What is Functional
Programming?
Better data structures.
Better concurrency.
Better objects.
3
The problems
of our time.
4
...
Friday, April 12, 13
Not just these big companies, but many organizations have lots of data they want to analyze and exploit.
(San Francisco)
Mud, Death Hollow Trail, Utah
We need better
modularity.
We need
better
agility.
(Ascending the steel cable ladder up the back side of Half Dome, Yosemite National Park)
Maligne Lake, Jasper Nat. Park
We need a return
to simplicity.
Friday, April 12, 13
Every now and then, we need to stop, look at what were doing, and remove the cruft weve accumulated. If youre a Java
programmer, recall how efforts like the Spring Framework forced a rethinking of J2EE. I claim that a lot of the code we write,
specifically lots of object middleware, is cruft. Functional programming isnt *simple*, but in my view it reflects a refocusing on
core principles and minimally-sufficient design.
Immutable
Values
14
y = sin(x)
1 = sin(/2)
x and y are variables.
Once you assign a value to x,
you fix the value assigned to y.
15
y = sin(x)
+= 1
17
If a value is immutable,
synchronizing access is no longer necessary!
20
21
list = List.new(1,
List.new(2,
List.new(3, )))
22
Side-effect
free
functions 23
y = sin(x)
24
y = sin(x)
First-class
functions
28
usearg("Dean", &f)
# "Hello, Dean!"
30
Side note: Java 8 will *hopefully*, *finally* add a lambda syntax to eliminate lots of this boilerplate.
Higher-order
Functions
def usearg(arg, &func)
func.call(arg)
end
Recursion
36
=> "(1,(2,(3,())))"
Better data
structures
39
Better data
structures
40
41
42
NullPointerException!!
Map<K,V>.get signature:
V get(Object key);
47
49
Better data
structures
50
=> "(1,(2,(3,())))"
54
55
56
Better data
structures
57
filter find_all
map map
fold inject
59
61
63
64
67
68
69
Substitute - for +.
Subtraction is neither
commutative nor associative.
70
Better data
structures
72
73
74
79
81
Better data
structures
83
84
85
a1 b1
a2 a3 b2 b3
a4 b4
90
At time 1, value1
value1 value2 still exists. The new
value2 reuses a3,
a4, and the b1 tree
a1 b1
a2 a3 b2 b3
a4 b4
91
a1 b1
a2 a3 b2 b3
a4 b4
92
94
Actor1 Actor2
2 3
Actor1 Actor2
2 3
98
104
Atomicity
Consistency
Isolation
Durability
105
Friday, April 12, 13
Youre familiar with ACID transactions, a central feature of relational databases.
ACID transactions
ensure data integrity.
106
107
Friday, April 12, 13
Can we get the same semantics for updating values in memory?? Note that memory isnt durable.
Software Transactional
Memory (STM)
Atomicity
Consistency
Isolation
Durability
108
Friday, April 12, 13
Software: its managed in software (there were some experimental efforts to do this in hardware in the 90s).
Transactional semantics.
Memory: were mutating values in memory.
Time 0
Persistent Data
value1
Structures
are just what we need.
a1 b1
a2 a3 b2 b3
a4 b4
109
At time 0, two
ref1 ref2
references, ref1 and
ref2 both refer to the
same value.
value1
a1 b1
a2 a3 b2 b3
a4 b4
110
ref1 ref2
At time 1, ref2 has
been moved to the
new value.
value1 value2
a1 b1
a2 a3 b2 b3
a4 b4
111
ref1 ref2
In Clojure simple
assignment to mutate
a value isnt allowed.
STM is one of several
value1 value2
mechanisms you
must use.
a1 b1
a2 a3 b2 b3
a4 b4
112
121
122
We start by defining a function that can generate N random sample instances within an arbitrary range.
Imaginary RSpec
describe "Money addition" do
money_gen = Generator.new do
Money(-100.0) to Money(100.0)
end
property "is commutative" do
money_gen.make_pairs do |m1,m2|
m1.add(m2).should_be_close(
m2.add(m1), Money::PRECISION)
end
end verify that addition is
end commutative!
123
124
129
The word pattern in pattern matching is not meant in the design pattern sense.
Pattern Matching is one
of the most pervasive
tools in functional
programming.
134
137
or
package ToJSON
toJSON(ParentA1)
toJSON(ParentB1)
toJSON(ChildB1)
toJSON(ChildB2)
toJSON(...)
138
140
ParentB1
toJSON
ChildB1
toJSON
ChildB2
toJSON
or Process 1 Process 2 Process 3
141
4 1 1
or
Relational
Mapping
3
ChildB1 ChildB2
toJSON toJSON
Result Set Result Set
2 2
Database Database
Recap
...
(San Francisco)
Mud, Death Hallow Trail, Utah
We need better
modularity.
We need
better
agility.
(Ascending the steel cable ladder up the back side of Half Dome, Yosemite National Park)
Maligne Lake, Jasper Nat. Park
We need a return
to simplicity.
Friday, April 12, 13
Every now and then, we need to stop, look at what were doing, and remove the cruft weve accumulated. I claim that a lot of the
code we write, specifically lots of object middleware, is cruft.
151
Friday, April 12, 13
Here is a list of the most popular dynamically-typed functional languages.
Going from here:
Channel 9 videos
Blogs, books, ...
152
Friday, April 12, 13
There are excellent MSDN Channel 9 videos on functional programming.
Numerous blogs, books, etc...
Thank You! Functional
Programming
[email protected] for Java Developers
@deanwampler
Dean Wampler
polyglotprogramming.com