Functional Groovy - Confess

2,009
-1

Published on

Functional Groovy - Confess

Published in: Technologies

Functional Groovy - Confess

  1. 1. FUNCTIONAL GROOVY ANDRES ALMIRAY ANDRES.ALMIRAY@CANOO.COM @AALMIRAY
  2. 2. ABOUT THE SPEAKERJava developer since the beginningTrue believer in open sourceGroovy committer since 2007Project lead of the Griffon frameworkCurrently working for
  3. 3. FUNCTIONALGROOVY,ARE YOUKIDDING ME?
  4. 4. GROOVY IS NOTHASKELL RUSSEL WINDER
  5. 5. CLOSURES == FUNCTIONSClosures are functions (i.e, blocks of code) with anenvironment containing a binding for all free variables of thefunction
  6. 6. CLOSURES == FUNCTIONSClosures are NOT side effect free by design
  7. 7. CLOSURES: PARAMETERS (1)Parameter types may be omitted if type information is notneeded
  8. 8. CLOSURES: PARAMETERS (2)Parameters may have default valuesNOTE: Default values must be defined from right to left
  9. 9. CLOSURES: DEFAULTPARAMETERClosures may have a default parameter named it
  10. 10. CLOSURES LEAD TO …IteratorsStreamsPartial EvaluationCompositionMemoizationTail calls
  11. 11. ITERATORS (1)
  12. 12. ITERATORS (2)p
  13. 13. ITERATORS
  14. 14. GEEKS & BEERS Functional Java Lambdaj Google Guava
  15. 15. USE CASESGiven 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
  16. 16. USE CASE 1Find the cheapest beer
  17. 17. USE CASE 2Find the most expensive beer
  18. 18. USE CASE 3What’s the name of the most expensive beer
  19. 19. USE CASE 4Get all the beer names
  20. 20. USE CASE 5What’s the total price
  21. 21. PROS & CONSJava collections Non-lazy evaluationClosures! Streams notPlays nice with all included in coreother functional filter/map/reduceframeworksMetaprogramming!
  22. 22. S/COLLECT/MAP/Don’t like collect? Use Groovy’s metaprogramming
  23. 23. STREAMS (1)Lazy generators. Extension module created by @tim_yateshttp://timyates.github.com/groovy-stream/
  24. 24. STREAMS (2)Groovy is Java friendly. Use any Java library such asfunctional-java
  25. 25. FJ MODULE EXTENSIONhttps://github.com/mperry/functionalgroovy
  26. 26. STREAMS (JDK8)Collection and Lambda friendly, lazy evaluation
  27. 27. STREAMS (JDK8)https://twitter.com/jodastephen/status/316991150672183297
  28. 28. PARTIAL EVALUATION (1)Currying creates a new closure with fixed parameters, left toright
  29. 29. PARTIAL EVALUATION (2)Currying may be applied right to left too, even on an arbitraryindex
  30. 30. OBJECTS AS PARTIAL EVALSAny class may implement the call() method, enabling implicitevaluation
  31. 31. COMPOSITION (1)Closures may be composed (left to right) using the >>operator
  32. 32. COMPOSITION (2)Closures may be composed (right to left) using the <<operator
  33. 33. MEMOIZATIONCache computed values for increased performance
  34. 34. TAIL CALLS (1)Recursive closures may use Tail Calls thanks to trampoline()
  35. 35. TAIL CALLS (2)Apply @TailRecursive on methodshttps://github.com/jlink/tailrec/
  36. 36. METHODS AS CLOSURESAny method may be transformed to a Closure using the .&operator
  37. 37. IMMUTABILITYThe @Immutable AST transformation makes writingimmutable classes trivial
  38. 38. GPARShttp://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
  39. 39. PARALLEL COLLECTIONSGpars enhances JDK/GDK collections with parallel executionenabled versions
  40. 40. MONADShttps://github.com/dsrkoc/monadologie
  41. 41. 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
  42. 42. HTTP://PEOPLE.CANOO.COM/SHARE ANDRES ALMIRAY ANDRES.ALMIRAY@CANOO.COM @AALMIRAY

×