Functional Groovy - Confess
-
Upload
andres-almiray -
Category
Technology
-
view
2.546 -
download
0
Transcript of Functional Groovy - Confess
FUNCTIONAL GROOVY
ANDRES ALMIRAY [email protected] @AALMIRAY
ABOUT THE SPEAKER Java developer since the beginning True believer in open source Groovy committer since 2007
Project lead of the Griffon framework
Currently working for
FUNCTIONAL GROOVY, ARE YOU KIDDING ME?
GROOVY IS NOT HASKELL RUSSEL WINDER
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
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
STREAMS (JDK8) Collection and Lambda friendly, lazy evaluation
STREAMS (JDK8)
https://twitter.com/jodastephen/status/316991150672183297
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/
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
HTTP://PEOPLE.CANOO.COM/SHARE
ANDRES ALMIRAY [email protected] @AALMIRAY