Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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...
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 
Mattias Johan 
Functional Java 
Lambdaj 
Google Guava
USE CASES 
Given a list of beers and their prices: 
• Find the cheapest beer 
• Find the most expensive beer 
• What’s the...
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 ev...
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.gith...
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 abstract...
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/view...
THANK YOU! 
HTTP://PEOPLE.CANOO.COM/SHARE 
Andres Almiray Ix-chel Ruiz 
@aalmiray @ixchelruiz
Javaone - Getting Funky with Groovy
Javaone - Getting Funky with Groovy
Javaone - Getting Funky with Groovy
Upcoming SlideShare
Loading in …5
×

Javaone - Getting Funky with Groovy

2,291 views

Published on

Published in: Software
  • Be the first to comment

Javaone - Getting Funky with Groovy

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

×