SlideShare a Scribd company logo
Copyright 2011 - 2018, ThinkOpen S.r.l.Copyright 2011 - 2018, ThinkOpen S.r.l.
Why java moved a step forward to functional
programming?
Copyright 2011 - 2018, ThinkOpen S.r.l.
2
Copyright 2011 - 2018, ThinkOpen S.r.l.
3
Copyright 2011 - 2018, ThinkOpen S.r.l.Copyright 2011 - 2018, ThinkOpen S.r.l.
FUNCTIONAL PROGRAMMING
=
LOGIC
+
CATEGORY THEORY
+
COMPUTER SCIENCE
Copyright 2011 - 2018, ThinkOpen S.r.l.
Lambda calculus
λx.y
Alonzo Church
Copyright 2011 - 2018, ThinkOpen S.r.l.
Haskell vs. Ada vs. C++ vs. Awk vs. …
by Paul Hudak and Mark P. Jones
6
Copyright 2011 - 2018, ThinkOpen S.r.l.
7
Copyright 2011 - 2018, ThinkOpen S.r.l.
8
Copyright 2011 - 2018, ThinkOpen S.r.l.
3 BILLION DEVICES
9
Copyright 2011 - 2018, ThinkOpen S.r.l.
3 BILLION DEVICES
12 BILLION CORES
10
Copyright 2011 - 2018, ThinkOpen S.r.l.
3 BILLION DEVICES
12 BILLION CORES
24 BILLION THREADS
11
Copyright 2011 - 2018, ThinkOpen S.r.l.
Some people, when confronted with a problem,
think, “I know, I’ll use multi-threading”
Nothhw tpe yawrve o oblems.
12
Copyright 2011 - 2018, ThinkOpen S.r.l.
Semaphores
Thread pools
Callbacks
Synchronization
Locks
Atomic operations
Race condition
Functions
Functions
Functions
Functions
Functions
Functions
Functions
Copyright 2011 - 2018, ThinkOpen S.r.l.
Copyright 2011 - 2018, ThinkOpen S.r.l.
14
Copyright 2011 - 2018, ThinkOpen S.r.l.
15
Copyright 2011 - 2018, ThinkOpen S.r.l.
16
Copyright 2011 - 2018, ThinkOpen S.r.l.
17
Copyright 2011 - 2018, ThinkOpen S.r.l.Copyright 2011 - 2018, ThinkOpen S.r.l.
“Functional programming is
more a way of thinking than a
tool of set”
Neal Ford
Copyright 2011 - 2018, ThinkOpen S.r.l.
19
Execution in the
Kingdom of Nouns
by
Steve Yegge
Copyright 2011 - 2018, ThinkOpen S.r.l.
20
Copyright 2011 - 2018, ThinkOpen S.r.l.
21
button.setOnAction( new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
//…
}
});
Copyright 2011 - 2018, ThinkOpen S.r.l.
22
button.onAction( a -> /*...*/ );
Copyright 2011 - 2018, ThinkOpen S.r.l.
When the verbs were slaves
Object foo(int x) {
//..
return null;
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
When the verbs were slaves
Object foo(int x) {
//..
return null;
}
IDLE
Copyright 2011 - 2018, ThinkOpen S.r.l.
When the verbs were slaves
Object foo(int x) {
//..
return null;
}
Object foo(int x) {
//..
throw new RuntimeException();
//..
}
IDLE
Copyright 2011 - 2018, ThinkOpen S.r.l.
When the verbs were slaves
Object foo(int x) {
//..
return null;
}
Object foo(int x) {
//..
throw new RuntimeException();
//..
}
IDLE
LAIRS
Copyright 2011 - 2018, ThinkOpen S.r.l.
When the verbs were slaves
Object foo(int x) {
//..
return null;
}
Object foo(int x) {
//..
throw new RuntimeException();
//..
}
Object foo(int x) throws Exception{
//..
throw new Exception(...);
//..
}
IDLE
LAIRS
Copyright 2011 - 2018, ThinkOpen S.r.l.
When the verbs were slaves
Object foo(int x) {
//..
return null;
}
Object foo(int x) {
//..
throw new RuntimeException();
//..
}
Object foo(int x) throws Exception{
//..
throw new Exception(...);
//..
}
IDLE
LAIRS
CONFRONTATIONAL
Copyright 2011 - 2018, ThinkOpen S.r.l.
Verbs in the kingdom of Java 8
var lambda = x -> x+1;
Copyright 2011 - 2018, ThinkOpen S.r.l.
Verbs in the kingdom of Java 8
var f = t -> ...;
FIRST-CLASS CITIZEN
Copyright 2011 - 2018, ThinkOpen S.r.l.
Verbs in the kingdom of Java 8
var lambda = x -> x+1;
Optional<R> foo(T t) {
//….
}
FIRST-CLASS CITIZEN
Copyright 2011 - 2018, ThinkOpen S.r.l.
Verbs in the kingdom of Java 8
var lambda = x -> x+1;
Optional<R> foo(T t) {
//….
}
FIRST-CLASS CITIZEN
TRUSTY
Copyright 2011 - 2018, ThinkOpen S.r.l.
Verbs in the kingdom of Java 8
var true = (x,y) -> x;
var false = (x,y) -> y;
Copyright 2011 - 2018, ThinkOpen S.r.l.
Verbs in the kingdom of Java 8
var true = (x,y) -> x;
var false = (x,y) -> y;DATA
Copyright 2011 - 2018, ThinkOpen S.r.l.
#1 Parametrize everything
35
Copyright 2011 - 2018, ThinkOpen S.r.l.
36
#1 Parametrize everything
public class NumericalList <T extends Number>
extends ArrayList<T> {
public double sum() {
double sum = 0;
for(Number n : this)
sum += n.doubleValue();
return sum;
}
public double product() {
double product = 0;
for(Number n : this)
product *= n.doubleValue();
return product;
}
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
37
#1 Parametrize everything
public class NumericalList <T extends Number>
extends ArrayList<T> {
public double sum() {
double sum = 0;
for(Number n : this)
sum += n.doubleValue();
return sum;
}
public double product() {
double product = 0;
for(Number n : this)
product *= n.doubleValue();
return product;
}
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
38
#1 Parametrize everything
public class NumericalList <T extends Number>
extends ArrayList<T> {
public double sum() {
double sum = 0;
for(Number n : this)
sum += n.doubleValue();
return sum;
}
public double product() {
double product = 0;
for(Number n : this)
product *= n.doubleValue();
return product;
}
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
39
#1 Parametrize everything
public class NumericalList <T extends Number>
extends ArrayList<T> {
public double sum() {
double sum = 0;
for(Number n : this)
sum += n.doubleValue();
return sum;
}
public double product() {
double product = 0;
for(Number n : this)
product *= n.doubleValue();
return product;
}
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
40
#1 Parametrize everything
public class NumericalList <T extends Number>
extends ArrayList<T> {
private double fetch(double start, Operation op) {
double tmp = start;
for(Number n : this)
tmp = op.on(tmp, n.doubleValue());
return tmp;
}
public double sum() {
return fetch(0, new Operation {
@Override
public double on(double arg0, double arg1) {
return arg0 + arg1;
}
});
}
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
41
#1 Parametrize everything
public class NumericalList <T extends Number>
extends ArrayList<T> {
private double fetch(double start, Operation op) {
double tmp = start;
for(Number n : this)
tmp = op.on(tmp, n.doubleValue());
return tmp;
}
public double sum() {
return fetch(0, (arg0, arg1) -> arg0 + arg1);
}
interface Operation {
double op(double arg0, arg1);
}
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
42
#1 Parametrize everything
public class NumericalList <T extends Number>
extends ArrayList<T> {
private double fetch(double start, Operation op) {
double tmp = start;
for(Number n : this)
tmp = op.on(tmp, n.doubleValue());
return tmp;
}
public double sum() {
return fetch(0, (arg0, arg1) -> arg0 + arg1);
}
public double sumOnlyEven() {
return fetch(0, (arg0, arg1) -> arg1 % 2 == 0 ? arg0 + arg1 : arg0);
}
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
43
#1 Parametrize everything
interface Operation {
double op(double arg0, arg1);
boolean areValid(double arg0, arg1);
}
private double fetch(double start, Operation op) {
double tmp = start;
for(Number n : this)
if(op.areValid(tmp, arg1))
tmp = op.on(tmp, n.doubleValue());
return tmp;
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
44
#1 Parametrize everything
public double sum() {
return fetch(0, new Operation {
@Override
public double on(double arg0, double arg1) {
return arg0 + arg1;
}
@Override
public boolean areValid(double arg0, double arg1) {
return true;
}
});
}
public double multiply() {
return fetch(0, new Operation {
@Override
public double on(double arg0, double arg1) {
return arg0 * arg1;
}
@Override
public boolean areValid(double arg0, double arg1) {
return true;
}
});
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
45
#1 Parametrize everything
interface Operation {
double op(double arg0, arg1);
default boolean areValid(double arg0, double arg1) {
return true;
}
}
private double fetch(double start, Operation op) {
double tmp = 0;
for(Number n : this)
if(op.areValid(tmp, arg1))
tmp = op.on(tmp, n.doubleValue());
return tmp;
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
46
#1 Parametrize everything
public double sumOnlyEven() {
return fetch(0, new Operation {
@Override
public double on(double arg0, double arg1) {
return arg0 + arg1;
}
@Override
public boolean areValid(double arg0, double arg1) {
return (arg0 + arg1) % 2 == 0;
}
});
}
public double multiplyOnlyEven() {
return fetch(0, new Operation {
@Override
public double on(double arg0, double arg1) {
return arg0 * arg1;
}
@Override
public boolean areValid(double arg0, double arg1) {
return (arg0 + arg1) % 2 == 0;
}
});
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
#2 Functions types are interfaces
47
Copyright 2011 - 2018, ThinkOpen S.r.l.
#2 Functions types are interfaces
48
interface Operation {
double op(double arg0, double arg1);
boolean areValid(double arg0, double arg1);
}
Single Responsibility Principle
+
Interface Segregation Principle
@FunctionalInterface
interface Validator {
boolean areValid(double arg0,
double arg1);
}
@FunctionalInterface
interface Operation {
double op(double arg0,
double arg1);
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
#2 Functions types are interfaces
49
@FunctionalInterface
interface Validator {
boolean areValid(double arg0,
double arg1);
}
Single Responsibility Principle
+
Interface Segregation Principle
@FunctionalInterface
interface Validator {
boolean isValid(double arg0);
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
#2 Functions types are interfaces
50
@FunctionalInterface
interface Validator {
boolean areValid(double arg0,
double arg1);
}
Single Responsibility Principle
+
Interface Segregation Principle
@FunctionalInterface
interface …..{
boolean …..(double arg0);
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
#3 Monoids
building complexity starting from simplicity
51
Copyright 2011 - 2018, ThinkOpen S.r.l.
52
#3 Monoids building complexity starting from simplicity
class Sum implements Operation {
double op(double arg0, double arg1) { return arg0 + arg1; }
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
53
#3 Monoids building complexity starting from simplicity
class Sum implements Operation {
double op(double arg0, double arg1) { return arg0 + arg1; }
}
abstract class OperationDecorator implements Operation {
protected Operation operationToBeDecorated; // the Operation being decorated
public OperationDecorator (Operation operationToBeDecorated) {
this.operationToBeDecorated = operationToBeDecorated;
}
@Override
public double op(double arg0, double arg1) {
return operationToBeDecorated.op(arg0, arg1); //Delegation
}
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
54
#3 Monoids building complexity starting from simplicity
class Sum implements Operation {
double op(double arg0, double arg1) { return arg0 + arg1; }
}
abstract class OperationDecorator implements Operation {
protected Operation operationToBeDecorated; // the Operation being decorated
public OperationDecorator (Operation operationToBeDecorated) {
this.operationToBeDecorated = operationToBeDecorated;
}
@Override
public double op(double arg0, double arg1) {
return operationToBeDecorated.op(arg0, arg1); //Delegation
}
}
class DoubleAgumentsDecorator extends OperationDecorator {
public DoubleAgumentsDecorator (Operation opToBeDecorated) {
super(opToBeDecorated);
}
@Override
public double op(double arg0, double arg1) {
return super.op(arg0*2, arg1*2);
}
}
Copyright 2011 - 2018, ThinkOpen S.r.l.
55
#3 Monoids building complexity starting from simplicity
class Sum implements Operation {
double op(double arg0, double arg1) { return arg0 + arg1; }
}
abstract class OperationDecorator implements Operation {
protected Operation operationToBeDecorated; // the Operation being decorated
public OperationDecorator (Operation operationToBeDecorated) {
this.operationToBeDecorated = operationToBeDecorated;
}
@Override
public double op(double arg0, double arg1) {
return operationToBeDecorated.op(arg0, arg1); //Delegation
}
}
class DoubleAgumentsDecorator extends OperationDecorator {
public DoubleAgumentsDecorator (Operation opToBeDecorated) {
super(opToBeDecorated);
}
@Override
public double op(double arg0, double arg1) {
return super.op(arg0*2, arg1*2);
}
}
new CeilResultDecorator(new DoubleAgumentsDecorator(new Sum()))
Copyright 2011 - 2018, ThinkOpen S.r.l.
56
#3 Monoids building complexity starting from simplicity
class Sum implements Operation {
double op(double arg0, double arg1) { return arg0 + arg1; }
}
abstract class OperationDecorator implements Operation {
protected Operation operationToBeDecorated; // the Operation being decorated
public OperationDecorator (Operation operationToBeDecorated) {
this.operationToBeDecorated = operationToBeDecorated;
}
@Override
public double op(double arg0, double arg1) {
return operationToBeDecorated.op(arg0, arg1); //Delegation
}
}
class DoubleAgumentsDecorator extends OperationDecorator {
public DoubleAgumentsDecorator (Operation opToBeDecorated) {
super(opToBeDecorated);
}
@Override
public double op(double arg0, double arg1) {
return super.op(arg0*2, arg1*2);
}
}
new CeilResultDecorator(new DoubleAgumentsDecorator(new Sum()))
Copyright 2011 - 2018, ThinkOpen S.r.l.
57
#3 Monoids building complexity starting from simplicity
class Sum implements Operation {
double op(double arg0, double arg1) { return arg0 + arg1; }
}
abstract class OperationDecorator implements Operation {
protected Operation operationToBeDecorated; // the Operation being decorated
public OperationDecorator (Operation operationToBeDecorated) {
this.operationToBeDecorated = operationToBeDecorated;
}
@Override
public double op(double arg0, double arg1) {
return operationToBeDecorated.op(arg0, arg1); //Delegation
}
}
class DoubleAgumentsDecorator extends OperationDecorator {
public DoubleAgumentsDecorator (Operation opToBeDecorated) {
super(opToBeDecorated);
}
@Override
public double op(double arg0, double arg1) {
return super.op(arg0*2, arg1*2);
}
}
new BlahDecorator(new BlahBlahDecorator(new Sum()))
Copyright 2011 - 2018, ThinkOpen S.r.l.
58
#3 Monoids building complexity starting from simplicity
var doubles = (double arg) -> arg * 2;
var round = Math::ciel;
var add10 = (double arg) -> arg + 10;
var roundAdd10 = add10.andThen(round);
var veryComplexFunction = fstStep
.andThen(sndStep)
.andThen(Math::sin)
.andThen(...)
….
.andThen(...);
var veryVeryComplexFunction = fstVeryComplexFunction
.andThen(sndveryComplexFunction)
….
Copyright 2011 - 2018, ThinkOpen S.r.l.
double -> double
59
#3 Monoids building complexity starting from simplicity
doubles
double -> double
round
double -> double
add10
double -> double
double -> double
doubles
double -> double
add10
double -> double
double -> double
double
double -> double
add10
double -> double op
double -> double
Copyright 2011 - 2018, ThinkOpen S.r.l.
double -> double
60
#3 Monoids building complexity starting from simplicity
double -> int int -> String String -> double byte[] -> doubledouble -> byte[]
Copyright 2011 - 2018, ThinkOpen S.r.l.
61
#3 Monoids building complexity starting from simplicity
Composition pattern only works for functions
that have only one parameter
Copyright 2011 - 2018, ThinkOpen S.r.l.
62
#3 Monoids building complexity starting from simplicity
High order functions are not just function
that can be moved around but also functions
that returns functions
Copyright 2011 - 2018, ThinkOpen S.r.l.
#4 Partial applications and Closure
63
Copyright 2011 - 2018, ThinkOpen S.r.l.
64
let product a b = a * b;
#4 Partial applications and Closure
Copyright 2011 - 2018, ThinkOpen S.r.l.
65
product :: Num a => a -> a -> a
let product a b = a * b;
#4 Partial applications and Closure
Copyright 2011 - 2018, ThinkOpen S.r.l.
66
product :: Num a => a -> (a -> a)
let product a b = a * b;
#4 Partial applications and Closure
Copyright 2011 - 2018, ThinkOpen S.r.l.
67
#3 Monoids building complexity starting from simplicity
Function<Double, Function<Double,Double>>
product :: Num a => a -> (a -> a)
let product a b = a * b;
Copyright 2011 - 2018, ThinkOpen S.r.l.
68
#4 Partial applications and Closure
Function<Double, Function<Double,Double>>
product :: Num a => a -> (a -> a)
let product a b = a * b;
Function<Double, Function<Double,Double>>
product = a -> (b -> a * b);
Copyright 2011 - 2018, ThinkOpen S.r.l.
69
Function<Double, Function<Double,Double>>
product :: Num a => a -> (a -> a)
let product a b = a * b;
Function<Double, Function<Double,Double>>
product = a -> (b -> a * b);
var doubles = product(2);
var tenTimes = product(10);
double ten = tenTimes(1);
#4 Partial applications and Closure
Copyright 2011 - 2018, ThinkOpen S.r.l.
70
Foo newFoo(....) {
class Bar implements Foo { }
Bar b = new Bar();
//….
return b;
}
#4 Partial applications and Closure
Copyright 2011 - 2018, ThinkOpen S.r.l.
71
Function<T,R> foo(....) {
//….
var privateObj = ….;
return t -> {
//here I can use privateObj
return ret;
}
}
#4 Partial applications and Closure
Copyright 2011 - 2018, ThinkOpen S.r.l.
#5 Monads and map
72
Copyright 2011 - 2018, ThinkOpen S.r.l.
73
#5 Monads and map
if (a != null ) {
//…
if(b != null) {
//...
if(c != null) {
//…
if(...) {
//...
} else { return null; }
} else { return null; }
} else { return null; }
} else { return null; }
Copyright 2011 - 2018, ThinkOpen S.r.l.
74
#5 Monads and map
if (optionalA.isPresent()) {
var a = optionalA.get();
//…
if (optionalB.isPresent()) {
var b = optionalB.get();
//...
if (optionalC.isPresent()) {
var c = optionalC.get();
//…
if(...) {
//...
} else { return Optional<T>.empty(); }
} else { return Optional<T>.empty(); }
} else { return Optional<T>.empty(); }
} else { return Optional<T>.empty(); }
Copyright 2011 - 2018, ThinkOpen S.r.l.
75
#5 Monads and map
var ifSomeDo = f -> obj -> notNull(obj) ? f(obj) : null;
var sample = arg -> doSomething
.andThen(ifSomeDo(somethingElse)
.andThen(ifSomeDo(somethingElse2)
.andThen(ifSomeDo( a -> map(a))
.apply(arg);
Copyright 2011 - 2018, ThinkOpen S.r.l.
76
Optional’s univers
Values’ universe
#5 Monads and map
Copyright 2011 - 2018, ThinkOpen S.r.l.
77
Optional’s univers
Values’ universe
#5 Monads and map
Copyright 2011 - 2018, ThinkOpen S.r.l.
78
Optional’s univers
Values’ universe
#5 Monads and map
Copyright 2011 - 2018, ThinkOpen S.r.l.
79
Optional’s univers
Values’ universe
T -> R
Optional<T> -> Optional<R>
Optional.map
#5 Monads and map
Copyright 2011 - 2018, ThinkOpen S.r.l.
80
Optional<Optional<Optional<Optional<Optional<O
ptional<Optional<Optional<Optional<Optional<Op
tional<Optional<Optional<Value>>>>>>>>>>>>>
#5 Monads and map
Copyright 2011 - 2018, ThinkOpen S.r.l.
81
Optional.flatMap
#5 Monads and map
Copyright 2011 - 2018, ThinkOpen S.r.l.
82
#5 Monads and map
Copyright 2011 - 2018, ThinkOpen S.r.l.
83
#5 Monads and map
Copyright 2011 - 2018, ThinkOpen S.r.l.
84
#5 Monads and map
Copyright 2011 - 2018, ThinkOpen S.r.l.
85
#5 Monads and map
Copyright 2011 - 2018, ThinkOpen S.r.l.
86
#5 Monads and map
Copyright 2011 - 2018, ThinkOpen S.r.l.
87
#5 Monads and map
Copyright 2011 - 2018, ThinkOpen S.r.l.
88
#5 Monads and map
Copyright 2011 - 2018, ThinkOpen S.r.l.
89

More Related Content

What's hot (20)

PPTX
Java 7, 8 & 9 - Moving the language forward
Mario Fusco
 
PPTX
Computer programming 2 Lesson 10
MLG College of Learning, Inc
 
PDF
Functional programming java
Maneesh Chaturvedi
 
PDF
GC in C++0x [eng]
yak1ex
 
PDF
Data structure lab manual
nikshaikh786
 
PPTX
Functions in c++,
Padma Kannan
 
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Philip Schwarz
 
PDF
A taste of Functional Programming
Jordan Open Source Association
 
PDF
Scala categorytheory
Knoldus Inc.
 
PDF
Functional programming in C++
Alexandru Bolboaca
 
ODP
Clojure basics
Knoldus Inc.
 
ODP
Functional Programming With Scala
Knoldus Inc.
 
PDF
Functional programming 101
Maneesh Chaturvedi
 
PDF
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Philip Schwarz
 
PDF
Reasoning about laziness
Johan Tibell
 
PPT
Presentation
manogallery
 
PPT
Linq intro
Bình Trọng Án
 
PDF
Functional programming in Python
Colin Su
 
PDF
Let's make a contract: the art of designing a Java API
Mario Fusco
 
PDF
Applicative Functor - Part 3
Philip Schwarz
 
Java 7, 8 & 9 - Moving the language forward
Mario Fusco
 
Computer programming 2 Lesson 10
MLG College of Learning, Inc
 
Functional programming java
Maneesh Chaturvedi
 
GC in C++0x [eng]
yak1ex
 
Data structure lab manual
nikshaikh786
 
Functions in c++,
Padma Kannan
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Philip Schwarz
 
A taste of Functional Programming
Jordan Open Source Association
 
Scala categorytheory
Knoldus Inc.
 
Functional programming in C++
Alexandru Bolboaca
 
Clojure basics
Knoldus Inc.
 
Functional Programming With Scala
Knoldus Inc.
 
Functional programming 101
Maneesh Chaturvedi
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Philip Schwarz
 
Reasoning about laziness
Johan Tibell
 
Presentation
manogallery
 
Linq intro
Bình Trọng Án
 
Functional programming in Python
Colin Su
 
Let's make a contract: the art of designing a Java API
Mario Fusco
 
Applicative Functor - Part 3
Philip Schwarz
 

Similar to "Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu (20)

PPTX
Stop that!
Doug Sparling
 
PPTX
Is java8 a true functional programming language
SQLI
 
PPTX
Is java8a truefunctionallanguage
Samir Chekkal
 
PPTX
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Codemotion
 
PPTX
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Andrzej Jóźwiak
 
PDF
Bologna Developer Zone - About Kotlin
Marco Vasapollo
 
PPTX
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Matthew Farwell
 
PDF
SeneJug java_8_prez_122015
senejug
 
PPTX
Scala Back to Basics: Type Classes
Tomer Gabel
 
PDF
Effective Java with Groovy - How Language can Influence Good Practices
Naresha K
 
PPTX
A well-typed program never goes wrong
Julien Wetterwald
 
PDF
Poniendo Kotlin en producción a palos (Kotlin in production, the hard way)
Andrés Viedma Peláez
 
PDF
Evolving with Java - How to remain Relevant and Effective
Naresha K
 
PDF
Evolving with Java - How to Remain Effective
Naresha K
 
KEY
Clean code and Code Smells
Mario Sangiorgio
 
PDF
OOP and FP
Mario Fusco
 
PDF
Functional OO programming (as part of the the PTT lecture)
Ralf Laemmel
 
PPTX
Introduction to kotlin + spring boot demo
Muhammad Abdullah
 
PDF
Functional Programming 101 for Java 7 Developers
Jayaram Sankaranarayanan
 
PDF
SOLID Java Code
Omar Bashir
 
Stop that!
Doug Sparling
 
Is java8 a true functional programming language
SQLI
 
Is java8a truefunctionallanguage
Samir Chekkal
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Codemotion
 
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Andrzej Jóźwiak
 
Bologna Developer Zone - About Kotlin
Marco Vasapollo
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Matthew Farwell
 
SeneJug java_8_prez_122015
senejug
 
Scala Back to Basics: Type Classes
Tomer Gabel
 
Effective Java with Groovy - How Language can Influence Good Practices
Naresha K
 
A well-typed program never goes wrong
Julien Wetterwald
 
Poniendo Kotlin en producción a palos (Kotlin in production, the hard way)
Andrés Viedma Peláez
 
Evolving with Java - How to remain Relevant and Effective
Naresha K
 
Evolving with Java - How to Remain Effective
Naresha K
 
Clean code and Code Smells
Mario Sangiorgio
 
OOP and FP
Mario Fusco
 
Functional OO programming (as part of the the PTT lecture)
Ralf Laemmel
 
Introduction to kotlin + spring boot demo
Muhammad Abdullah
 
Functional Programming 101 for Java 7 Developers
Jayaram Sankaranarayanan
 
SOLID Java Code
Omar Bashir
 
Ad

More from ThinkOpen (20)

PDF
Discover Facilitation: gestire le riunioni in modo efficace
ThinkOpen
 
PDF
Infrastructure as a code: a cloud approach
ThinkOpen
 
PDF
Smart Signage: la nuova digital experience
ThinkOpen
 
PDF
I Graph Database: analisi del comportamento degli utenti
ThinkOpen
 
PDF
2019: Odissea nell'e-commerce
ThinkOpen
 
PDF
Guida galattica a Javascript
ThinkOpen
 
PDF
Java 8 -12: da Oracle a Eclipse. Due anni e una rivoluzione
ThinkOpen
 
PDF
Amazon Alexa vs Google Home. Quale scegliere? Funzionalità e usi
ThinkOpen
 
PDF
Amazon Web Services - Le potenzialità di AWS e il mondo di Amazon Alexa by Ni...
ThinkOpen
 
PPTX
Polymer 3.0 by Michele Gallotti
ThinkOpen
 
PDF
"Odoo: l'open source che fa tremare SAP" by Davide Davin e Nicola Napolitano
ThinkOpen
 
PDF
"Configuration Manager: il ruolo nel ciclo di vita del software" by Omar Rossini
ThinkOpen
 
PDF
"Google Home: how to make Google do it" by Theodor Dumitrescu e Gianfranco Bo...
ThinkOpen
 
PDF
"ThinkOpen Agile Days - #Day3" by Donato Andrisani e Giuseppe Trotta
ThinkOpen
 
PDF
"Reactive programming" by Theodor Dumitrescu & Gianfranco Bottiglieri
ThinkOpen
 
PDF
"GDPR: cos'è e come funziona" by Francesco Puglisi
ThinkOpen
 
PDF
"ThinkOpen Agile Days - #Day2" by Donato Andrisani e Giuseppe Trotta
ThinkOpen
 
PDF
"ThinkOpen Agile Days - #Day" by Giuseppe Trotta
ThinkOpen
 
PDF
"React Native" by Vanessa Leo e Roberto Brogi
ThinkOpen
 
PDF
"How to... React" by Luca Perna
ThinkOpen
 
Discover Facilitation: gestire le riunioni in modo efficace
ThinkOpen
 
Infrastructure as a code: a cloud approach
ThinkOpen
 
Smart Signage: la nuova digital experience
ThinkOpen
 
I Graph Database: analisi del comportamento degli utenti
ThinkOpen
 
2019: Odissea nell'e-commerce
ThinkOpen
 
Guida galattica a Javascript
ThinkOpen
 
Java 8 -12: da Oracle a Eclipse. Due anni e una rivoluzione
ThinkOpen
 
Amazon Alexa vs Google Home. Quale scegliere? Funzionalità e usi
ThinkOpen
 
Amazon Web Services - Le potenzialità di AWS e il mondo di Amazon Alexa by Ni...
ThinkOpen
 
Polymer 3.0 by Michele Gallotti
ThinkOpen
 
"Odoo: l'open source che fa tremare SAP" by Davide Davin e Nicola Napolitano
ThinkOpen
 
"Configuration Manager: il ruolo nel ciclo di vita del software" by Omar Rossini
ThinkOpen
 
"Google Home: how to make Google do it" by Theodor Dumitrescu e Gianfranco Bo...
ThinkOpen
 
"ThinkOpen Agile Days - #Day3" by Donato Andrisani e Giuseppe Trotta
ThinkOpen
 
"Reactive programming" by Theodor Dumitrescu & Gianfranco Bottiglieri
ThinkOpen
 
"GDPR: cos'è e come funziona" by Francesco Puglisi
ThinkOpen
 
"ThinkOpen Agile Days - #Day2" by Donato Andrisani e Giuseppe Trotta
ThinkOpen
 
"ThinkOpen Agile Days - #Day" by Giuseppe Trotta
ThinkOpen
 
"React Native" by Vanessa Leo e Roberto Brogi
ThinkOpen
 
"How to... React" by Luca Perna
ThinkOpen
 
Ad

Recently uploaded (20)

PDF
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
PDF
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
PDF
AI Software Development Process, Strategies and Challenges
Net-Craft.com
 
PDF
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
PPTX
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
PPTX
computer forensics encase emager app exp6 1.pptx
ssuser343e92
 
PPTX
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
PDF
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
PPTX
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
 
PDF
Dealing with JSON in the relational world
Andres Almiray
 
PPTX
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
PPTX
ManageIQ - Sprint 264 Review - Slide Deck
ManageIQ
 
PDF
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
 
PPTX
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
PDF
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
 
PPTX
Introduction to web development | MERN Stack
JosephLiyon
 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
 
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
AI Software Development Process, Strategies and Challenges
Net-Craft.com
 
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
computer forensics encase emager app exp6 1.pptx
ssuser343e92
 
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
 
Dealing with JSON in the relational world
Andres Almiray
 
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
ManageIQ - Sprint 264 Review - Slide Deck
ManageIQ
 
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
 
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
 
Introduction to web development | MERN Stack
JosephLiyon
 
Automated Test Case Repair Using Language Models
Lionel Briand
 

"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu

  • 1. Copyright 2011 - 2018, ThinkOpen S.r.l.Copyright 2011 - 2018, ThinkOpen S.r.l. Why java moved a step forward to functional programming?
  • 2. Copyright 2011 - 2018, ThinkOpen S.r.l. 2
  • 3. Copyright 2011 - 2018, ThinkOpen S.r.l. 3
  • 4. Copyright 2011 - 2018, ThinkOpen S.r.l.Copyright 2011 - 2018, ThinkOpen S.r.l. FUNCTIONAL PROGRAMMING = LOGIC + CATEGORY THEORY + COMPUTER SCIENCE
  • 5. Copyright 2011 - 2018, ThinkOpen S.r.l. Lambda calculus λx.y Alonzo Church
  • 6. Copyright 2011 - 2018, ThinkOpen S.r.l. Haskell vs. Ada vs. C++ vs. Awk vs. … by Paul Hudak and Mark P. Jones 6
  • 7. Copyright 2011 - 2018, ThinkOpen S.r.l. 7
  • 8. Copyright 2011 - 2018, ThinkOpen S.r.l. 8
  • 9. Copyright 2011 - 2018, ThinkOpen S.r.l. 3 BILLION DEVICES 9
  • 10. Copyright 2011 - 2018, ThinkOpen S.r.l. 3 BILLION DEVICES 12 BILLION CORES 10
  • 11. Copyright 2011 - 2018, ThinkOpen S.r.l. 3 BILLION DEVICES 12 BILLION CORES 24 BILLION THREADS 11
  • 12. Copyright 2011 - 2018, ThinkOpen S.r.l. Some people, when confronted with a problem, think, “I know, I’ll use multi-threading” Nothhw tpe yawrve o oblems. 12
  • 13. Copyright 2011 - 2018, ThinkOpen S.r.l. Semaphores Thread pools Callbacks Synchronization Locks Atomic operations Race condition Functions Functions Functions Functions Functions Functions Functions Copyright 2011 - 2018, ThinkOpen S.r.l.
  • 14. Copyright 2011 - 2018, ThinkOpen S.r.l. 14
  • 15. Copyright 2011 - 2018, ThinkOpen S.r.l. 15
  • 16. Copyright 2011 - 2018, ThinkOpen S.r.l. 16
  • 17. Copyright 2011 - 2018, ThinkOpen S.r.l. 17
  • 18. Copyright 2011 - 2018, ThinkOpen S.r.l.Copyright 2011 - 2018, ThinkOpen S.r.l. “Functional programming is more a way of thinking than a tool of set” Neal Ford
  • 19. Copyright 2011 - 2018, ThinkOpen S.r.l. 19 Execution in the Kingdom of Nouns by Steve Yegge
  • 20. Copyright 2011 - 2018, ThinkOpen S.r.l. 20
  • 21. Copyright 2011 - 2018, ThinkOpen S.r.l. 21 button.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { //… } });
  • 22. Copyright 2011 - 2018, ThinkOpen S.r.l. 22 button.onAction( a -> /*...*/ );
  • 23. Copyright 2011 - 2018, ThinkOpen S.r.l. When the verbs were slaves Object foo(int x) { //.. return null; }
  • 24. Copyright 2011 - 2018, ThinkOpen S.r.l. When the verbs were slaves Object foo(int x) { //.. return null; } IDLE
  • 25. Copyright 2011 - 2018, ThinkOpen S.r.l. When the verbs were slaves Object foo(int x) { //.. return null; } Object foo(int x) { //.. throw new RuntimeException(); //.. } IDLE
  • 26. Copyright 2011 - 2018, ThinkOpen S.r.l. When the verbs were slaves Object foo(int x) { //.. return null; } Object foo(int x) { //.. throw new RuntimeException(); //.. } IDLE LAIRS
  • 27. Copyright 2011 - 2018, ThinkOpen S.r.l. When the verbs were slaves Object foo(int x) { //.. return null; } Object foo(int x) { //.. throw new RuntimeException(); //.. } Object foo(int x) throws Exception{ //.. throw new Exception(...); //.. } IDLE LAIRS
  • 28. Copyright 2011 - 2018, ThinkOpen S.r.l. When the verbs were slaves Object foo(int x) { //.. return null; } Object foo(int x) { //.. throw new RuntimeException(); //.. } Object foo(int x) throws Exception{ //.. throw new Exception(...); //.. } IDLE LAIRS CONFRONTATIONAL
  • 29. Copyright 2011 - 2018, ThinkOpen S.r.l. Verbs in the kingdom of Java 8 var lambda = x -> x+1;
  • 30. Copyright 2011 - 2018, ThinkOpen S.r.l. Verbs in the kingdom of Java 8 var f = t -> ...; FIRST-CLASS CITIZEN
  • 31. Copyright 2011 - 2018, ThinkOpen S.r.l. Verbs in the kingdom of Java 8 var lambda = x -> x+1; Optional<R> foo(T t) { //…. } FIRST-CLASS CITIZEN
  • 32. Copyright 2011 - 2018, ThinkOpen S.r.l. Verbs in the kingdom of Java 8 var lambda = x -> x+1; Optional<R> foo(T t) { //…. } FIRST-CLASS CITIZEN TRUSTY
  • 33. Copyright 2011 - 2018, ThinkOpen S.r.l. Verbs in the kingdom of Java 8 var true = (x,y) -> x; var false = (x,y) -> y;
  • 34. Copyright 2011 - 2018, ThinkOpen S.r.l. Verbs in the kingdom of Java 8 var true = (x,y) -> x; var false = (x,y) -> y;DATA
  • 35. Copyright 2011 - 2018, ThinkOpen S.r.l. #1 Parametrize everything 35
  • 36. Copyright 2011 - 2018, ThinkOpen S.r.l. 36 #1 Parametrize everything public class NumericalList <T extends Number> extends ArrayList<T> { public double sum() { double sum = 0; for(Number n : this) sum += n.doubleValue(); return sum; } public double product() { double product = 0; for(Number n : this) product *= n.doubleValue(); return product; } }
  • 37. Copyright 2011 - 2018, ThinkOpen S.r.l. 37 #1 Parametrize everything public class NumericalList <T extends Number> extends ArrayList<T> { public double sum() { double sum = 0; for(Number n : this) sum += n.doubleValue(); return sum; } public double product() { double product = 0; for(Number n : this) product *= n.doubleValue(); return product; } }
  • 38. Copyright 2011 - 2018, ThinkOpen S.r.l. 38 #1 Parametrize everything public class NumericalList <T extends Number> extends ArrayList<T> { public double sum() { double sum = 0; for(Number n : this) sum += n.doubleValue(); return sum; } public double product() { double product = 0; for(Number n : this) product *= n.doubleValue(); return product; } }
  • 39. Copyright 2011 - 2018, ThinkOpen S.r.l. 39 #1 Parametrize everything public class NumericalList <T extends Number> extends ArrayList<T> { public double sum() { double sum = 0; for(Number n : this) sum += n.doubleValue(); return sum; } public double product() { double product = 0; for(Number n : this) product *= n.doubleValue(); return product; } }
  • 40. Copyright 2011 - 2018, ThinkOpen S.r.l. 40 #1 Parametrize everything public class NumericalList <T extends Number> extends ArrayList<T> { private double fetch(double start, Operation op) { double tmp = start; for(Number n : this) tmp = op.on(tmp, n.doubleValue()); return tmp; } public double sum() { return fetch(0, new Operation { @Override public double on(double arg0, double arg1) { return arg0 + arg1; } }); } }
  • 41. Copyright 2011 - 2018, ThinkOpen S.r.l. 41 #1 Parametrize everything public class NumericalList <T extends Number> extends ArrayList<T> { private double fetch(double start, Operation op) { double tmp = start; for(Number n : this) tmp = op.on(tmp, n.doubleValue()); return tmp; } public double sum() { return fetch(0, (arg0, arg1) -> arg0 + arg1); } interface Operation { double op(double arg0, arg1); } }
  • 42. Copyright 2011 - 2018, ThinkOpen S.r.l. 42 #1 Parametrize everything public class NumericalList <T extends Number> extends ArrayList<T> { private double fetch(double start, Operation op) { double tmp = start; for(Number n : this) tmp = op.on(tmp, n.doubleValue()); return tmp; } public double sum() { return fetch(0, (arg0, arg1) -> arg0 + arg1); } public double sumOnlyEven() { return fetch(0, (arg0, arg1) -> arg1 % 2 == 0 ? arg0 + arg1 : arg0); } }
  • 43. Copyright 2011 - 2018, ThinkOpen S.r.l. 43 #1 Parametrize everything interface Operation { double op(double arg0, arg1); boolean areValid(double arg0, arg1); } private double fetch(double start, Operation op) { double tmp = start; for(Number n : this) if(op.areValid(tmp, arg1)) tmp = op.on(tmp, n.doubleValue()); return tmp; }
  • 44. Copyright 2011 - 2018, ThinkOpen S.r.l. 44 #1 Parametrize everything public double sum() { return fetch(0, new Operation { @Override public double on(double arg0, double arg1) { return arg0 + arg1; } @Override public boolean areValid(double arg0, double arg1) { return true; } }); } public double multiply() { return fetch(0, new Operation { @Override public double on(double arg0, double arg1) { return arg0 * arg1; } @Override public boolean areValid(double arg0, double arg1) { return true; } }); }
  • 45. Copyright 2011 - 2018, ThinkOpen S.r.l. 45 #1 Parametrize everything interface Operation { double op(double arg0, arg1); default boolean areValid(double arg0, double arg1) { return true; } } private double fetch(double start, Operation op) { double tmp = 0; for(Number n : this) if(op.areValid(tmp, arg1)) tmp = op.on(tmp, n.doubleValue()); return tmp; }
  • 46. Copyright 2011 - 2018, ThinkOpen S.r.l. 46 #1 Parametrize everything public double sumOnlyEven() { return fetch(0, new Operation { @Override public double on(double arg0, double arg1) { return arg0 + arg1; } @Override public boolean areValid(double arg0, double arg1) { return (arg0 + arg1) % 2 == 0; } }); } public double multiplyOnlyEven() { return fetch(0, new Operation { @Override public double on(double arg0, double arg1) { return arg0 * arg1; } @Override public boolean areValid(double arg0, double arg1) { return (arg0 + arg1) % 2 == 0; } }); }
  • 47. Copyright 2011 - 2018, ThinkOpen S.r.l. #2 Functions types are interfaces 47
  • 48. Copyright 2011 - 2018, ThinkOpen S.r.l. #2 Functions types are interfaces 48 interface Operation { double op(double arg0, double arg1); boolean areValid(double arg0, double arg1); } Single Responsibility Principle + Interface Segregation Principle @FunctionalInterface interface Validator { boolean areValid(double arg0, double arg1); } @FunctionalInterface interface Operation { double op(double arg0, double arg1); }
  • 49. Copyright 2011 - 2018, ThinkOpen S.r.l. #2 Functions types are interfaces 49 @FunctionalInterface interface Validator { boolean areValid(double arg0, double arg1); } Single Responsibility Principle + Interface Segregation Principle @FunctionalInterface interface Validator { boolean isValid(double arg0); }
  • 50. Copyright 2011 - 2018, ThinkOpen S.r.l. #2 Functions types are interfaces 50 @FunctionalInterface interface Validator { boolean areValid(double arg0, double arg1); } Single Responsibility Principle + Interface Segregation Principle @FunctionalInterface interface …..{ boolean …..(double arg0); }
  • 51. Copyright 2011 - 2018, ThinkOpen S.r.l. #3 Monoids building complexity starting from simplicity 51
  • 52. Copyright 2011 - 2018, ThinkOpen S.r.l. 52 #3 Monoids building complexity starting from simplicity class Sum implements Operation { double op(double arg0, double arg1) { return arg0 + arg1; } }
  • 53. Copyright 2011 - 2018, ThinkOpen S.r.l. 53 #3 Monoids building complexity starting from simplicity class Sum implements Operation { double op(double arg0, double arg1) { return arg0 + arg1; } } abstract class OperationDecorator implements Operation { protected Operation operationToBeDecorated; // the Operation being decorated public OperationDecorator (Operation operationToBeDecorated) { this.operationToBeDecorated = operationToBeDecorated; } @Override public double op(double arg0, double arg1) { return operationToBeDecorated.op(arg0, arg1); //Delegation } }
  • 54. Copyright 2011 - 2018, ThinkOpen S.r.l. 54 #3 Monoids building complexity starting from simplicity class Sum implements Operation { double op(double arg0, double arg1) { return arg0 + arg1; } } abstract class OperationDecorator implements Operation { protected Operation operationToBeDecorated; // the Operation being decorated public OperationDecorator (Operation operationToBeDecorated) { this.operationToBeDecorated = operationToBeDecorated; } @Override public double op(double arg0, double arg1) { return operationToBeDecorated.op(arg0, arg1); //Delegation } } class DoubleAgumentsDecorator extends OperationDecorator { public DoubleAgumentsDecorator (Operation opToBeDecorated) { super(opToBeDecorated); } @Override public double op(double arg0, double arg1) { return super.op(arg0*2, arg1*2); } }
  • 55. Copyright 2011 - 2018, ThinkOpen S.r.l. 55 #3 Monoids building complexity starting from simplicity class Sum implements Operation { double op(double arg0, double arg1) { return arg0 + arg1; } } abstract class OperationDecorator implements Operation { protected Operation operationToBeDecorated; // the Operation being decorated public OperationDecorator (Operation operationToBeDecorated) { this.operationToBeDecorated = operationToBeDecorated; } @Override public double op(double arg0, double arg1) { return operationToBeDecorated.op(arg0, arg1); //Delegation } } class DoubleAgumentsDecorator extends OperationDecorator { public DoubleAgumentsDecorator (Operation opToBeDecorated) { super(opToBeDecorated); } @Override public double op(double arg0, double arg1) { return super.op(arg0*2, arg1*2); } } new CeilResultDecorator(new DoubleAgumentsDecorator(new Sum()))
  • 56. Copyright 2011 - 2018, ThinkOpen S.r.l. 56 #3 Monoids building complexity starting from simplicity class Sum implements Operation { double op(double arg0, double arg1) { return arg0 + arg1; } } abstract class OperationDecorator implements Operation { protected Operation operationToBeDecorated; // the Operation being decorated public OperationDecorator (Operation operationToBeDecorated) { this.operationToBeDecorated = operationToBeDecorated; } @Override public double op(double arg0, double arg1) { return operationToBeDecorated.op(arg0, arg1); //Delegation } } class DoubleAgumentsDecorator extends OperationDecorator { public DoubleAgumentsDecorator (Operation opToBeDecorated) { super(opToBeDecorated); } @Override public double op(double arg0, double arg1) { return super.op(arg0*2, arg1*2); } } new CeilResultDecorator(new DoubleAgumentsDecorator(new Sum()))
  • 57. Copyright 2011 - 2018, ThinkOpen S.r.l. 57 #3 Monoids building complexity starting from simplicity class Sum implements Operation { double op(double arg0, double arg1) { return arg0 + arg1; } } abstract class OperationDecorator implements Operation { protected Operation operationToBeDecorated; // the Operation being decorated public OperationDecorator (Operation operationToBeDecorated) { this.operationToBeDecorated = operationToBeDecorated; } @Override public double op(double arg0, double arg1) { return operationToBeDecorated.op(arg0, arg1); //Delegation } } class DoubleAgumentsDecorator extends OperationDecorator { public DoubleAgumentsDecorator (Operation opToBeDecorated) { super(opToBeDecorated); } @Override public double op(double arg0, double arg1) { return super.op(arg0*2, arg1*2); } } new BlahDecorator(new BlahBlahDecorator(new Sum()))
  • 58. Copyright 2011 - 2018, ThinkOpen S.r.l. 58 #3 Monoids building complexity starting from simplicity var doubles = (double arg) -> arg * 2; var round = Math::ciel; var add10 = (double arg) -> arg + 10; var roundAdd10 = add10.andThen(round); var veryComplexFunction = fstStep .andThen(sndStep) .andThen(Math::sin) .andThen(...) …. .andThen(...); var veryVeryComplexFunction = fstVeryComplexFunction .andThen(sndveryComplexFunction) ….
  • 59. Copyright 2011 - 2018, ThinkOpen S.r.l. double -> double 59 #3 Monoids building complexity starting from simplicity doubles double -> double round double -> double add10 double -> double double -> double doubles double -> double add10 double -> double double -> double double double -> double add10 double -> double op double -> double
  • 60. Copyright 2011 - 2018, ThinkOpen S.r.l. double -> double 60 #3 Monoids building complexity starting from simplicity double -> int int -> String String -> double byte[] -> doubledouble -> byte[]
  • 61. Copyright 2011 - 2018, ThinkOpen S.r.l. 61 #3 Monoids building complexity starting from simplicity Composition pattern only works for functions that have only one parameter
  • 62. Copyright 2011 - 2018, ThinkOpen S.r.l. 62 #3 Monoids building complexity starting from simplicity High order functions are not just function that can be moved around but also functions that returns functions
  • 63. Copyright 2011 - 2018, ThinkOpen S.r.l. #4 Partial applications and Closure 63
  • 64. Copyright 2011 - 2018, ThinkOpen S.r.l. 64 let product a b = a * b; #4 Partial applications and Closure
  • 65. Copyright 2011 - 2018, ThinkOpen S.r.l. 65 product :: Num a => a -> a -> a let product a b = a * b; #4 Partial applications and Closure
  • 66. Copyright 2011 - 2018, ThinkOpen S.r.l. 66 product :: Num a => a -> (a -> a) let product a b = a * b; #4 Partial applications and Closure
  • 67. Copyright 2011 - 2018, ThinkOpen S.r.l. 67 #3 Monoids building complexity starting from simplicity Function<Double, Function<Double,Double>> product :: Num a => a -> (a -> a) let product a b = a * b;
  • 68. Copyright 2011 - 2018, ThinkOpen S.r.l. 68 #4 Partial applications and Closure Function<Double, Function<Double,Double>> product :: Num a => a -> (a -> a) let product a b = a * b; Function<Double, Function<Double,Double>> product = a -> (b -> a * b);
  • 69. Copyright 2011 - 2018, ThinkOpen S.r.l. 69 Function<Double, Function<Double,Double>> product :: Num a => a -> (a -> a) let product a b = a * b; Function<Double, Function<Double,Double>> product = a -> (b -> a * b); var doubles = product(2); var tenTimes = product(10); double ten = tenTimes(1); #4 Partial applications and Closure
  • 70. Copyright 2011 - 2018, ThinkOpen S.r.l. 70 Foo newFoo(....) { class Bar implements Foo { } Bar b = new Bar(); //…. return b; } #4 Partial applications and Closure
  • 71. Copyright 2011 - 2018, ThinkOpen S.r.l. 71 Function<T,R> foo(....) { //…. var privateObj = ….; return t -> { //here I can use privateObj return ret; } } #4 Partial applications and Closure
  • 72. Copyright 2011 - 2018, ThinkOpen S.r.l. #5 Monads and map 72
  • 73. Copyright 2011 - 2018, ThinkOpen S.r.l. 73 #5 Monads and map if (a != null ) { //… if(b != null) { //... if(c != null) { //… if(...) { //... } else { return null; } } else { return null; } } else { return null; } } else { return null; }
  • 74. Copyright 2011 - 2018, ThinkOpen S.r.l. 74 #5 Monads and map if (optionalA.isPresent()) { var a = optionalA.get(); //… if (optionalB.isPresent()) { var b = optionalB.get(); //... if (optionalC.isPresent()) { var c = optionalC.get(); //… if(...) { //... } else { return Optional<T>.empty(); } } else { return Optional<T>.empty(); } } else { return Optional<T>.empty(); } } else { return Optional<T>.empty(); }
  • 75. Copyright 2011 - 2018, ThinkOpen S.r.l. 75 #5 Monads and map var ifSomeDo = f -> obj -> notNull(obj) ? f(obj) : null; var sample = arg -> doSomething .andThen(ifSomeDo(somethingElse) .andThen(ifSomeDo(somethingElse2) .andThen(ifSomeDo( a -> map(a)) .apply(arg);
  • 76. Copyright 2011 - 2018, ThinkOpen S.r.l. 76 Optional’s univers Values’ universe #5 Monads and map
  • 77. Copyright 2011 - 2018, ThinkOpen S.r.l. 77 Optional’s univers Values’ universe #5 Monads and map
  • 78. Copyright 2011 - 2018, ThinkOpen S.r.l. 78 Optional’s univers Values’ universe #5 Monads and map
  • 79. Copyright 2011 - 2018, ThinkOpen S.r.l. 79 Optional’s univers Values’ universe T -> R Optional<T> -> Optional<R> Optional.map #5 Monads and map
  • 80. Copyright 2011 - 2018, ThinkOpen S.r.l. 80 Optional<Optional<Optional<Optional<Optional<O ptional<Optional<Optional<Optional<Optional<Op tional<Optional<Optional<Value>>>>>>>>>>>>> #5 Monads and map
  • 81. Copyright 2011 - 2018, ThinkOpen S.r.l. 81 Optional.flatMap #5 Monads and map
  • 82. Copyright 2011 - 2018, ThinkOpen S.r.l. 82 #5 Monads and map
  • 83. Copyright 2011 - 2018, ThinkOpen S.r.l. 83 #5 Monads and map
  • 84. Copyright 2011 - 2018, ThinkOpen S.r.l. 84 #5 Monads and map
  • 85. Copyright 2011 - 2018, ThinkOpen S.r.l. 85 #5 Monads and map
  • 86. Copyright 2011 - 2018, ThinkOpen S.r.l. 86 #5 Monads and map
  • 87. Copyright 2011 - 2018, ThinkOpen S.r.l. 87 #5 Monads and map
  • 88. Copyright 2011 - 2018, ThinkOpen S.r.l. 88 #5 Monads and map
  • 89. Copyright 2011 - 2018, ThinkOpen S.r.l. 89