jfokus functional groovy

31
FUNCTIONAL GROOVY ANDRES ALMIRAY CANOO ENGINEERING A.G. @AALMIRAY

Upload: andres-almiray

Post on 05-Dec-2014

2.021 views

Category:

Documents


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Jfokus functional groovy

FUNCTIONAL GROOVY ANDRES ALMIRAY CANOO ENGINEERING A.G. @AALMIRAY

Page 2: Jfokus functional groovy

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

Page 3: Jfokus functional groovy
Page 4: Jfokus functional groovy

FUNCTIONAL GROOVY, ARE YOU KIDDING ME?

Page 5: Jfokus functional groovy

GROOVY IS NOT HASKELL RUSSEL WINDER

Page 6: Jfokus functional 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 7: Jfokus functional groovy

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

Page 8: Jfokus functional groovy

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

Page 9: Jfokus functional groovy

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

Page 10: Jfokus functional groovy

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

Page 11: Jfokus functional groovy

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

Page 12: Jfokus functional groovy

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

Page 13: Jfokus functional groovy

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

Page 14: Jfokus functional groovy

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

Page 15: Jfokus functional groovy

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

Page 16: Jfokus functional groovy

MEMOIZATION Cache computed values for increased performance

Page 17: Jfokus functional groovy

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

Page 18: Jfokus functional groovy

TAIL CALLS (2) Apply @TailRecursive on methods

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

Page 19: Jfokus functional groovy

ITERATORS (1)

Page 20: Jfokus functional groovy

ITERATORS (2) p

Page 21: Jfokus functional groovy

ITERATORS

Page 22: Jfokus functional groovy

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

Page 23: Jfokus functional groovy

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

Page 24: Jfokus functional groovy

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

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

Page 25: Jfokus functional groovy

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

Page 26: Jfokus functional groovy

IMMUTABILITY The @Immutable AST transformation makes writing immutable classes trivial

Page 27: Jfokus functional 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 28: Jfokus functional groovy

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

Page 29: Jfokus functional 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 30: Jfokus functional groovy

Q & A

Page 31: Jfokus functional groovy

THANK YOU!