Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Functional Programming in Groovy

2,079 views

Published on

A description of Functional Programming basics and how they are applied in Groovy.

Published in: Technology

Functional Programming in Groovy

  1. 1. Functional Programming 101 in Groovy Evgeny Goldin DevCon Tel-Aviv, February 14, 2013
  2. 2. Evgeny GoldinJava developer since 2000FP enthusiast: Scheme, Perl, Groovy, ScalaGroovyMag, Gr8ConfSCM at Trademobgithub.com/evgeny-goldin/@evgeny_goldin
  3. 3. Are you FP-er?
  4. 4. http://thecoolnessfactor.com/2013/01/winds-change/changes-ahead-exit-sign/
  5. 5. In this session Why FP? FP building blocks FP in Groovy FP candidates
  6. 6. In this sessionMindset switch
  7. 7. Why FP?
  8. 8. Because shit happens
  9. 9. .. or do we make it happen?
  10. 10. Average code is complex Side-effects go wild Logical steps are interleaved Changes are error-pronehttp://www.guardian.co.uk/sustainable-business/research-confirms-health-impacts-tire
  11. 11. Average state is fragile and shared Exposed for updates in any wayCan easily mutate under your feet Changes are error-prone http://www.bbcgoodfood.com/content/knowhow/glossary/egg/
  12. 12. Parallelism is hard “The Multicore Revolution” Threads + shared mutable state :( Changes are error-pronehttp://www.russel.org.uk/Presentations/euroPython2010_theMulticoreRevolution.pdf http://jalopnik.com/5965581/this-is-how-not-to-parallel-park-on-a-hill
  13. 13. Changes are error-prone
  14. 14. http://lenta.ru/sitemap.xmlhttps://bitbucket.org/evgenyg/devcon-2013/src/master/sitemap-imperative.groovyhttps://bitbucket.org/evgenyg/devcon-2013/src/master/sitemap-functional.groovy
  15. 15. ModularityBreak the flow into distinct steps Do one thing and do it well
  16. 16. FlexibilityShuffle your steps
  17. 17. TestabilityEnsure correctness of your steps
  18. 18. ParallelismScale your steps!
  19. 19. FP building blocks(only a subset this time)
  20. 20. Recursion List = head() + tail() State is hidden, built up on stack Recursion eliminationhttps://bitbucket.org/evgenyg/devcon-2013/src/master/recursion.groovy
  21. 21. Immutable StateMoving parts are minimized No defensive copying Caching, Parallelism
  22. 22. Immutable State New state = f ( old state )Changes are isolated, constructor only g ( i + 1 ), g ( list + ‘element’ )
  23. 23. Immutable State Persistent data structures Updated in placeYields a new immutable state
  24. 24. Githttp://git-scm.com/book/en/Git-Branching-What-a-Branch-Is
  25. 25. First-class functions Anonymous functions Higher-order functionsCommand/Template/Visitor => “Free Willy”
  26. 26. No side-effects sin, cos, max, min, sum, sortOpens doors for optimizations Caching, Parallelism
  27. 27. Mindset switch Imperative => Functional Variables => Values Classes => FunctionsExplicit instructions => Declarative definitions How (Steps) => What (Result)
  28. 28. Mindset switchPointers, memory allocations, GC - check State management Iterations Filtering, conversion, folding Parallelism
  29. 29. FP in Groovy
  30. 30. Gradual conversionhttp://www.buysgmath.com/p/200/eph-gradually-difficulty-mathematics-1/
  31. 31. final j = 5@Immutable
  32. 32. Anonymous Functions a.k.a. Closures
  33. 33. Filter - findAll() Map - collect() Fold - inject() https://bitbucket.org/evgenyg/devcon-2013/src/master/functions.groovyhttp://www.barlifeuk.com/index.php/2012/08/enter-the-janneau-armagnac-three- musketeers-inspired-competition/
  34. 34. find() any() every() each()eachWithIndex() takeWhile() dropWhile()
  35. 35. Collection Range Map Array String ...Object.iterator()
  36. 36. Categories, Meta, Extensionspublic  static  <T>  Collection<T>  findAll(Collection<T>  self,                                                                                  Closure  filter)  public  static  <T>  List<T>  collect(Collection<?>  self,                                                                    Closure<T>  transform)public  static  T  inject(Collection<T>  self,                                                Closure<V>  closure)public  static  <T>  T              head(List<T>  self)public  static  <T>  List<T>  tail(List<T>  self)... org.codehaus.groovy.runtime.DefaultGroovyMethods
  37. 37. Memoization a.k.a. Cachinghttps://bitbucket.org/evgenyg/devcon-2013/src/master/memoization.groovy
  38. 38. TrampolineRecursion => Loop (no stack overflow)https://bitbucket.org/evgenyg/devcon-2013/src/master/trampoline.groovy
  39. 39. GPars! Data Parallelismhttps://bitbucket.org/evgenyg/devcon-2013/src/master/gpars.groovy
  40. 40. eachParallel()collectParallel()findAllParallel()everyParallel() foldParallel() sumParallel() gmemoize()
  41. 41. FP Candidates
  42. 42. Loops => any / everyhttps://bitbucket.org/evgenyg/devcon-2013/src/master/loops.groovy http://en.hdyo.org/tee/questions
  43. 43. Split, filter, map, fold https://bitbucket.org/evgenyg/devcon-2013/src/master/map-reduce.groovyhttps://bitbucket.org/evgenyg/devcon-2013/src/master/check-versions.groovy http://www.cityofwaterfalls.ca/hermitage_cascade.html
  44. 44. Open your eyes
  45. 45. pinboard.in/u:evgenyg/t:fp/ Arturo Herrero - Functional Programming with Groovy Neal Ford - Functional Thinking Martin Odersky - FP Principles in Scala Erik Meijer - FP FundamentalsMIT - Structure and Interpretation of Computer Programs University of Washington - Programming Languages Stanford - Programming Paradigms
  46. 46. Thank you! @evgeny_goldinevgenyg@gmail.com

×