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
830
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
なぜCodeceptJSを選んだか
goataka
0
180
JVM(JavaVM)の性能分析者観点で探るInstanaの可能性
instanautsjp
0
120
Server-Side Engineer of LINE Sukimani
lycorp_recruit_jp
0
360
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
290
Oracle Cloudの生成AIサービスって実際どこまで使えるの? エンジニア目線で試してみた
minorun365
PRO
4
300
UI State設計とテスト方針
rmakiyama
3
800
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
160
ゼロから創る横断SREチーム 挑戦と進化の軌跡
rvirus0817
3
280
マイクロサービスにおける容易なトランザクション管理に向けて
scalar
0
190
サービスでLLMを採用したばっかりに振り回され続けたこの一年のあれやこれや
segavvy
2
550
Wantedly での Datadog 活用事例
bgpat
2
690
pg_bigmをRustで実装する(第50回PostgreSQLアンカンファレンス@オンライン 発表資料)
shinyakato_
0
110
Featured
See All Featured
Speed Design
sergeychernyshev
25
680
The Pragmatic Product Professional
lauravandoore
32
6.3k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.1k
Docker and Python
trallard
42
3.1k
Fireside Chat
paigeccino
34
3.1k
Designing Experiences People Love
moore
138
23k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
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