Dmitry Vinnik
@DmitryVinnik dvinnik.com
Do you even Function?
Guiding Through
Functional Interfaces
What are our goals?
@DmitryVinnik dvinnik.com
Goals
Functional Programming (FP):
What, How, When and Why
Default FP Capabilities in Java
Custom Functional Solutions
Why Functional
Programming?
@DmitryVinnik dvinnik.com
Because Concurrency
@DmitryVinnik dvinnik.com
Multi-Core
Motivation
Microservices
CCSs
@DmitryVinnik dvinnik.com
Conclusion?
@DmitryVinnik dvinnik.com
Concurrency is
a New Reality
@DmitryVinnik dvinnik.com
Concurrency is
a Reality
@DmitryVinnik dvinnik.com
But!
@DmitryVinnik dvinnik.com
What’s in it for us?
@DmitryVinnik dvinnik.com
Benefits
No Idling ↑ UX
Abstraction
@DmitryVinnik dvinnik.com
Only good parts, right?
@DmitryVinnik dvinnik.com
Complexities
Thread
Safety
Performanc
e
Liveness
SDLC
@DmitryVinnik dvinnik.com
How do we tackle it?
@DmitryVinnik dvinnik.com
With Functional
Programming
@DmitryVinnik dvinnik.com
Design for Functional
Programming
@DmitryVinnik dvinnik.com
Design for FP
Design by
Contract
Temporal
Coupling
Immutability &
Atomicity
Lazy Evaluation
Lazy
Evaluation
No Temporal
Coupling
Immutability &
Atomicity
@DmitryVinnik dvinnik.com
Design by Contract
@DmitryVinnik dvinnik.com
“Require No More,
Promise No Less”
Quote from Bertrand Meyer,
“Design by Contract”
@DmitryVinnik dvinnik.com
Design by Contract
@DmitryVinnik dvinnik.com
Design for FP
Design by
Contract
Temporal
Coupling
Immutability &
Atomicity
Lazy Evaluation
Lazy
Evaluation
No Temporal
Coupling
Immutability &
Atomicity
@DmitryVinnik dvinnik.com
Design by
Contract
Lazy
Evaluation
Lazy Evaluation
@DmitryVinnik dvinnik.com
"Lazy Evaluation is like
Filing Taxes"
Quote from Venkat Subramaniam,
“Let’s Get Lazy”
@DmitryVinnik dvinnik.com
Lazy Evaluation
@DmitryVinnik dvinnik.com
Design for FP
Design by
Contract
Temporal
Coupling
Immutability &
Atomicity
Lazy Evaluation
Lazy
Evaluation
No Temporal
Coupling
Immutability &
Atomicity
@DmitryVinnik dvinnik.com
Design by
Contract
Lazy
Evaluation
Lazy
Evaluation
No Temporal
Coupling
Temporal Coupling
@DmitryVinnik dvinnik.com
“When time & order
really matter”
@DmitryVinnik dvinnik.com
Temporal Coupling
@DmitryVinnik dvinnik.com
Temporal Coupling
@DmitryVinnik dvinnik.com
Temporal Coupling
@DmitryVinnik dvinnik.com
Temporal Coupling
@DmitryVinnik dvinnik.com
Design for FP
Design by
Contract
Temporal
Coupling
Immutability &
Atomicity
Lazy Evaluation
Lazy
Evaluation
No Temporal
Coupling
Immutability &
Atomicity
@DmitryVinnik dvinnik.com
Design by
Contract
Lazy
Evaluation
Lazy
Evaluation
No Temporal
Coupling
No Temporal
Coupling
Immutability &
Atomicity
Functional Programming
@DmitryVinnik dvinnik.com
In Practice
@DmitryVinnik dvinnik.com
How would we write
Functional Code?
@DmitryVinnik dvinnik.com
Let’s consider two styles:
Imperative and
Declarative
@DmitryVinnik dvinnik.com
Assume a Use Case:
Searching for a word
in a phrase
@DmitryVinnik dvinnik.com
Imperative Style
@DmitryVinnik dvinnik.com
Declarative Style
@DmitryVinnik dvinnik.com
Styles Side by Side
@DmitryVinnik dvinnik.com
Benefits of
Declarative Style
↑ Readability
↓ Verbosity↓ Complexity ↓ Verbosity
↑ Readability
@DmitryVinnik dvinnik.com
Decreased Complexity
@DmitryVinnik dvinnik.com
Benefits of
Declarative Style
↑ Readability
↓ Verbosity↓ Complexity ↓ Verbosity
↑ Readability
@DmitryVinnik dvinnik.com
↓ Complexity ↓ Verbosity
Decreased Verbosity
@DmitryVinnik dvinnik.com
Decreased Verbosity
@DmitryVinnik dvinnik.com
Decreased Verbosity
@DmitryVinnik dvinnik.com
Benefits of
Declarative Style
↑ Readability
↓ Verbosity↓ Complexity ↓ Verbosity
↑ Readability
@DmitryVinnik dvinnik.com
↓ Complexity ↓ Verbosity↓ Verbosity
↑ Readability
Increased Readability
@DmitryVinnik dvinnik.com
Conclusion?
@DmitryVinnik dvinnik.com
Changing Paradigms
@DmitryVinnik dvinnik.com
FP
Changing Paradigms
Imperative
Programming
Declarative
Programming
@DmitryVinnik dvinnik.com
Enough Theory!
@DmitryVinnik dvinnik.com
Let’s see some FP in Java
@DmitryVinnik dvinnik.com
Starting from the Base
@DmitryVinnik dvinnik.com
Stream
Optional
Starting from the Base
@DmitryVinnik dvinnik.com
What do we do the most?
@DmitryVinnik dvinnik.com
Dealing with Data
@DmitryVinnik dvinnik.com
Dealing with Collections
@DmitryVinnik dvinnik.com
How do we get Functional
with Collections?
@DmitryVinnik dvinnik.com
Pipeline of
Functions
Data
Mutation
Lazily
Evaluated
Data
Transformation
Streams
@DmitryVinnik dvinnik.com
Streams
@DmitryVinnik dvinnik.com
That’s all great
@DmitryVinnik dvinnik.com
But...
@DmitryVinnik dvinnik.com
What if I want to find
something?
@DmitryVinnik dvinnik.com
Optional is the answer
@DmitryVinnik dvinnik.com
Finding Optional Talk
@DmitryVinnik dvinnik.com
Optional
Value State
Null Check
Supplement
Data
Mutation
Data
Transformation
@DmitryVinnik dvinnik.com
Optional
@DmitryVinnik dvinnik.com
Looks promising
@DmitryVinnik dvinnik.com
But it still seems verbose
@DmitryVinnik dvinnik.com
Method Reference
to the Rescue!
@DmitryVinnik dvinnik.com
What is it good for?
@DmitryVinnik dvinnik.com
“There are only two hard
things in Computer Science:
Cache Invalidation and
Naming Things”
Quote from Phil Karlton
@DmitryVinnik dvinnik.com
Method Reference: Before
@DmitryVinnik dvinnik.com
Why do we need
temporary variables?
@DmitryVinnik dvinnik.com
Method Reference: After
@DmitryVinnik dvinnik.com
Method Reference
@DmitryVinnik dvinnik.com
Looks good
@DmitryVinnik dvinnik.com
But what about
Functional Programming
and Debugging
@DmitryVinnik dvinnik.com
One Line per Statement Style
@DmitryVinnik dvinnik.com
Now we have all the tools
@DmitryVinnik dvinnik.com
Let’s Get Functional
@DmitryVinnik dvinnik.com
High Order(HO) Functions
Functions
as First Class Entities
Functions
as Input
Functions
as Output
@DmitryVinnik dvinnik.com
Why HO Functions?
↑ Abstraction
DRY by
Design
↓ Complexity ↑ Readability
@DmitryVinnik dvinnik.com
Are all Functions equal?
@DmitryVinnik dvinnik.com
No!
@DmitryVinnik dvinnik.com
Types of Functions
Pure
Functions
Impure
Functions
@DmitryVinnik dvinnik.com
Pure Functions
Immutable Side Effects
Same
Input
Same
Output
@DmitryVinnik dvinnik.com
Pure Functions
@DmitryVinnik dvinnik.com
Let’s see Functions
in Action
@DmitryVinnik dvinnik.com
Let’s see Functions
in Java
@DmitryVinnik dvinnik.com
Single Abstract
Method
Helper
Annotation
Static
Methods
Default
Methods
Functional Interfaces
@DmitryVinnik dvinnik.com
Functional Interfaces
@DmitryVinnik dvinnik.com
High Order Functions
Out-of-the-Box
Function<T, R> Predicate<T>
Consumer<T> Supplier<T>
Predicate<T>
Consumer<T> Supplier<T>
@DmitryVinnik dvinnik.com
Function<T, R>
@DmitryVinnik dvinnik.com
One OutputOne Input
Function<T, R>
@DmitryVinnik dvinnik.com
Function<T, R>
@DmitryVinnik dvinnik.com
Function<T, R>
@DmitryVinnik dvinnik.com
Function<T, R>
@DmitryVinnik dvinnik.com
Function<T, R>
@DmitryVinnik dvinnik.com
Capabilities:
Functions Composition
Function<T, R>
@DmitryVinnik dvinnik.com
Function<T, R>
@DmitryVinnik dvinnik.com
Function<T, R>
@DmitryVinnik dvinnik.com
Function<T, R>
@DmitryVinnik dvinnik.com
Function<T, R>
@DmitryVinnik dvinnik.com
High Order Functions
Out-of-the-Box
Function<T, R> Predicate<T>
Consumer<T> Supplier<T>
Predicate<T>
Consumer<T> Supplier<T>
@DmitryVinnik dvinnik.com
Function<T, R> Predicate<T>
Predicate<T>
@DmitryVinnik dvinnik.com
Boolean
Output
One Input
Predicate<T>
@DmitryVinnik dvinnik.com
Predicate<T>
@DmitryVinnik dvinnik.com
Predicate<T>
@DmitryVinnik dvinnik.com
Predicate<T>
@DmitryVinnik dvinnik.com
Predicate<T>
@DmitryVinnik dvinnik.com
Capabilities:
Logical Operators
Predicate<T>
@DmitryVinnik dvinnik.com
Predicate<T>
@DmitryVinnik dvinnik.com
Predicate<T>
@DmitryVinnik dvinnik.com
Predicate<T>
@DmitryVinnik dvinnik.com
Predicate<T>
@DmitryVinnik dvinnik.com
High Order Functions
Out-of-the-Box
Function<T, R> Predicate<T>
Consumer<T> Supplier<T>
Predicate<T>
Consumer<T> Supplier<T>
@DmitryVinnik dvinnik.com
Function<T, R> Predicate<T>Predicate<T>
Consumer<T>
Consumer<T>
@DmitryVinnik dvinnik.com
No OutputOne Input
Consumer<T>
@DmitryVinnik dvinnik.com
Consumer<T>
@DmitryVinnik dvinnik.com
Consumer<T>
@DmitryVinnik dvinnik.com
Consumer<T>
@DmitryVinnik dvinnik.com
Capabilities:
Functions Chaining
Consumer<T>
@DmitryVinnik dvinnik.com
Consumer<T>
@DmitryVinnik dvinnik.com
Consumer<T>
@DmitryVinnik dvinnik.com
Consumer<T>
@DmitryVinnik dvinnik.com
High Order Functions
Out-of-the-Box
Function<T, R> Predicate<T>
Consumer<T> Supplier<T>
Predicate<T>
Consumer<T> Supplier<T>
@DmitryVinnik dvinnik.com
Function<T, R> Predicate<T>Predicate<T>
Consumer<T>Consumer<T> Supplier<T>
Supplier<T>
@DmitryVinnik dvinnik.com
One OutputNo Input
Supplier<T>
@DmitryVinnik dvinnik.com
Supplier<T>
@DmitryVinnik dvinnik.com
Supplier<T>
@DmitryVinnik dvinnik.com
Supplier<T>
@DmitryVinnik dvinnik.com
Capabilities:
Wrapped Execution
Supplier<T>
@DmitryVinnik dvinnik.com
Supplier<T>
@DmitryVinnik dvinnik.com
Supplier<T>
@DmitryVinnik dvinnik.com
Summary
High Order Functions
@DmitryVinnik dvinnik.com
Function<T, R>
Consumer<T>
Data
Transformation
Predicate<T> Data Filtering
Data
Processing
Supplier<T>
Process
Execution
Out-of-the-Box is nice
@DmitryVinnik dvinnik.com
But what about Custom
Functional Use Cases?
@DmitryVinnik dvinnik.com
Let’s Build Custom
Functional Interface
@DmitryVinnik dvinnik.com
@DmitryVinnik dvinnik.com
What about some
Common Use Cases?
@DmitryVinnik dvinnik.com
Error
Handling
Error
Passing
Multi-Dimensional
Structures
Common Use Cases
Error
Passing
Multi-Dimensional
Structures
@DmitryVinnik dvinnik.com
FP: Error Handling
@DmitryVinnik dvinnik.com
FP: Error Handling
@DmitryVinnik dvinnik.com
FP: Error Handling
@DmitryVinnik dvinnik.com
Let’s simplify
@DmitryVinnik dvinnik.com
FP: Error Handling
@DmitryVinnik dvinnik.com
FP: Error Handling
@DmitryVinnik dvinnik.com
FP: Error Handling
@DmitryVinnik dvinnik.com
Error
Handling
Error
Passing
Multi-Dimensional
Structures
Common Use Cases
Error
Passing
Multi-Dimensional
Structures
Error
Handling
Error
Passing
@DmitryVinnik dvinnik.com
FP: Error Passing
@DmitryVinnik dvinnik.com
Error
Handling
Error
Passing
Multi-Dimensional
Structures
Common Use Cases
Error
Passing
Multi-Dimensional
Structures
Error
Handling
Error
Passing
Error
Passing
Multi-Dimensional
Structures
@DmitryVinnik dvinnik.com
Multi-Dimensional
Structures: jOOQ/jOOL
Function
1...16
Consumer
1...16
Tuple
1...16
Sequences
@DmitryVinnik dvinnik.com
Functional Programming
looks great!
@DmitryVinnik dvinnik.com
But is there anything to
be avoided?
@DmitryVinnik dvinnik.com
Functional Programming:
Antipatterns
Overly
DRY
Early
Optimization
Impure
Functions
@DmitryVinnik dvinnik.com
Call For Action
Embrace FP
Review Your
Application
Continue
Learning
@DmitryVinnik dvinnik.com
Q/A
@DmitryVinnik dvinnik.com
About Speaker
Twitter: @DmitryVinnik
Blog: dvinnik.com
LinkedIn: in/dmitry-vinnik/
Email: dmitry@dvinnik.com

