Javaone - Getting Funky with Groovy

43
GETTING FUNKY WITH ANDRES ALMIRAY IX-CHEL RUIZ @AALMIRAY @IXCHELRUIZ

description

 

Transcript of Javaone - Getting Funky with Groovy

Page 1: Javaone - Getting Funky with Groovy

GETTING FUNKY WITH ANDRES ALMIRAY IX-CHEL RUIZ @AALMIRAY @IXCHELRUIZ

Page 2: Javaone - Getting Funky with Groovy
Page 3: Javaone - Getting Funky with Groovy
Page 4: Javaone - Getting Funky with Groovy
Page 5: Javaone - Getting Funky with Groovy

FUNCTIONAL GROOVY, ARE YOU KIDDING ME?

Page 6: Javaone - Getting Funky with Groovy

GROOVY IS NOT HASKELL RUSSEL WINDER

* have a look at https://github.com/frege/frege

Page 7: Javaone - Getting Funky with Groovy

CLOSURES == FUNCTIONS Closures are functions (i.e, blocks of code) with an environment containing a binding for all free variables of the function

Page 8: Javaone - Getting Funky with Groovy

CLOSURES == FUNCTIONS Closures are NOT side effect free by design

Page 9: Javaone - Getting Funky with Groovy

CLOSURES: PARAMETERS (1) Parameter types may be omitted if type information is not needed

Page 10: Javaone - Getting Funky with Groovy

CLOSURES: PARAMETERS (2) Parameters may have default values NOTE: Default values must be defined from right to left

Page 11: Javaone - Getting Funky with Groovy

CLOSURES: DEFAULT PARAMETER Closures may have a default parameter named it

Page 12: Javaone - Getting Funky with Groovy

CLOSURES LEAD TO … Iterators Streams Partial Evaluation Composition Memoization Tail calls

Page 13: Javaone - Getting Funky with Groovy

ITERATORS (1)

Page 14: Javaone - Getting Funky with Groovy

ITERATORS (2) p

Page 15: Javaone - Getting Funky with Groovy

ITERATORS

Page 16: Javaone - Getting Funky with Groovy

GEEKS & BEERS

Functional Java Lambdaj

Google Guava

Johan Mattias

Page 17: Javaone - Getting Funky with Groovy

USE CASES Given a list of beers and their prices:

•  Find the cheapest beer

•  Find the most expensive beer

•  What’s the name of the most expensive beer

•  Get all the beer names

•  What’s the total price

Page 18: Javaone - Getting Funky with Groovy

USE CASE 1 Find the cheapest beer

Page 19: Javaone - Getting Funky with Groovy

USE CASE 2 Find the most expensive beer

Page 20: Javaone - Getting Funky with Groovy

USE CASE 3 What’s the name of the most expensive beer

Page 21: Javaone - Getting Funky with Groovy

USE CASE 4 Get all the beer names

Page 22: Javaone - Getting Funky with Groovy

USE CASE 5 What’s the total price

Page 23: Javaone - Getting Funky with Groovy

PROS & CONS Java collections Closures! Plays nice with all other functional frameworks Metaprogramming!

Non-lazy evaluation Streams not included in core filter/map/reduce

Page 24: Javaone - Getting Funky with Groovy

S/COLLECT/MAP/ Don’t like collect? Use Groovy’s metaprogramming

Page 25: Javaone - Getting Funky with Groovy

STREAMS (1) Lazy generators. Extension module created by @tim_yates

http://timyates.github.com/groovy-stream/

Page 26: Javaone - Getting Funky with Groovy

STREAMS (2) Groovy is Java friendly. Use any Java library such as functional-java

Page 27: Javaone - Getting Funky with Groovy

FJ MODULE EXTENSION https://github.com/mperry/functionalgroovy

Update: Mark has posted some notes at

http://mperry.github.io/2013/07/28/groovy-null-handling.html

Page 28: Javaone - Getting Funky with Groovy

STREAMS (JDK8) Collection and Lambda friendly, lazy evaluation

Page 29: Javaone - Getting Funky with Groovy

PARTIAL EVALUATION (1) Currying creates a new closure with fixed parameters, left to right

Page 30: Javaone - Getting Funky with Groovy

PARTIAL EVALUATION (2) Currying may be applied right to left too, even on an arbitrary index

Page 31: Javaone - Getting Funky with Groovy

OBJECTS AS PARTIAL EVALS Any class may implement the call() method, enabling implicit evaluation

Page 32: Javaone - Getting Funky with Groovy

COMPOSITION (1) Closures may be composed (left to right) using the >> operator

Page 33: Javaone - Getting Funky with Groovy

COMPOSITION (2) Closures may be composed (right to left) using the << operator

Page 34: Javaone - Getting Funky with Groovy

MEMOIZATION Cache computed values for increased performance

Page 35: Javaone - Getting Funky with Groovy

TAIL CALLS (1) Recursive closures may use Tail Calls thanks to trampoline()

Page 36: Javaone - Getting Funky with Groovy

TAIL CALLS (2) Apply @TailRecursive on methods https://github.com/jlink/tailrec/ now included in Groovy 2.3!

Page 37: Javaone - Getting Funky with Groovy

METHODS AS CLOSURES Any method may be transformed to a Closure using the .& operator

Page 38: Javaone - Getting Funky with Groovy

IMMUTABILITY The @Immutable AST transformation makes writing immutable classes trivial

Page 39: Javaone - Getting Funky with Groovy

GPARS http://gpars.codehaus.org/

Concurrent collection processing Composable asynchronous functions

Fork/Join abstraction

Actor programming model

Dataflow concurrency constructs

CSP Agent - an thread-safe reference to mutable state

Page 40: Javaone - Getting Funky with Groovy

PARALLEL COLLECTIONS Gpars enhances JDK/GDK collections with parallel execution enabled versions

Page 41: Javaone - Getting Funky with Groovy

MONADS https://github.com/dsrkoc/monadologie

Page 42: Javaone - Getting Funky with Groovy

RESOURCES •  http://pragprog.com/magazines/2013-01/using-

memoization-in-groovy

•  http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=functional+thinking

•  https://github.com/jlink/tailrec/

•  http://timyates.github.com/groovy-stream

•  http://www.jroller.com/vaclav

•  http://gpars.codehaus.org

•  http://www.slideshare.net/arturoherrero/functional-programming-with-groovy

Page 43: Javaone - Getting Funky with Groovy

THANK YOU!

HTTP://PEOPLE.CANOO.COM/SHARE

Andres Almiray Ix-chel Ruiz

@aalmiray @ixchelruiz