Slide 1

Slide 1 text

JAVA 8 LAMBDAS, MONADS && JAVA COLLECTIONS Grzegorz Piwowarek

Slide 2

Slide 2 text

GRZEGORZ PIWOWAREK @PIVOVARIT

Slide 3

Slide 3 text

GRZEGORZ PIWOWAREK @PIVOVARIT visionsoftondal.com

Slide 4

Slide 4 text

Plan: -lambda expressions -java.util.function -monad -Optional -Stream

Slide 5

Slide 5 text

lambda expressions (...) -> statement - Anonymous function

Slide 6

Slide 6 text

lambda expressions x -> x + 1

Slide 7

Slide 7 text

lambda expressions x -> x + 1 - No type information

Slide 8

Slide 8 text

lambda expressions x -> x + 1 () -> 42 () -> {return 42;} (x, y) -> {} () -> {}

Slide 9

Slide 9 text

method references

Slide 10

Slide 10 text

java.util.function

Slide 11

Slide 11 text

java.util.function

Slide 12

Slide 12 text

Function, BiFunction Consumer extends Function Supplier extends Function Predicate extends Function UnaryOperator extends Function BinaryOperator extends BiFunction

Slide 13

Slide 13 text

Function, BiFunction Consumer extends Function Supplier extends Function Predicate extends Function UnaryOperator extends Function BinaryOperator extends BiFunction

Slide 14

Slide 14 text

Function, BiFunction Consumer extends Function Supplier extends Function Predicate extends Function UnaryOperator extends Function BinaryOperator extends BiFunction

Slide 15

Slide 15 text

Function, BiFunction Consumer extends Function Supplier extends Function Predicate extends Function UnaryOperator extends Function BinaryOperator extends BiFunction

Slide 16

Slide 16 text

Function, BiFunction Consumer extends Function Supplier extends Function Predicate extends Function UnaryOperator extends Function BinaryOperator extends BiFunction

Slide 17

Slide 17 text

MONAD

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

GOOGLE IMAGES...

Slide 20

Slide 20 text

...

Slide 21

Slide 21 text

MONAD Design pattern http://got-steam.com/

Slide 22

Slide 22 text

Why bother? : Boilerplate -1 Readability +1 Complexity -1 Responsibility -1

Slide 23

Slide 23 text

type: M "unit": T -> M "bind": M.bind(T -> M) = M MONAD "bind": M.bind(T -> U)= M

Slide 24

Slide 24 text

type: M "unit": T -> M "bind": M bind(T -> M) = M MONAD "bind": M.bind(T -> U)= M

Slide 25

Slide 25 text

"bind": M bind(T -> U)= M What if U: M> ?

Slide 26

Slide 26 text

"bind": M bind(T -> U)= M What if U: M> ?

Slide 27

Slide 27 text

"bind": M bind(T -> U)= M What if U: M> ? http://www.ivanaborovnjak.com/project/box-in-a-box/

Slide 28

Slide 28 text

Monads in Java 8 Optional Stream CompletableFuture

Slide 29

Slide 29 text

Monads in Java 8 Optional Stream CompletableFuture

Slide 30

Slide 30 text

Optional Encapsulation of operations on optional values

Slide 31

Slide 31 text

Optional type: M "unit": T -> M "bind": M bind(T -> M) = M

Slide 32

Slide 32 text

Optional type: Optional "unit": T -> M "bind": M bind(T -> M) = M

Slide 33

Slide 33 text

Optional type: Optional "unit": Optional.ofNullable(), Optional.of() "bind": M bind(T -> M) = M

Slide 34

Slide 34 text

Optional type: Optional "unit": Optional.ofNullable(), Optional.of() "bind": Optional.flatMap()

Slide 35

Slide 35 text

Filtering an Optional .filter(Predicate)

Slide 36

Slide 36 text

Unwrapping an Optional .get() .orElse(T default) .orElseGet(Supplier) .orElseThrow(Supplier) .ifPresent(Consumer)

Slide 37

Slide 37 text

Java 7 style

Slide 38

Slide 38 text

Java 8 style

Slide 39

Slide 39 text

Java 8 style - flatMap

Slide 40

Slide 40 text

Java 7,5 style ;)

Slide 41

Slide 41 text

Java 7,5 style ;)

Slide 42

Slide 42 text

Stream Encapsulation of operations on multiple items

Slide 43

Slide 43 text

Stream type: Stream "unit": Stream.of(), Arrays.stream(), Collection.stream() "bind": Stream.flatMap()

Slide 44

Slide 44 text

Stream

Slide 45

Slide 45 text

Stream

Slide 46

Slide 46 text

Stream

Slide 47

Slide 47 text

Stream && Optional

Slide 48

Slide 48 text

Stream lazy-initialized nonreusable

Slide 49

Slide 49 text

intermediate operations .map() .flatMap() .filter() .peek()

Slide 50

Slide 50 text

intermediate operations .map() .flatMap() .filter() .peek() Stream not consumed: does not print anything

Slide 51

Slide 51 text

Java7

Slide 52

Slide 52 text

Java8

Slide 53

Slide 53 text

Consuming Stream .forEach(Consumer) .collect() .reduce(BinaryOperator) .allMatch(), anyMatch(), noneMatch() .findFirst(), findAny() .count() .toArray()

Slide 54

Slide 54 text

Stream.reduce()

Slide 55

Slide 55 text

Collectors .toList(), toMap(), toSet(), toCollection() .minBy(), maxBy() .joining() .partitioningBy() ...and many others

Slide 56

Slide 56 text

Collectors.toList()

Slide 57

Slide 57 text

Collectors.toMap()

Slide 58

Slide 58 text

Collectors.joining()

Slide 59

Slide 59 text

Debuggability? IntelliJ IDEA: - v14.0 - partial support - v15.0 - full support

Slide 60

Slide 60 text

Stream in APIs BufferedReader.lines() Files.newDirectoryStream() Random.ints() ...

Slide 61

Slide 61 text

you want more?

Slide 62

Slide 62 text

https://github.com/ jasongoodwin/better-java-monads

Slide 63

Slide 63 text

Thank You!

Slide 64

Slide 64 text

REFERENCES: -"MONADIC JAVA" BY MARIO FUSCO -"WHAT'S WRONG WITH JAVA 8" BY PIERRE-YVES SAUMONT -WWW.ORACLE.COM -"A FISTFUL OF MONADS" - LEARN YOU A HASKELL FOR GREAT GOOD