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
810
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
590
Bootstrapping Your Org's CI and CD with Gradle
johnrengelman
0
190
Introduction To Gradle
johnrengelman
2
900
Build Application Stacks with Gradle
johnrengelman
1
200
Enterprise Grails - Spring Batch
johnrengelman
1
1k
Other Decks in Technology
See All in Technology
Terraform未経験の御様に対してどの ように導⼊を進めていったか
tkikuchi
2
430
100 名超が参加した日経グループ横断の競技型 AWS 学習イベント「Nikkei Group AWS GameDay」の紹介/mediajaws202411
nikkei_engineer_recruiting
1
170
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
220
【Startup CTO of the Year 2024 / Audience Award】アセンド取締役CTO 丹羽健
niwatakeru
0
870
Platform Engineering for Software Developers and Architects
syntasso
1
510
dev 補講: プロダクトセキュリティ / Product security overview
wa6sn
1
2.3k
TypeScript、上達の瞬間
sadnessojisan
46
13k
テストコード品質を高めるためにMutation Testingライブラリ・Strykerを実戦導入してみた話
ysknsid25
7
2.6k
Shopifyアプリ開発における Shopifyの機能活用
sonatard
4
250
隣接領域をBeyondするFinatextのエンジニア組織設計 / beyond-engineering-areas
stajima
1
270
ISUCONに強くなるかもしれない日々の過ごしかた/Findy ISUCON 2024-11-14
fujiwara3
8
860
iOS/Androidで同じUI体験をネ イティブで作成する際に気をつ けたい落とし穴
fumiyasac0921
1
110
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Unsuck your backbone
ammeep
668
57k
How GitHub (no longer) Works
holman
310
140k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Building Your Own Lightsaber
phodgson
103
6.1k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
Thoughts on Productivity
jonyablonski
67
4.3k
4 Signs Your Business is Dying
shpigford
180
21k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
The Pragmatic Product Professional
lauravandoore
31
6.3k
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