Embrace Java8 - EclipseCon 2020 › ... › Java8Tutorial.pdf · Embrace Java8. Why functi!al?...
Transcript of Embrace Java8 - EclipseCon 2020 › ... › Java8Tutorial.pdf · Embrace Java8. Why functi!al?...
Functional Programmingwith Eclipse
Sebastian Zarnekow itemis
Embrace Java8
Why functional?
Demo
Imperative Programming is about How to do it
Functional Programming focusses onWhat to do
Mutationvs
Transformation
Java Meets
Immutability
equals() hashCode()
toString() <ctor>()
final fields clone()
copy ctor
AutoValue Lombok
Pureness
No Side Effects Referential Transparency Caching & Memoization
Lazy EvaluationComposition
and
Use Small Atomic Pieces Assemble Rich Behavior cf. Pipes & Filters
BehaviorParameterization
Groceries:10 Eggs Milk Bread Butter
Houseworkhoover yr room go with the dog
Houseworkhoover yr room go with the dog Pass Behavior / Instructions Around
Code is Data cf. Strategy Pattern
Separation of Concerns
https://twitter.com/mariofusco/status/572332914895134720/photo/1
LambdasStreams
and
• 2006 - “We’ll never have lambdas in Java”(James Gosling)
• 2007 - 3 Different Proposals for Lambdas
• 2008 - “We’ll never have lambdas in Java”(Mark Reinhold, Devoxx)
• 2009 - Project Lambda is Alive (JSR 335)(Mark Reinhold, Devoxx)
History Lesson (Credits to Mario Fusco, Codemotion)
λ
Syntaxs -> s.length()
(int x, int y) -> x+y
() -> 42
λ
Syntaxλ(x, y, z) -> { if (y == z) return x; else { int result = y; for (int i = 1; i < z; i++) result *= i; return result; } }
Method References
String::length System::getProperty super::toString ArrayList::new double[]::new "abc"::length System.out::println
Method References
ArrayList::new new ArrayList<>(5) new ArrayList<>(myCollection) new ArrayList<>()
Poly Expressions
• Implementations in Interfaces
• Default Methods
• Static Methods
• Only Behavior is Inherited
• Backwards Compatible
Interface Evolution
List.replaceAll(UnaryOperator) List.sort(Comparator)
Collection.removeIf(Predicate)
Iterable.forEach(Consumer)
API Evolution
StreamsStreams [..] are [..] concerned with declaratively describing their source and the computational operations which will be performed in aggregate on that source
myListOfStrings.stream() .filter(s -> s != null) .mapToInt(String::length) .sum();
Streams
Stream<T> IntStream LongStream DoubleStream
Streams (2)
Intermediate Operations Terminal Operations
Collection.stream() Collection.parallelStream()
API Evolution (2)
API
Syntax
VM
github.com/szarnekow/Java8TutorialExercises @ GitHub