Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
600
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
LLMOps: Eval-Centric を前提としたMLOps
asei
5
300
累計2500万着電を支える大規模 電話自動応答サービスのアーキテクチャ / Architecture of a Large-Scale Automated Phone Response Service Supporting 25 Million Cumulative Calls
ymachida
8
4k
Entra ID の基礎(Japan Microsoft 365 コミュニティ カンファレンス 2024)
murachiakira
3
1.8k
AWS re:Invent 2024 予選落ちのBedrockアプデをまとめて解説!
minorun365
PRO
2
220
LINEヤフーにおける超大規模プラットフォーム実現への挑戦と学び / Challenges and Lessons in Building an Ultra-Large-Scale Platform at LY Corporation
hhiroshell
1
820
クラウドネイティブなNewSQLで実現するミッションクリティカルなアプリケーションの運用
yuyu_hf
PRO
1
150
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
30
15k
ONNX推論クレートの比較と実装奮闘記
emergent
0
240
次のコンテナセキュリティの時代 - User Namespace With a Pod / CloudNative Days Winter 2024
pfn
PRO
4
380
2024年のAmazon Bedrockアップデート一挙おさらい 〜まだ間に合う! re:Invent直前までの重大ニュースを速習しよう〜
minorun365
PRO
3
160
Bytebaseで実現する データベース管理の効率化
shogo452
1
130
コンパウンド戦略に向けた技術選定とリアーキテクチャ
kworkdev
PRO
1
3.9k
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Designing the Hi-DPI Web
ddemaree
280
34k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
Designing for Performance
lara
604
68k
Faster Mobile Websites
deanohume
305
30k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
What's new in Ruby 2.0
geeforr
343
31k
Music & Morning Musume
bryan
46
6.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Agile that works and the tools we love
rasmusluckow
327
21k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
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