functional groovy - confess

44
FUNCTIONAL GROOVY ANDRES ALMIRAY [email protected] @AALMIRAY

Upload: andres-almiray

Post on 16-Apr-2017

2.552 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Functional Groovy - Confess

FUNCTIONAL GROOVY

ANDRES ALMIRAY [email protected] @AALMIRAY

Page 2: Functional Groovy - Confess

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 Groovy - Confess
Page 4: Functional Groovy - Confess
Page 5: Functional Groovy - Confess

FUNCTIONAL GROOVY, ARE YOU KIDDING ME?

Page 6: Functional Groovy - Confess

GROOVY IS NOT HASKELL RUSSEL WINDER

Page 7: Functional Groovy - Confess

CLOSURES == FUNCTIONS Closures are functions (i.e, blocks of code) with an environment containing a binding for all free variables of the function

Page 8: Functional Groovy - Confess

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

Page 9: Functional Groovy - Confess

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

Page 10: Functional Groovy - Confess

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

Page 11: Functional Groovy - Confess

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

Page 12: Functional Groovy - Confess

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

Page 13: Functional Groovy - Confess

ITERATORS (1)

Page 14: Functional Groovy - Confess

ITERATORS (2) p

Page 15: Functional Groovy - Confess

ITERATORS

Page 16: Functional Groovy - Confess

GEEKS & BEERS

Functional Java Lambdaj

Google Guava

Page 17: Functional Groovy - Confess

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

Page 18: Functional Groovy - Confess

USE CASE 1 Find the cheapest beer

Page 19: Functional Groovy - Confess

USE CASE 2 Find the most expensive beer

Page 20: Functional Groovy - Confess

USE CASE 3 What’s the name of the most expensive beer

Page 21: Functional Groovy - Confess

USE CASE 4 Get all the beer names

Page 22: Functional Groovy - Confess

USE CASE 5 What’s the total price

Page 23: Functional Groovy - Confess

PROS & CONS Java collections Closures! Plays nice with all other functional frameworks Metaprogramming!

Non-lazy evaluation Streams not included in core filter/map/reduce

Page 24: Functional Groovy - Confess

S/COLLECT/MAP/ Don’t like collect? Use Groovy’s metaprogramming

Page 25: Functional Groovy - Confess

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

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

Page 26: Functional Groovy - Confess

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

Page 27: Functional Groovy - Confess

FJ MODULE EXTENSION https://github.com/mperry/functionalgroovy

Page 28: Functional Groovy - Confess

STREAMS (JDK8) Collection and Lambda friendly, lazy evaluation

Page 29: Functional Groovy - Confess

STREAMS (JDK8)

https://twitter.com/jodastephen/status/316991150672183297

Page 30: Functional Groovy - Confess

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

Page 31: Functional Groovy - Confess

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

Page 32: Functional Groovy - Confess

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

Page 33: Functional Groovy - Confess

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

Page 34: Functional Groovy - Confess

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

Page 35: Functional Groovy - Confess

MEMOIZATION Cache computed values for increased performance

Page 36: Functional Groovy - Confess

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

Page 37: Functional Groovy - Confess

TAIL CALLS (2) Apply @TailRecursive on methods

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

Page 38: Functional Groovy - Confess

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

Page 39: Functional Groovy - Confess

IMMUTABILITY The @Immutable AST transformation makes writing immutable classes trivial

Page 40: Functional Groovy - Confess

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 41: Functional Groovy - Confess

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

Page 42: Functional Groovy - Confess

MONADS https://github.com/dsrkoc/monadologie

Page 43: Functional Groovy - Confess

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 44: Functional Groovy - Confess

HTTP://PEOPLE.CANOO.COM/SHARE

ANDRES ALMIRAY [email protected] @AALMIRAY