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 Functional Programming for Groovy Develo...
Search
John Engelman
July 31, 2015
Technology
1
850
Java 8 Functional Programming for Groovy Developers
An introduction to the JDK8 functional model for Groovy developers.
John Engelman
July 31, 2015
Tweet
Share
More Decks by John Engelman
See All by John Engelman
Source to Deployment with Gradle & Docker
johnrengelman
1
610
Bootstrapping Your Org's CI and CD with Gradle
johnrengelman
0
190
Introduction To Gradle
johnrengelman
2
910
Build Application Stacks with Gradle
johnrengelman
1
210
Enterprise Grails - Spring Batch
johnrengelman
1
1.1k
Other Decks in Technology
See All in Technology
エンジニアが加速させるプロダクトディスカバリー 〜最速で価値ある機能を見つける方法〜 / product discovery accelerated by engineers
rince
4
530
ソフトウェアエンジニアと仕事するときに知っておいたほうが良いこと / Key points for working with software engineers
pinkumohikan
1
140
Exadata Database Service on Cloud@Customer セキュリティ、ネットワーク、および管理について
oracle4engineer
PRO
2
1.5k
php-conference-nagoya-2025
fuwasegu
0
140
Perlの生きのこり - エンジニアがこの先生きのこるためのカンファレンス2025
kfly8
1
240
あれは良かった、あれは苦労したB2B2C型SaaSの新規開発におけるCloud Spanner
hirohito1108
2
890
OSS構成管理ツールCMDBuildを使ったAWSリソース管理の自動化
satorufunai
0
390
Raycast AI APIを使ってちょっと便利な拡張機能を作ってみた / created-a-handy-extension-using-the-raycast-ai-api
kawamataryo
0
190
コンテナサプライチェーンセキュリティ
kyohmizu
1
130
クラウドサービス事業者におけるOSS
tagomoris
3
970
ホワイトボードチャレンジ 説明&実行資料
ichimichi
0
140
遷移の高速化 ヤフートップの試行錯誤
narirou
5
850
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1368
200k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
10
1.3k
Measuring & Analyzing Core Web Vitals
bluesmoon
6
250
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.3k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.3k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Java REST API Framework Comparison - PWX 2021
mraible
29
8.4k
Six Lessons from altMBA
skipperchong
27
3.6k
Testing 201, or: Great Expectations
jmmastey
42
7.2k
Unsuck your backbone
ammeep
669
57k
Transcript
JDK 8 Functional Programming …For Groovy Developers
John Engelman • @johnrengelman • github.com/johnrengelman • ratpack.io • objectpartners.com/category/blog
Caveats • Scala • Clojure • Erlang • Haskell
…Big pile of Salt
None
Questions?
public static <T> TransformablePublisher<T> stream(Publisher<T> publisher) { return Streams.transformable(subscriber ->
require().streamSubscribe((handle) -> publisher.subscribe(new Subscriber<T>() { @Override public void onSubscribe(final Subscription subscription) { handle.event(() -> subscriber.onSubscribe(subscription) ); } @Override public void onNext(final T element) { handle.event(() -> subscriber.onNext(element)); } @Override public void onComplete() { handle.complete(subscriber::onComplete); } @Override public void onError(final Throwable cause) { handle.complete(() -> subscriber.onError(cause)); } }) )); }
java.util.function https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
JDK8 Functional Interfaces Conceptually, a functional interface has exactly one
abstract method. - https://docs.oracle.com/javase/8/docs/api/java/ lang/FunctionalInterface.html
public interface Supplier<T> { T get(); }
Supplier<Foo> supply() { return (() -> { return new Foo();
}); } lambda expression parameters list body
Supplier<Foo> supply() { return (() -> new Foo()); } implicit
return where have we seen that before?
Supplier<Foo> supply() { return Foo::new } method handle
Supplier<Foo> supply() { return { new Foo() } } Supplier<Foo>
supply() { //http://mrhaki.blogspot.com/2015/03/groovy-goodness- use-constructor-as.htm Foo.metaClass.&invokeConstructor }
public interface Consumer<T> { void accept(T t); }
Consumer<Foo> consume() { return (val -> System.out.println(val)); } Consumer<Foo> consumeMethod()
{ return System.out::println; }
Consumer<Foo> consume() { return { val -> println val.name }
} Consumer<Foo> consumeMethod() { this.&println }
public interface Function<T, R> { R appy(T t); }
Function<Foo, Bar> convert() { return (foo -> { return new
Bar(foo.name); }); } Function<Foo, Bar> convertMethod() { return Bar::new; }
Function<Foo, Bar> convert() { return { foo -> new Bar(foo.name)
} } Function<Foo, Bar> convertMethod() { Bar.metaClass.&invokeConstructor }
public interface BiConsumer<T, U> { void accept(T t, U u);
} public interface BiFunction<T, U, R> { R apply(T t, U u); }
Where is TriFunction<T, U, V, R>?
• BiConsumer<T, U> • BiFunction<T, U, R> • BinaryOperator<T> •
BiPredicate<T, U> • BooleanSupplier • Consumer<T> • DoubleBinaryOperator • DoubleConsumer • DoubleFunction<R> • DoublePredicate • DoubleSupplier • DoubleToIntFunction • DoubleToLongFunction • DoubleUnaryOperator • Function<T, R> • IntBinaryOperator • IntConsumer • IntFunction<R> • IntPredicate • IntSupplier • IntToDoubleFunction • IntToLongFunction • IntUnaryOperator • LongBinaryOperator • LongConsumer • LongFunction<R> • LongPredicate • LongSupplier • LongToDoubleFunction • LongToIntFunction • LongUnaryOperator • ObjDoubleConsumer<T> • ObjIntConsumer<T> • ObjLongConsumer<T> • Predicate<T> • Supplier<T> • ToDoubleBiFunction<T, U> • ToDoubleFunction<T> • ToIntBitFunction<T, U> • ToIntFunction<T> • ToLongBiFunction<T, U> • ToLongFunction<T> • UnaryOperator<T> Java • Closure Groovy
java.util.stream https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
List<Foo> filter(List<Foo> foos) { return foos.stream() .filter(foo -> foo.name.equals(“bar") ).collect(Collectors.toList());
} List<Bar> map(List<Foo> foos) { return foos.stream() .map(Bar::new).collect(Collectors.toList()); } List<Foo> distinct(List<Foo> foos) { return foos.stream().distinct().collect(Collectors.toList()); }
List<Foo> filter(List<Foo> foos) { foos.findAll { foo -> foo.name ==
'bar' } } List<Bar> map(List<Foo> foos) { foos.collect { new Bar(foo) } } List<Foo> distinct(List<Foo> foos) { foos.unique() }
public static <T> TransformablePublisher<T> stream(Publisher<T> publisher) { return Streams.transformable(subscriber ->
require().streamSubscribe((handle) -> publisher.subscribe(new Subscriber<T>() { @Override public void onSubscribe(final Subscription subscription) { handle.event(() -> subscriber.onSubscribe(subscription) ); } @Override public void onNext(final T element) { handle.event(() -> subscriber.onNext(element)); } @Override public void onComplete() { handle.complete(subscriber::onComplete); } @Override public void onError(final Throwable cause) { handle.complete(() -> subscriber.onError(cause)); } }) )); }
public static <T> TransformablePublisher<T> stream(Publisher<T> publisher) { return Streams.transformable {
subscriber -> require().streamSubscribe { handle -> publisher.subscribe(new Subscriber<T>() { @Override public void onSubscribe(final Subscription subscription) { handle.event { subscriber.onSubscribe(subscription) } } @Override public void onNext(final T element) { handle.event { subscriber.onNext(element) } } @Override public void onComplete() { handle.complete(subscriber.&onComplete) } @Override public void onError(final Throwable cause) { handle.complete { subscriber.onError(cause) } } }) } } }
Default Interface Methods • Game changer public interface Baz {
String getName(); default Bar bar() { return new Bar(this); } } Bar interfaceMethod(Baz baz) { return baz.bar(); } Bar useInterface() { return interfaceMethod(() -> "John"); } becomes a Supplier<String> lambda implicitly coerced to interface
Default Interface Methods • Not directly* supported in Groovy *
Groovy 2.3 introduced Traits which are similar
References/More Info • https://github.com/danveloper/uberconf2014- from-groovy-to-java8/blob/master/from-groovy-to- java8.pdf • Anything w/ Venkat
• https://www.youtube.com/watch? v=Ee5t_EGjv0A