Javaone - Getting Funky with Groovy
-
Upload
andres-almiray -
Category
Software
-
view
1.000 -
download
4
description
Transcript of Javaone - Getting Funky with Groovy
GETTING FUNKY WITH ANDRES ALMIRAY IX-CHEL RUIZ @AALMIRAY @IXCHELRUIZ
FUNCTIONAL GROOVY, ARE YOU KIDDING ME?
GROOVY IS NOT HASKELL RUSSEL WINDER
* have a look at https://github.com/frege/frege
CLOSURES == FUNCTIONS Closures are functions (i.e, blocks of code) with an environment containing a binding for all free variables of the function
CLOSURES == FUNCTIONS Closures are NOT side effect free by design
CLOSURES: PARAMETERS (1) Parameter types may be omitted if type information is not needed
CLOSURES: PARAMETERS (2) Parameters may have default values NOTE: Default values must be defined from right to left
CLOSURES: DEFAULT PARAMETER Closures may have a default parameter named it
CLOSURES LEAD TO … Iterators Streams Partial Evaluation Composition Memoization Tail calls
ITERATORS (1)
ITERATORS (2) p
ITERATORS
GEEKS & BEERS
Functional Java Lambdaj
Google Guava
Johan Mattias
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
USE CASE 1 Find the cheapest beer
USE CASE 2 Find the most expensive beer
USE CASE 3 What’s the name of the most expensive beer
USE CASE 4 Get all the beer names
USE CASE 5 What’s the total price
PROS & CONS Java collections Closures! Plays nice with all other functional frameworks Metaprogramming!
Non-lazy evaluation Streams not included in core filter/map/reduce
S/COLLECT/MAP/ Don’t like collect? Use Groovy’s metaprogramming
STREAMS (1) Lazy generators. Extension module created by @tim_yates
http://timyates.github.com/groovy-stream/
STREAMS (2) Groovy is Java friendly. Use any Java library such as functional-java
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
STREAMS (JDK8) Collection and Lambda friendly, lazy evaluation
PARTIAL EVALUATION (1) Currying creates a new closure with fixed parameters, left to right
PARTIAL EVALUATION (2) Currying may be applied right to left too, even on an arbitrary index
OBJECTS AS PARTIAL EVALS Any class may implement the call() method, enabling implicit evaluation
COMPOSITION (1) Closures may be composed (left to right) using the >> operator
COMPOSITION (2) Closures may be composed (right to left) using the << operator
MEMOIZATION Cache computed values for increased performance
TAIL CALLS (1) Recursive closures may use Tail Calls thanks to trampoline()
TAIL CALLS (2) Apply @TailRecursive on methods https://github.com/jlink/tailrec/ now included in Groovy 2.3!
METHODS AS CLOSURES Any method may be transformed to a Closure using the .& operator
IMMUTABILITY The @Immutable AST transformation makes writing immutable classes trivial
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
PARALLEL COLLECTIONS Gpars enhances JDK/GDK collections with parallel execution enabled versions
MONADS https://github.com/dsrkoc/monadologie
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
THANK YOU!
HTTP://PEOPLE.CANOO.COM/SHARE
Andres Almiray Ix-chel Ruiz
@aalmiray @ixchelruiz