Do you even Function? Guiding Through Functional Interfaces

Editor's Notes

  • #5 How many of you have Macbook Pro? How many of you used AWS/Azure? Have you heard about Microservices
  • #6 How many of you have Macbook Pro? How many of you used AWS/Azure? Have you heard about Microservices
  • #7 How many of you have Macbook Pro? How many of you used AWS/Azure? Have you heard about Microservices Source: The Next Platform
  • #8 Change or die situation at this point World perspective
  • #9 Change or die situation at this point World perspective
  • #10 Change or die situation at this point World perspective
  • #19 “Always Design for Concurrency” - Pragmatic Programmer
  • #23 “Always Design for Concurrency” - Pragmatic Programmer
  • #27 “Always Design for Concurrency” - Pragmatic Programmer
  • #28 Good example is http clienta
  • #29 Good example is http clienta
  • #30 Good example is http clienta
  • #31 Good example is http clienta
  • #32 Good example is http clienta
  • #33 Good example is http clienta
  • #34 “Always Design for Concurrency” - Pragmatic Programmer
  • #43 Especially Complexity of Concurrency
  • #45 Especially Complexity of Concurrency
  • #49 Especially Complexity of Concurrency
  • #63 Not a data stracture Cannot reuse
  • #70 Note how field is not optional Always check before invoking get()
  • #85 https://softwareengineering.stackexchange.com/questions/39742/when-is-a-feature-considered-a-first-class-citizen-in-a-programming-language-p
  • #87 Java so flexible, need to be extra careful
  • #88 Java so flexible, need to be extra careful
  • #89 Java so flexible, need to be extra careful
  • #94 Even Runnable is FI Annotation is optional
  • #96  High Order Function Javadoc Function<T, R> Represents a function that accepts one argument and produces a result Predicate<T> Represents a predicate (boolean-valued function) of one argument Consumer<T> Represents an operation that accepts a single input argument and returns no result Supplier<T> Represents a supplier of results ... ... High Order Function Javadoc Function<T, R> Represents a function that accepts one argument and produces a result Predicate<T> Represents a predicate (boolean-valued function) of one argument Consumer<T> Represents an operation that accepts a single input argument and returns no result Supplier<T> Represents a supplier of results ... ...
  • #108  High Order Function Javadoc Function<T, R> Represents a function that accepts one argument and produces a result Predicate<T> Represents a predicate (boolean-valued function) of one argument Consumer<T> Represents an operation that accepts a single input argument and returns no result Supplier<T> Represents a supplier of results ... ... High Order Function Javadoc Function<T, R> Represents a function that accepts one argument and produces a result Predicate<T> Represents a predicate (boolean-valued function) of one argument Consumer<T> Represents an operation that accepts a single input argument and returns no result Supplier<T> Represents a supplier of results ... ...
  • #120  High Order Function Javadoc Function<T, R> Represents a function that accepts one argument and produces a result Predicate<T> Represents a predicate (boolean-valued function) of one argument Consumer<T> Represents an operation that accepts a single input argument and returns no result Supplier<T> Represents a supplier of results ... ... High Order Function Javadoc Function<T, R> Represents a function that accepts one argument and produces a result Predicate<T> Represents a predicate (boolean-valued function) of one argument Consumer<T> Represents an operation that accepts a single input argument and returns no result Supplier<T> Represents a supplier of results ... ...
  • #130  High Order Function Javadoc Function<T, R> Represents a function that accepts one argument and produces a result Predicate<T> Represents a predicate (boolean-valued function) of one argument Consumer<T> Represents an operation that accepts a single input argument and returns no result Supplier<T> Represents a supplier of results ... ... High Order Function Javadoc Function<T, R> Represents a function that accepts one argument and produces a result Predicate<T> Represents a predicate (boolean-valued function) of one argument Consumer<T> Represents an operation that accepts a single input argument and returns no result Supplier<T> Represents a supplier of results ... ...
  • #131 The simplest one Use as Executor
  • #132 The simplest one Use as Executor
  • #133 The simplest one Use as Executor
  • #134 The simplest one Use as Executor
  • #135 The simplest one Use as Executor
  • #136 The simplest one Use as Executor
  • #137 The simplest one Use as Executor
  • #138 The simplest one Use as Executor
  • #141 Making next step in FP
  • #142 Making next step in FP
  • #147 diffplug/durian
  • #148 diffplug/durian
  • #149 diffplug/durian
  • #151 diffplug/durian
  • #152 diffplug/durian
  • #153 diffplug/durian
  • #155 jOOQ/jOOL
  • #157 jOOQ/jOOL
  • #160 Early Optimization (Streams, or Parallel Streams)
  • #161 Need to embrace, move java 9,10,11 Look at reactive