Some basic FP concepts
-
Upload
falko-riemenschneider -
Category
Presentations & Public Speaking
-
view
150 -
download
0
description
Transcript of Some basic FP concepts
![Page 1: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/1.jpg)
Some basic FP concepts
@friemens
![Page 2: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/2.jpg)
Why is mutable state problematic?
Programming without assignment?
Functions as Values
Leave data alone!
Handle mutable state safely
![Page 3: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/3.jpg)
1930s Lambda Calculus
1960s Lisp
1990s Haskell
Nothing new here!
![Page 4: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/4.jpg)
„Familiarity and Simplicity are orthogonal concepts.“
Rich Hickey
![Page 5: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/5.jpg)
Why is mutable state problematic?
![Page 6: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/6.jpg)
A system without side-effects is useless,
but...
![Page 7: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/7.jpg)
Mutation hinders reasoning.
![Page 8: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/8.jpg)
Mutable state increases test effort.
![Page 9: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/9.jpg)
Mutation introduces order.
1. 2.
![Page 10: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/10.jpg)
Mutable state makes concurrency hard.
![Page 11: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/11.jpg)
Side-effects restrict how the machine can help us.
FP programming
OOP programming
![Page 12: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/12.jpg)
![Page 13: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/13.jpg)
Pure Functions
Side-effects
Context dependance
The future-proof structure of any software system.
![Page 14: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/14.jpg)
Functions as Values
![Page 15: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/15.jpg)
Pure Function=
No side-effects+
Result depends only on param values
![Page 16: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/16.jpg)
Lambda Expression=
An anonymous function.
![Page 17: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/17.jpg)
Closure=
A function + some captured environment.
![Page 18: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/18.jpg)
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.
![Page 19: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/19.jpg)
Function Application
Fn: [Any... -> Any]apply: [Fn Any* -> Any]
Take collection of param values and invoke function.
![Page 20: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/20.jpg)
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.)
![Page 21: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/21.jpg)
Function Composition
F: [X -> Y]G: [Y -> Z]compose: [G F -> [X -> Z]]
Concatenate computations.
![Page 22: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/22.jpg)
Programming without assignment?
![Page 23: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/23.jpg)
Good bye =, for, while and cousins, ...
… say hello to let, map, filter and friends!
![Page 24: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/24.jpg)
Thinking in collection transformations.
xs
ys
z
filter
map
concat reduce
![Page 25: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/25.jpg)
Don't be so eager!
map filter mapcat into
![Page 26: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/26.jpg)
Cheap parallelization.
reduce
combine
![Page 27: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/27.jpg)
Handle mutable state safely
![Page 28: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/28.jpg)
Separate state from identity.
State x State x'
Identity
swap!
f
![Page 29: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/29.jpg)
Give identities well-defined concurrency semantics.
![Page 30: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/30.jpg)
Share immutable state liberally.
Be restrictive with access to identities.
![Page 31: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/31.jpg)
Leave data alone!
![Page 32: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/32.jpg)
Objects claim feature completeness.
BigHero
-a string
-a map
+method1+method2+method3
![Page 33: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/33.jpg)
You can't foresee the future.
![Page 34: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/34.jpg)
You will violate the Open-Closed-Principle.
![Page 35: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/35.jpg)
„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
![Page 36: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/36.jpg)
Data is simple.
![Page 37: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/37.jpg)
Data Model
API
DSL
![Page 38: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/38.jpg)
Wrap up
![Page 39: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/39.jpg)
OO makes code understandable by encapsulating moving parts.
FP makes code understandable by minimizing moving parts.
Michael Feathers
![Page 40: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/40.jpg)
The future is functional.
You need to practice FP before you „get“ it.
It's more fun!
![Page 41: Some basic FP concepts](https://reader033.fdocuments.net/reader033/viewer/2022051109/547e7076b4af9fb9158b56bf/html5/thumbnails/41.jpg)
Questions?