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
840
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
1k
Other Decks in Technology
See All in Technology
2025-01-24-SRETT11-OpenTofuについてそろそろ調べてみるか
masasuzu
0
130
FinJAWS_reinvent2024_recap_database
asahihidehiko
2
310
攻撃者の視点で社内リソースはどう見えるのかを ASMで実現する
hikaruegashira
3
1.7k
アクセシブルなマークアップの上に成り立つユーザーファーストなドロップダウンメニューの実装 / 20250127_cloudsign_User1st_FE
bengo4com
1
1.1k
サーバレスの未来〜The Key to Simplifying Everything〜
kawaji_scratch
2
330
メンバーがオーナーシップを発揮しやすいチームづくり
ham0215
2
340
ブロックチェーンR&D企業における SREの実態 / SRE Kaigi 2025
datachain
0
3.1k
panicを深ぼってみる
kworkdev
PRO
1
110
Autify Company Deck
autifyhq
2
41k
第27回クラウド女子会 ~re:Invent 振り返りLT会~ 私の周辺で反響のあった re:Invent 2024 アップデートつれづれ/reinvent-2024-update-reverberated-around-me
emiki
1
560
インフラコストとセキュリティ課題解決のためのリアーキテクチャリング / srekaigi2025
hgsgtk
3
3.5k
企業テックブログにおける執筆ネタの考え方・見つけ方・広げ方 / How to Think of, Find, and Expand Writing Topics for Corporate Tech Blogs
honyanya
0
680
Featured
See All Featured
Side Projects
sachag
452
42k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
260
Why You Should Never Use an ORM
jnunemaker
PRO
55
9.2k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.2k
A Tale of Four Properties
chriscoyier
157
23k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
BBQ
matthewcrist
85
9.4k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
39
1.9k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Embracing the Ebb and Flow
colly
84
4.5k
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