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 Wars VIII: The Function Awakens
Search
Grzegorz Piwowarek
April 11, 2016
Programming
0
680
Java Wars VIII: The Function Awakens
Grzegorz Piwowarek
April 11, 2016
Tweet
Share
More Decks by Grzegorz Piwowarek
See All by Grzegorz Piwowarek
Javaslang DevoxxBE 2016
pivovarit
1
130
Javaslang DevoxxMA
pivovarit
2
900
JavaSlang JavaDayKiev 2016
pivovarit
0
75
Javaslang - functional Java done right
pivovarit
0
210
Java 8 vs. Checked Exceptions
pivovarit
0
110
Java 8: Lambdas, Monads and Java Collections
pivovarit
1
110
Other Decks in Programming
See All in Programming
CSC509 Lecture 09
javiergs
PRO
0
140
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
290
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.5k
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
役立つログに取り組もう
irof
28
9.6k
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
900
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
230
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
290
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
410
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Rails Girls Zürich Keynote
gr2m
94
13k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Typedesign – Prime Four
hannesfritz
40
2.4k
For a Future-Friendly Web
brad_frost
175
9.4k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Producing Creativity
orderedlist
PRO
341
39k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Transcript
Grzegorz Piwowarek
[email protected]
GitHub, Twitter :: @pivovarit
GitHub, Twitter :: @pivovarit VISIONS OF TONDAL.PL
None
None
None
None
Why?
//TODO: -lambda expressions -java.util.function -monad -java.util.Optional -java.util.stream.Stream GitHub, Twitter ::
@pivovarit
lambda expressions GitHub, Twitter :: @pivovarit
lambda expressions (...) -> statement - Anonymous function GitHub, Twitter
:: @pivovarit
lambda expressions x -> x + 1 GitHub, Twitter ::
@pivovarit
lambda expressions x -> x + 1 GitHub, Twitter ::
@pivovarit () -> 42
lambda expressions x -> x + 1 GitHub, Twitter ::
@pivovarit () -> 42 () -> {return 42;}
lambda expressions x -> x + 1 GitHub, Twitter ::
@pivovarit () -> 42 (x, y) -> {} () -> {return 42;}
lambda expressions x -> x + 1 - No type
information GitHub, Twitter :: @pivovarit
method references GitHub, Twitter :: @pivovarit
java.util.function GitHub, Twitter :: @pivovarit
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> GitHub, Twitter :: @pivovarit
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> GitHub, Twitter :: @pivovarit
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> GitHub, Twitter :: @pivovarit
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> GitHub, Twitter :: @pivovarit
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> GitHub, Twitter :: @pivovarit
MONAD GitHub, Twitter :: @pivovarit
GitHub, Twitter :: @pivovarit
GOOGLE IMAGES...
GOOGLE IMAGES...
GOOGLE IMAGES...
GOOGLE IMAGES...
GOOGLE IMAGES...
GOOGLE IMAGES...
GOOGLE IMAGES...
GOOGLE IMAGES...
... GitHub, Twitter :: @pivovarit
MONAD Design pattern http://got-steam.com/ GitHub, Twitter :: @pivovarit
Why bother? : Boilerplate-- Readability++ Complexity-- GitHub, Twitter :: @pivovarit
Imperative approach GitHub, Twitter :: @pivovarit
Declarative approach GitHub, Twitter :: @pivovarit
Generic type: "unit" method: "bind" method: MONAD GitHub, Twitter ::
@pivovarit
Generic type: "unit" method: "bind" method: MONAD GitHub, Twitter ::
@pivovarit
Generic type: "unit" method: "bind" method: MONAD GitHub, Twitter ::
@pivovarit
Generic type: "unit" method: "bind" method: MONAD GitHub, Twitter ::
@pivovarit
GitHub, Twitter :: @pivovarit
GitHub, Twitter :: @pivovarit
GitHub, Twitter :: @pivovarit
http://www.ivanaborovnjak.com/project/box-in-a-box/ GitHub, Twitter :: @pivovarit
Solution GitHub, Twitter :: @pivovarit
Monads in Java 8 Optional Stream CompletableFuture GitHub, Twitter ::
@pivovarit
Monads in Java 8 Optional Stream CompletableFuture GitHub, Twitter ::
@pivovarit
Optional Encapsulation of operations on optional values GitHub, Twitter ::
@pivovarit
Optional Encapsulation of operations on optional values GitHub, Twitter ::
@pivovarit
Optional type: "unit": "bind": GitHub, Twitter :: @pivovarit
Optional type: Optional<T> "unit": "bind": GitHub, Twitter :: @pivovarit
Optional type: Optional<T> "unit": Optional.ofNullable(), Optional.of() "bind": GitHub, Twitter ::
@pivovarit
Optional type: Optional<T> "unit": Optional.ofNullable(), Optional.of() "bind": Optional.flatMap(), Optional.map() GitHub,
Twitter :: @pivovarit
Filtering an Optional .filter(Predicate<T>) GitHub, Twitter :: @pivovarit
Unwrapping an Optional .get() .orElse(T default) .orElseGet(Supplier<T>) .orElseThrow(Supplier<Ex>) .ifPresent(Consumer<T>) GitHub,
Twitter :: @pivovarit
GitHub, Twitter :: @pivovarit http://theworryfreelife.com/wp-content/uploads/2015/08/comfort-zone.jpg
Java 7 style GitHub, Twitter :: @pivovarit
Java 8 style GitHub, Twitter :: @pivovarit
Java 7,5 style ;) GitHub, Twitter :: @pivovarit
Java 7,5 style ;) GitHub, Twitter :: @pivovarit
GitHub, Twitter :: @pivovarit
GitHub, Twitter :: @pivovarit
Stream Encapsulation of operations on multiple items GitHub, Twitter ::
@pivovarit
Stream type: Stream<T> "unit": Stream.of(), Arrays.stream(), Collection.stream() "bind": Stream.flatMap() GitHub,
Twitter :: @pivovarit
Stream GitHub, Twitter :: @pivovarit
Stream GitHub, Twitter :: @pivovarit
Stream GitHub, Twitter :: @pivovarit
Imperative vs. Declarative GitHub, Twitter :: @pivovarit
Imperative vs. Declarative GitHub, Twitter :: @pivovarit
Stream && Optional GitHub, Twitter :: @pivovarit
Stream lazy-initialized nonreusable GitHub, Twitter :: @pivovarit
intermediate operations .map() .flatMap() .filter() .peek() GitHub, Twitter :: @pivovarit
intermediate operations .map() .flatMap() .filter() .peek() Stream not consumed: does
not print anything GitHub, Twitter :: @pivovarit
Consuming Stream .forEach(Consumer<T>) .collect() .reduce(BinaryOperator<T>) .allMatch(), anyMatch(), noneMatch() .findFirst(), findAny()
.count() .toArray() GitHub, Twitter :: @pivovarit
Stream.reduce() GitHub, Twitter :: @pivovarit
Collectors .toList(), toMap(), toSet(), toCollection() .minBy(), maxBy() .joining() .partitioningBy() ...and
many others GitHub, Twitter :: @pivovarit
Collectors.toList() GitHub, Twitter :: @pivovarit
Stream in APIs "any string".chars() Files.newDirectoryStream() Random.ints() ... GitHub, Twitter
:: @pivovarit
you want more? GitHub, Twitter :: @pivovarit
github.com/ TouK/ThrowingFunction http://www.javaslang.io/ GitHub, Twitter :: @pivovarit
Thank You! GitHub, Twitter :: @pivovarit
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