functional programming in groovy
DESCRIPTION
A description of Functional Programming basics and how they are applied in Groovy.TRANSCRIPT
Functional Programming 101 in Groovy
Evgeny GoldinDevCon Tel-Aviv, February 14, 2013
Evgeny Goldin
Java developer since 2000
FP enthusiast: Scheme, Perl, Groovy, Scala
GroovyMag, Gr8Conf
SCM at Trademob
github.com/evgeny-goldin/
@evgeny_goldin
http://thecoolnessfactor.com/2013/01/winds-change/changes-ahead-exit-sign/
In this session
Why FP?
FP building blocks
FP in Groovy
FP candidates
In this session
Mindset switch
Why FP?
Because shit happens
.. or do we make it happen?
Average code is complex
Side-effects go wild
Logical steps are interleaved
Changes are error-prone
http://www.guardian.co.uk/sustainable-business/research-confirms-health-impacts-tire
Average state is fragile and shared
Exposed for updates in any way
Can easily mutate under your feet
Changes are error-prone
http://www.bbcgoodfood.com/content/knowhow/glossary/egg/
Parallelism is hard
“The Multicore Revolution”
Threads + shared mutable state :(
Changes are error-prone
http://www.russel.org.uk/Presentations/euroPython2010_theMulticoreRevolution.pdfhttp://jalopnik.com/5965581/this-is-how-not-to-parallel-park-on-a-hill
Changes are error-prone
http://lenta.ru/sitemap.xml
https://bitbucket.org/evgenyg/devcon-2013/src/master/sitemap-imperative.groovy
https://bitbucket.org/evgenyg/devcon-2013/src/master/sitemap-functional.groovy
ModularityBreak the flow into distinct steps
Do one thing and do it well
FlexibilityShuffle your steps
TestabilityEnsure correctness of your steps
ParallelismScale your steps!
FP building blocks(only a subset this time)
RecursionList = head() + tail()
State is hidden, built up on stack
Recursion eliminationhttps://bitbucket.org/evgenyg/devcon-2013/src/master/recursion.groovy
Immutable StateMoving parts are minimized
No defensive copying
Caching, Parallelism
Immutable StateNew state = f ( old state )
Changes are isolated, constructor only
g ( i + 1 ), g ( list + ‘element’ )
Immutable StatePersistent data structures
Updated in place
Yields a new immutable state
Git
http://git-scm.com/book/en/Git-Branching-What-a-Branch-Is
First-class functionsAnonymous functions
Higher-order functions
Command/Template/Visitor => “Free Willy”
No side-effectssin, cos, max, min, sum, sort
Opens doors for optimizations
Caching, Parallelism
Mindset switch Imperative => Functional
Variables => Values
Classes => Functions
Explicit instructions => Declarative definitions
How (Steps) => What (Result)
Mindset switchPointers, memory allocations, GC - check
State management
Iterations
Filtering, conversion, folding
Parallelism
FP in Groovy
Gradual conversion
http://www.buysgmath.com/p/200/eph-gradually-difficulty-mathematics-1/
final j = 5@Immutable
Anonymous Functionsa.k.a. Closures
Filter - findAll() Map - collect() Fold - inject()
http://www.barlifeuk.com/index.php/2012/08/enter-the-janneau-armagnac-three-musketeers-inspired-competition/
https://bitbucket.org/evgenyg/devcon-2013/src/master/functions.groovy
find()
any()
every()
each()
eachWithIndex()
takeWhile()
dropWhile()
Collection
Range
Map
Array
String
...
Object.iterator()
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
Memoizationa.k.a. Caching
https://bitbucket.org/evgenyg/devcon-2013/src/master/memoization.groovy
TrampolineRecursion => Loop (no stack overflow)https://bitbucket.org/evgenyg/devcon-2013/src/master/trampoline.groovy
GPars!Data Parallelism
https://bitbucket.org/evgenyg/devcon-2013/src/master/gpars.groovy
eachParallel()
collectParallel()
findAllParallel()
everyParallel()
foldParallel()
sumParallel()
gmemoize()
FP Candidates
Loops => any / every
http://en.hdyo.org/tee/questionshttps://bitbucket.org/evgenyg/devcon-2013/src/master/loops.groovy
Split, filter, map, fold
http://www.cityofwaterfalls.ca/hermitage_cascade.html
https://bitbucket.org/evgenyg/devcon-2013/src/master/map-reduce.groovyhttps://bitbucket.org/evgenyg/devcon-2013/src/master/check-versions.groovy
Open your eyes
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 Fundamentals
MIT - Structure and Interpretation of Computer Programs
University of Washington - Programming Languages
Stanford - Programming Paradigms
Thank you!
@evgeny_goldin