Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Java 8: Lambdas, Monads and Java Collections
Search
Grzegorz Piwowarek
July 06, 2015
Programming
1
100
Java 8: Lambdas, Monads and Java Collections
Presented during Confitura 2015
Grzegorz Piwowarek
July 06, 2015
Tweet
Share
More Decks by Grzegorz Piwowarek
See All by Grzegorz Piwowarek
Javaslang DevoxxBE 2016
pivovarit
1
130
Javaslang DevoxxMA
pivovarit
2
870
JavaSlang JavaDayKiev 2016
pivovarit
0
73
Javaslang - functional Java done right
pivovarit
0
210
Java Wars VIII: The Function Awakens
pivovarit
0
670
Java 8 vs. Checked Exceptions
pivovarit
0
110
Other Decks in Programming
See All in Programming
月間4.5億回再生を超える大規模サービス TVer iOSアプリのリアーキテクチャ戦略 - iOSDC2024
techtver
PRO
1
820
『ドメイン駆動設計をはじめよう』中核の業務領域
masuda220
PRO
5
970
dRuby 入門者によるあなたの身近にあるdRuby 入門
makicamel
4
350
いまから追い上げる、Jetpack Compose トレーニング
nyafunta9858
0
300
【TID2024】模擬講義:プログラマと一緒にゲームをデザインしてみよう!
akatsukigames_tech
0
610
あなたのアプリ、ログはでてますか?あるいはログをだしてますか? (Funabashi.dev用 軽量版)
uzulla
2
110
Lessons by WebAssembly app in production on CDN Edge Computing Service
tetsuharuohzeki
0
200
僕が思い描くTypeScriptの未来を勝手に先取りする
yukukotani
9
2.4k
大公開!iOS開発の悩みトップ5 〜iOSDC Japan 2024〜
ryunakayama
0
190
GoのIteratorに詳しくなってしまう
inatonix
1
200
エラーレスポンス設計から考える、0→1開発におけるGraphQLへの向き合い方
bicstone
5
1.5k
What we keep in mind when migrating from Serverless Framework to AWS CDK and AWS SAM
kasacchiful
1
140
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
123
18k
The Straight Up "How To Draw Better" Workshop
denniskardys
230
130k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
227
52k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
326
21k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
166
48k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
25
3.9k
[RailsConf 2023] Rails as a piece of cake
palkan
48
4.6k
Build The Right Thing And Hit Your Dates
maggiecrowley
30
2.3k
For a Future-Friendly Web
brad_frost
174
9.3k
How To Stay Up To Date on Web Technology
chriscoyier
786
250k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
24
600
Practical Orchestrator
shlominoach
185
10k
Transcript
JAVA 8 LAMBDAS, MONADS && JAVA COLLECTIONS Grzegorz Piwowarek
GRZEGORZ PIWOWAREK @PIVOVARIT
GRZEGORZ PIWOWAREK @PIVOVARIT visionsoftondal.com
Plan: -lambda expressions -java.util.function -monad -Optional -Stream
lambda expressions (...) -> statement - Anonymous function
lambda expressions x -> x + 1
lambda expressions x -> x + 1 - No type
information
lambda expressions x -> x + 1 () -> 42
() -> {return 42;} (x, y) -> {} () -> {}
method references
java.util.function
java.util.function
Function<T, R>, BiFunction<T,U,R> Consumer<T> extends Function<T,Void> Supplier<T> extends Function<Void, T>
Predicate<T> extends Function<T, Boolean> UnaryOperator<T> extends Function<T, T> BinaryOperator<T> extends BiFunction<T,T,T>
Function<T, R>, BiFunction<T,U,R> Consumer<T> extends Function<T,Void> Supplier<T> extends Function<Void, T>
Predicate<T> extends Function<T, Boolean> UnaryOperator<T> extends Function<T, T> BinaryOperator<T> extends BiFunction<T,T,T>
Function<T, R>, BiFunction<T,U,R> Consumer<T> extends Function<T,Void> Supplier<T> extends Function<Void, T>
Predicate<T> extends Function<T, Boolean> UnaryOperator<T> extends Function<T, T> BinaryOperator<T> extends BiFunction<T,T,T>
Function<T, R>, BiFunction<T,U,R> Consumer<T> extends Function<T,Void> Supplier<T> extends Function<Void, T>
Predicate<T> extends Function<T, Boolean> UnaryOperator<T> extends Function<T, T> BinaryOperator<T> extends BiFunction<T,T,T>
Function<T, R>, BiFunction<T,U,R> Consumer<T> extends Function<T,Void> Supplier<T> extends Function<Void, T>
Predicate<T> extends Function<T, Boolean> UnaryOperator<T> extends Function<T, T> BinaryOperator<T> extends BiFunction<T,T,T>
MONAD
None
GOOGLE IMAGES...
...
MONAD Design pattern http://got-steam.com/
Why bother? : Boilerplate -1 Readability +1 Complexity -1 Responsibility
-1
type: M<T> "unit": T -> M<T> "bind": M<T>.bind(T -> M<U>)
= M<U> MONAD "bind": M<T>.bind(T -> U)= M<U>
type: M<T> "unit": T -> M<T> "bind": M<T> bind(T ->
M<U>) = M<U> MONAD "bind": M<T>.bind(T -> U)= M<U>
"bind": M<T> bind(T -> U)= M<U> What if U: M<?>
?
"bind": M<T> bind(T -> U)= M<U> What if U: M<?>
?
"bind": M<T> bind(T -> U)= M<U> What if U: M<?>
? http://www.ivanaborovnjak.com/project/box-in-a-box/
Monads in Java 8 Optional Stream CompletableFuture
Monads in Java 8 Optional Stream CompletableFuture
Optional Encapsulation of operations on optional values
Optional type: M<T> "unit": T -> M<T> "bind": M<T> bind(T
-> M<U>) = M<U>
Optional type: Optional<T> "unit": T -> M<T> "bind": M<T> bind(T
-> M<U>) = M<U>
Optional type: Optional<T> "unit": Optional.ofNullable(), Optional.of() "bind": M<T> bind(T ->
M<U>) = M<U>
Optional type: Optional<T> "unit": Optional.ofNullable(), Optional.of() "bind": Optional.flatMap()
Filtering an Optional .filter(Predicate<T>)
Unwrapping an Optional .get() .orElse(T default) .orElseGet(Supplier<T>) .orElseThrow(Supplier<Ex>) .ifPresent(Consumer<T>)
Java 7 style
Java 8 style
Java 8 style - flatMap
Java 7,5 style ;)
Java 7,5 style ;)
Stream Encapsulation of operations on multiple items
Stream type: Stream<T> "unit": Stream.of(), Arrays.stream(), Collection.stream() "bind": Stream.flatMap()
Stream
Stream
Stream
Stream && Optional
Stream lazy-initialized nonreusable
intermediate operations .map() .flatMap() .filter() .peek()
intermediate operations .map() .flatMap() .filter() .peek() Stream not consumed: does
not print anything
Java7
Java8
Consuming Stream .forEach(Consumer<T>) .collect() .reduce(BinaryOperator<T>) .allMatch(), anyMatch(), noneMatch() .findFirst(), findAny()
.count() .toArray()
Stream.reduce()
Collectors .toList(), toMap(), toSet(), toCollection() .minBy(), maxBy() .joining() .partitioningBy() ...and
many others
Collectors.toList()
Collectors.toMap()
Collectors.joining()
Debuggability? IntelliJ IDEA: - v14.0 - partial support - v15.0
- full support
Stream in APIs BufferedReader.lines() Files.newDirectoryStream() Random.ints() ...
you want more?
https://github.com/ jasongoodwin/better-java-monads
Thank You!
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