@miciek
Michał Płachta
Pragmatist`s Guide to
www.michalplachta.com
Functional Geekery
Slide 2
Slide 2 text
@miciek
Let’s talk functional!
Slide 3
Slide 3 text
@miciek
Slide 4
Slide 4 text
@miciek
Galactic Twitter,
one week.
Slide 5
Slide 5 text
@miciek
Slide 6
Slide 6 text
@miciek
Being functional...
higher order functions
immutable collections
no reassignments
functions
immutable types
Slide 7
Slide 7 text
@miciek
Being functional...
higher order functions
-style
immutable collections
Guava Immutable Collections
Java 8 Streams
no reassignments
Java final keyword
functions
Java 8 lambdas
immutable types
Lombok @Value
Slide 8
Slide 8 text
@miciek
Being functional...
higher order functions
-style
built-in
and more!
immutable collections
built-in
no reassignments
val keyword
functions
first class citizens
immutable types
built-in
Slide 9
Slide 9 text
@miciek
Being functional...
higher order functions
-style
built-in
immutable collections
built-in
no reassignments
val keyword
functions
first class citizens
immutable types
built-in
Slide 10
Slide 10 text
@miciek
Bringing style to
Slide 11
Slide 11 text
@miciek
Being functional...
higher order functions
-style
built-in
and more!
immutable collections
built-in
no reassignments
Java final keyword
functions
built-in
immutable types
Lombok @Value
Slide 12
Slide 12 text
@miciek
Galactic Twitter,
one week.
Slide 13
Slide 13 text
@miciek
Slide 14
Slide 14 text
@miciek
Slide 15
Slide 15 text
@miciek
Slide 16
Slide 16 text
@miciek
Slide 17
Slide 17 text
@miciek
Future
Slide 18
Slide 18 text
@miciek
Future
finishes successfully with a result of type T
fails with an exception
Slide 19
Slide 19 text
@miciek
@miciek
Slide 20
Slide 20 text
@miciek
Slide 21
Slide 21 text
@miciek
Future.get
is a blocking call!
Slide 22
Slide 22 text
@miciek
Citizen
Future Future
List
Future
map ???
DBClient::getFollowers
Slide 23
Slide 23 text
@miciek
Citizen
Future Future
List
Future
List
Future
map
flatMap
Citizen
Future
map
Integer
Future
???
Slide 24
Slide 24 text
@miciek
Slide 25
Slide 25 text
@miciek
Future.forEach
doesn’t block! :)
Slide 26
Slide 26 text
@miciek
io.vavr.collection.HashMap
Option
can be Some when value is present
can be None when value is not present
Slide 27
Slide 27 text
@miciek
Slide 28
Slide 28 text
@miciek
polling every 200ms
Slide 29
Slide 29 text
@miciek
@miciek
Slide 30
Slide 30 text
@miciek
@miciek
I have 0 followers.
It’s not possible.
Slide 31
Slide 31 text
@miciek
@miciek
Slide 32
Slide 32 text
@miciek
Problem: treating 0 as “no value yet”
Slide 33
Slide 33 text
@miciek
Problem: treating 0 as “no value yet”
Solution: explicit return type
Option
can be Some
when value is present
can be None
when value is not present
We should use types to
model our assumptions!
Slide 34
Slide 34 text
@miciek
Slide 35
Slide 35 text
@miciek
polling every 200ms
when None is returned
when Some(102) is returned
Slide 36
Slide 36 text
@miciek
@miciek
Slide 37
Slide 37 text
@miciek
@miciek
I can’t check how many followers
does Obi-wan Kenobi have!
Slide 38
Slide 38 text
@miciek
@miciek
Slide 39
Slide 39 text
@miciek
Problem: not handling Future failures
java.lang.Exception: Bad Request:
citizen with name Obi-wan Kenobi couldn't be found
Slide 40
Slide 40 text
@miciek
Problem: not handling Future failures
Solution: explicit return type (again!)
Try
can be Success
when computation succeeded
can be Failure
when computation failed
We should use types to
model our assumptions!
Slide 41
Slide 41 text
@miciek
io.vavr.concurrent.Future
Slide 42
Slide 42 text
@miciek
Pipelining potential failures
Slide 43
Slide 43 text
@miciek
polling every 200ms
when None is returned
when Some(Success(102))
is returned
when Some(Failure) is returned
Slide 44
Slide 44 text
@miciek
@miciek
Slide 45
Slide 45 text
@miciek
@miciek
I am not happy with the return
types you use in the
application!
Slide 46
Slide 46 text
@miciek
@miciek
Slide 47
Slide 47 text
@miciek
@miciek
Future>>>
I am not happy with the return
types we use in the application!
Slide 48
Slide 48 text
@miciek
Problem: cryptic, complicated types
Solution: Algebraic Data Types
Sum
can be Something or
can be SomethingDifferent
We should use types to
model our assumptions!
can be SomethingElse or
Product
consists of Something and
SomethingElse
Slide 49
Slide 49 text
@miciek
Citizen
can be Civilian or
can be Stormtropper
can be Rebel or
Stormtrooper
consists of String name and
boolean isCloned
can be Jedi or
can be Sith or
ADTs
Slide 50
Slide 50 text
@miciek
Slide 51
Slide 51 text
@miciek
Slide 52
Slide 52 text
@miciek
How to convert Try into
RemoteFollowersData?
Slide 53
Slide 53 text
@miciek
Pattern Matching
Slide 54
Slide 54 text
@miciek
Slide 55
Slide 55 text
@miciek
Slide 56
Slide 56 text
@miciek
@miciek
Slide 57
Slide 57 text
@miciek
@miciek
Darth Vader has way too many
followers! They are all clones!
@miciek
Citizen
any Civilian or
any Stormtropper that is not cloned
any Rebel or
any Jedi or
any Sith or
Counting followers
returns true if
Slide 61
Slide 61 text
@miciek
Pattern Matching to the rescue!
Slide 62
Slide 62 text
@miciek
Slide 63
Slide 63 text
@miciek
@miciek
Slide 64
Slide 64 text
@miciek
@miciek
I am not happy! Galactic Twitter
is used for political purposes!
Slide 65
Slide 65 text
@miciek
Fear is the path
to the Dark Side.
Yoda
Help me Obi-Wan
Kenobi, you're my
only hope!
Princess Leia
Use the Force,
Luke.
Obi-Wan Kenobi
Very political!
Not cool!
Slide 66
Slide 66 text
@miciek
Don’t delete,
manipulate!
Censorship module
● Condition: If the tweet is pro Dark Side
○ Manipulation: Add more Dark Side
● If the tweet is pro Light Side
○ Replace Force with Dark Side
● If the tweet is a general wisdom
○ Replace Force with Dark Side
○ Add more Dark Side
● If the tweet is pro Rebellion and is not a joke
○ Hail the Empire
● If the tweet is pro Empire and is a joke
○ Trash the Rebellion
● If the tweet is not pro Empire nor Rebellion
○ Add Force and even more Force
○ Make joke about it
● If the tweet is pro empire
○ Add more Dark Side
● Always
○ Sacrifice for the Empire
Slide 67
Slide 67 text
@miciek
No more than 2
manipulations!
Censorship module initial filters
● If the tweet is pro Dark Side
○ Add more Dark Side
● If the tweet is pro Light Side
○ Replace Force with Dark Side
● If the tweet is a general wisdom
○ Replace Force with Dark Side
○ Add more Dark Side
● If the tweet is pro Rebellion and is not a joke
○ Hail the Empire
● If the tweet is pro Empire and is a joke
○ Trash the Rebellion
● If the tweet is not pro Empire nor Rebellion
○ Add Force and even more Force
○ Make joke about it
● If the tweet is pro empire
○ Add more Dark Side
● Always
○ Sacrifice for the Empire
@miciek
Executing a List of ints?
4
(0, 4) -> 0 + 4
6 3
(4, 6) -> 4 + 6 (10, 3) -> 10 + 3
Initial = 0
Output = 13
(result, x) -> result + x
Slide 74
Slide 74 text
@miciek
Executing a List of ints?
4
(result, x) -> result + x
6 3
(result, x) -> result + x (result, x) -> result + x
Initial = 0
io.vavr.collections.List
Output = 13
@miciek
Problem: writing tests by examples
Solution: property-based tests
We should test the
properties!
Slide 84
Slide 84 text
@miciek
Slide 85
Slide 85 text
@miciek
Slide 86
Slide 86 text
@miciek
@miciek
Slide 87
Slide 87 text
@miciek
@miciek
Starring
Coffeehouse Programmer
Darth Vader
Luke Skywalker
Uncle Bob
Slide 88
Slide 88 text
@miciek
@miciek
Directed by
Future type
Option type
Try type
“monads”
Slide 89
Slide 89 text
@miciek
@miciek
Special Effects
Algebraic Data Types
Pattern Matching
Property-based tests
Slide 90
Slide 90 text
@miciek
@miciek
github.com/miciek/galactic-twitter
Screenplay
Java vs Scala
Web server in Akka HTTP
Pattern Matching implementation
RemoteData as a Functor
Type class pattern
“Pimp my type” pattern
Slide 91
Slide 91 text
@miciek
@miciek
(Your) Next Episode
Everything
as
Data
Slide 92
Slide 92 text
@miciek
Pragmatist`s Guide to
Functional Geekery
github.com/miciek/galactic-twitter
ANY QUESTIONS?
Michał Płachta
www.michalplachta.com
Slide 93
Slide 93 text
@miciek
@miciek
Slide 94
Slide 94 text
@miciek
@miciek
You don’t have clean code!
Slide 95
Slide 95 text
@miciek
Slide 96
Slide 96 text
@miciek
Concern #1: represent state of db cache Concern #2: represent RemoteData as JSON
Slide 97
Slide 97 text
@miciek
Coupled concerns
RemoteData
RemoteData as
JSON Response
HTTP Server
Slide 98
Slide 98 text
@miciek
Type class pattern
RemoteData
RemoteData as
JSON Response
RemoteData
JSON Response
HTTP Server HTTP Server
RemoteData Tweet
type class
type class members
Slide 99
Slide 99 text
@miciek
Pure values, no mess!
Slide 100
Slide 100 text
@miciek
But the server needs an
instance of type class!
Slide 101
Slide 101 text
@miciek
Compiler will use them
automatically!
Slide 102
Slide 102 text
@miciek
Slide 103
Slide 103 text
@miciek
Decoupled concerns
RemoteData
RemoteData as
JSON Response
RemoteData
JSON Response
HTTP Server HTTP Server
RemoteData Tweet
type class
type class members
Slide 104
Slide 104 text
@miciek
Future type lib lib* built-in
Option type lib lib* built-in
Try type lib lib* built-in
ADTs lib built-in built-in
Pattern Matching lib kind of* built-in
Property tests lib lib lib
* more:
https://github.com/MarioAriasC/funKTionale
https://kotlin.link/articles/Kotlin-for-Scala-Developers.html
https://programmingideaswithjake.wordpress.com/2016/08/27/improved-pattern-matching-in-kotlin/
Everything as Data