functional groovygroovy committer since 2007 project lead of the griffon framework currently working...

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

Upload: others

Post on 04-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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

Page 2: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER
Page 4: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

FUNCTIONAL GROOVY, ARE YOU KIDDING ME?

Page 5: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

GROOVY IS NOT HASKELL RUSSEL WINDER

Page 6: FUNCTIONAL GROOVYGroovy 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

Page 7: FUNCTIONAL GROOVYGroovy 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 NOT side effect free by design

Page 8: FUNCTIONAL GROOVYGroovy 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: PARAMETERS (1) Parameter types may be omitted if type information is not needed

Page 9: FUNCTIONAL GROOVYGroovy 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: PARAMETERS (2) Parameters may have default values NOTE: Default values must be defined from right to left

Page 10: FUNCTIONAL GROOVYGroovy 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: DEFAULT PARAMETER Closures may have a default parameter named it

Page 11: FUNCTIONAL GROOVYGroovy 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 LEAD TO … Partial Evaluation Composition Memoization Tail calls Iterators Streams

Page 12: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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

Page 13: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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

Page 14: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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

Page 15: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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

Page 16: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

MEMOIZATION Cache computed values for increased performance

Page 17: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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

Page 18: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

TAIL CALLS (2) Apply @TailRecursive on methods

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

Page 19: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

ITERATORS (1)

Page 20: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

ITERATORS (2) p

Page 21: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

ITERATORS

Page 22: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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

Page 23: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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

Page 24: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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

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

Page 25: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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

Page 26: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

IMMUTABILITY The @Immutable AST transformation makes writing immutable classes trivial

Page 27: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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

Page 29: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

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: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

Q & A

Page 31: FUNCTIONAL GROOVYGroovy committer since 2007 Project lead of the Griffon framework Currently working for FUNCTIONAL GROOVY, ARE YOU KIDDING ME? GROOVY IS NOT HASKELL RUSSEL WINDER

THANK YOU!