Save 37% off PRO during our Black Friday Sale! »

Java 8 Functional Programming for Groovy Developers

Java 8 Functional Programming for Groovy Developers

An introduction to the JDK8 functional model for Groovy developers.

99ef9646867e17df7d846aa9c06652a5?s=128

John Engelman

July 31, 2015
Tweet

Transcript

  1. JDK 8 Functional Programming …For Groovy Developers

  2. John Engelman • @johnrengelman • github.com/johnrengelman • ratpack.io • objectpartners.com/category/blog

  3. Caveats • Scala • Clojure • Erlang • Haskell

  4. …Big pile of Salt

  5. None
  6. Questions?

  7. 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)); } }) )); }
  8. java.util.function https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

  9. JDK8 Functional Interfaces Conceptually, a functional interface has exactly one

    abstract method. - https://docs.oracle.com/javase/8/docs/api/java/ lang/FunctionalInterface.html
  10. public interface Supplier<T> { T get(); }

  11. Supplier<Foo> supply() { return (() -> { return new Foo();

    }); } lambda expression parameters list body
  12. Supplier<Foo> supply() { return (() -> new Foo()); } implicit

    return where have we seen that before?
  13. Supplier<Foo> supply() { return Foo::new } method handle

  14. Supplier<Foo> supply() { return { new Foo() } } Supplier<Foo>

    supply() { //http://mrhaki.blogspot.com/2015/03/groovy-goodness- use-constructor-as.htm Foo.metaClass.&invokeConstructor }
  15. public interface Consumer<T> { void accept(T t); }

  16. Consumer<Foo> consume() { return (val -> System.out.println(val)); } Consumer<Foo> consumeMethod()

    { return System.out::println; }
  17. Consumer<Foo> consume() { return { val -> println val.name }

    } Consumer<Foo> consumeMethod() { this.&println }
  18. public interface Function<T, R> { R appy(T t); }

  19. Function<Foo, Bar> convert() { return (foo -> { return new

    Bar(foo.name); }); } Function<Foo, Bar> convertMethod() { return Bar::new; }
  20. Function<Foo, Bar> convert() { return { foo -> new Bar(foo.name)

    } } Function<Foo, Bar> convertMethod() { Bar.metaClass.&invokeConstructor }
  21. public interface BiConsumer<T, U> { void accept(T t, U u);

    } public interface BiFunction<T, U, R> { R apply(T t, U u); }
  22. Where is TriFunction<T, U, V, R>?

  23. • 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
  24. java.util.stream https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

  25. 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()); }
  26. 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() }
  27. 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)); } }) )); }
  28. 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) } } }) } } }
  29. 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
  30. Default Interface Methods • Not directly* supported in Groovy *

    Groovy 2.3 introduced Traits which are similar
  31. 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