Some basic FP concepts
@friemens
Why is mutable state problematic?
Programming without assignment?
Functions as Values
Leave data alone!
Handle mutable state safely
1930s Lambda Calculus
1960s Lisp
1990s Haskell
Nothing new here!
„Familiarity and Simplicity are orthogonal concepts.“
Rich Hickey
Why is mutable state problematic?
A system without side-effects is useless,
but...
Mutation hinders reasoning.
Mutable state increases test effort.
Mutation introduces order.
1. 2.
Mutable state makes concurrency hard.
Side-effects restrict how the machine can help us.
FP programming
OOP programming
Pure Functions
Side-effects
Context dependance
The future-proof structure of any software system.
Functions as Values
Pure Function=
No side-effects+
Result depends only on param values
Lambda Expression=
An anonymous function.
Closure=
A function + some captured environment.
Higher-Order Function
Fn: [Any... -> Any]F: [Any -> Fn]G: [Fn -> Any]H: [Fn -> Fn]
A function that - does something with another function- or returns a function, - or both.
Function Application
Fn: [Any... -> Any]apply: [Fn Any* -> Any]
Take collection of param values and invoke function.
Partial Application
Fn: [Any1...Anym...Anyn -> Any]partial: [Fn Any1...Anym -> [Anym+1...Anyn -> Any]]
Create a new function with some arguments fixed.
(„Currying“ is automatic partial application.)
Function Composition
F: [X -> Y]G: [Y -> Z]compose: [G F -> [X -> Z]]
Concatenate computations.
Programming without assignment?
Good bye =, for, while and cousins, ...
… say hello to let, map, filter and friends!
Thinking in collection transformations.
xs
ys
z
filter
map
concat reduce
Don't be so eager!
map filter mapcat into
Cheap parallelization.
reduce
combine
Handle mutable state safely
Separate state from identity.
State x State x'
Identity
swap!
f
Give identities well-defined concurrency semantics.
Share immutable state liberally.
Be restrictive with access to identities.
Leave data alone!
Objects claim feature completeness.
BigHero
-a string
-a map
+method1+method2+method3
You can't foresee the future.
You will violate the Open-Closed-Principle.
„Inventing a class with its own interface to hold a piece of information is like inventing a new language to write every short story. “
Rich Hickey
Data is simple.
Data Model
API
DSL
Wrap up
OO makes code understandable by encapsulating moving parts.
FP makes code understandable by minimizing moving parts.
Michael Feathers
The future is functional.
You need to practice FP before you „get“ it.
It's more fun!
Questions?