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
670
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
860
JavaSlang JavaDayKiev 2016
pivovarit
0
73
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
99
Other Decks in Programming
See All in Programming
オートマトン学習しろ / Do automata learning
makenowjust
3
110
Mergeable Libraryで 高速なアプリ起動を実現しよう!
giginet
PRO
1
2.1k
Rubyとクリエイティブコーディングの輪の広がり / The Growing Circle of Ruby and Creative Coding
chobishiba
1
240
Amebaチョイス立ち上げの裏側 ~依存システムとの闘い~
daichi_igarashi
0
220
From Spring Boot 2 to Spring Boot 3 with Java 22 and Jakarta EE
ivargrimstad
0
1.1k
プログラマのための音楽入門
cheebow
5
560
複雑さに立ち向かうための ソフトウェア開発入門
shiz
3
650
初めてのiOS関連GitHub ActionsをMarketplaceに公開するまでの実録
konifar
3
200
仮想ファイルシステムを導入して開発環境のストレージ課題を解消する
segadevtech
2
440
GoのIteratorに詳しくなってしまう
inatonix
1
190
Kotlin 2.0 and Beyond
antonarhipov
2
140
令和トラベルにおけるLLM活用事例:社内ツール開発から得た学びと実践
ippo012
0
120
Featured
See All Featured
Clear Off the Table
cherdarchuk
90
320k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
230
17k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
27
8.9k
Making the Leap to Tech Lead
cromwellryan
128
8.8k
GraphQLとの向き合い方2022年版
quramy
43
13k
Raft: Consensus for Rubyists
vanstee
135
6.5k
How to Ace a Technical Interview
jacobian
275
23k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
278
13k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
123
18k
A Tale of Four Properties
chriscoyier
155
22k
Thoughts on Productivity
jonyablonski
66
4.2k
Docker and Python
trallard
39
3k
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