SlideShare une entreprise Scribd logo
1  sur  46
Télécharger pour lire hors ligne
Functional Programming
     101 in Groovy



            Evgeny Goldin
  DevCon 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
Are you FP-er?
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.pdf
       http://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
Modularity
Break the flow into distinct steps
  Do one thing and do it well
Flexibility
Shuffle your steps
Testability
Ensure correctness of your steps
Parallelism
Scale your steps!
FP building blocks
(only a subset this time)
Recursion
               List = head() + tail()
  State is hidden, built up on stack
              Recursion elimination
https://bitbucket.org/evgenyg/devcon-2013/src/master/recursion.groovy
Immutable State
Moving parts are minimized
   No defensive copying
   Caching, Parallelism
Immutable State
      New state = f ( old state )
Changes are isolated, constructor only
   g ( i + 1 ), g ( list + ‘element’ )
Immutable State
 Persistent 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 functions
          Anonymous functions
         Higher-order functions
Command/Template/Visitor => “Free Willy”
No side-effects
 sin, 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 switch
Pointers, 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 Functions
     a.k.a. Closures
Filter - findAll()
                                        Map - collect()
                                        Fold - inject()

     https://bitbucket.org/evgenyg/devcon-2013/src/master/functions.groovy


http://www.barlifeuk.com/index.php/2012/08/enter-the-janneau-armagnac-three-
                        musketeers-inspired-competition/
find()
     any()
    every()
    each()
eachWithIndex()
  takeWhile()
  dropWhile()
Collection
     Range
      Map
     Array
     String
       ...
Object.iterator()
Categories, Meta, Extensions
public	
  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
Memoization
                       a.k.a. Caching
https://bitbucket.org/evgenyg/devcon-2013/src/master/memoization.groovy
Trampoline
Recursion => 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




https://bitbucket.org/evgenyg/devcon-2013/src/master/loops.groovy
                http://en.hdyo.org/tee/questions
Split, filter, map, fold



 https://bitbucket.org/evgenyg/devcon-2013/src/master/map-reduce.groovy
https://bitbucket.org/evgenyg/devcon-2013/src/master/check-versions.groovy




       http://www.cityofwaterfalls.ca/hermitage_cascade.html
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
evgenyg@gmail.com

Contenu connexe

En vedette

Engage 2013 - Why Upgrade to v10 Tag
Engage 2013 - Why Upgrade to v10 TagEngage 2013 - Why Upgrade to v10 Tag
Engage 2013 - Why Upgrade to v10 Tag
Webtrends
 
Grafico diario del dax perfomance index para el 09 07-2012
Grafico diario del dax perfomance index para el 09 07-2012Grafico diario del dax perfomance index para el 09 07-2012
Grafico diario del dax perfomance index para el 09 07-2012
Experiencia Trading
 
Система Постройком
Система ПостройкомСистема Постройком
Система Постройком
kulibin
 
Газоанализатор ООО НПП Импульс
Газоанализатор ООО НПП ИмпульсГазоанализатор ООО НПП Импульс
Газоанализатор ООО НПП Импульс
kulibin
 

En vedette (19)

Alimentazione americana e Mc donald' s Vittoria Tomas
Alimentazione americana e Mc donald' s Vittoria TomasAlimentazione americana e Mc donald' s Vittoria Tomas
Alimentazione americana e Mc donald' s Vittoria Tomas
 
Alat ukur
Alat ukurAlat ukur
Alat ukur
 
Power of Personal Branding for Brands - Pubcon 2015
Power of Personal Branding for Brands - Pubcon 2015Power of Personal Branding for Brands - Pubcon 2015
Power of Personal Branding for Brands - Pubcon 2015
 
LinkedIn Endorsements
LinkedIn EndorsementsLinkedIn Endorsements
LinkedIn Endorsements
 
Sharing data from clinical and medical research
Sharing data from clinical and medical researchSharing data from clinical and medical research
Sharing data from clinical and medical research
 
Engage 2013 - Why Upgrade to v10 Tag
Engage 2013 - Why Upgrade to v10 TagEngage 2013 - Why Upgrade to v10 Tag
Engage 2013 - Why Upgrade to v10 Tag
 
Grafico diario del dax perfomance index para el 09 07-2012
Grafico diario del dax perfomance index para el 09 07-2012Grafico diario del dax perfomance index para el 09 07-2012
Grafico diario del dax perfomance index para el 09 07-2012
 
Brochure about Stanford Corporate Governance Research Initiative
Brochure about Stanford Corporate Governance Research InitiativeBrochure about Stanford Corporate Governance Research Initiative
Brochure about Stanford Corporate Governance Research Initiative
 
Tick Achieve - How To Get Stuff Done
Tick Achieve - How To Get Stuff DoneTick Achieve - How To Get Stuff Done
Tick Achieve - How To Get Stuff Done
 
2.7 mbonfim
2.7 mbonfim2.7 mbonfim
2.7 mbonfim
 
獅子山下女同志
獅子山下女同志獅子山下女同志
獅子山下女同志
 
Система Постройком
Система ПостройкомСистема Постройком
Система Постройком
 
Artikel FI
Artikel FIArtikel FI
Artikel FI
 
Ready Player One - Week 2 Check-in
Ready Player One - Week 2 Check-inReady Player One - Week 2 Check-in
Ready Player One - Week 2 Check-in
 
1. cronograma 2014
1. cronograma 20141. cronograma 2014
1. cronograma 2014
 
Guidelines to evaluate blended learning programs
Guidelines to evaluate blended learning programsGuidelines to evaluate blended learning programs
Guidelines to evaluate blended learning programs
 
Blender
BlenderBlender
Blender
 
Газоанализатор ООО НПП Импульс
Газоанализатор ООО НПП ИмпульсГазоанализатор ООО НПП Импульс
Газоанализатор ООО НПП Импульс
 
「旅のことば」について
「旅のことば」について「旅のことば」について
「旅のことば」について
 

Similaire à Functional Programming in Groovy

Industrial Strength Groovy - Tools for the Professional Groovy Developer: Pau...
Industrial Strength Groovy - Tools for the Professional Groovy Developer: Pau...Industrial Strength Groovy - Tools for the Professional Groovy Developer: Pau...
Industrial Strength Groovy - Tools for the Professional Groovy Developer: Pau...
Paul King
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
Guillaume Laforge
 
Psgi Plack Sfpm
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpm
som_nangia
 
Psgi Plack Sfpm
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpm
wilburlo
 

Similaire à Functional Programming in Groovy (20)

What's New in Groovy 1.6?
What's New in Groovy 1.6?What's New in Groovy 1.6?
What's New in Groovy 1.6?
 
The why and how of moving to PHP 5.4/5.5
The why and how of moving to PHP 5.4/5.5The why and how of moving to PHP 5.4/5.5
The why and how of moving to PHP 5.4/5.5
 
Swt J Face 3/3
Swt J Face 3/3Swt J Face 3/3
Swt J Face 3/3
 
Groovy Maven Builds
Groovy Maven BuildsGroovy Maven Builds
Groovy Maven Builds
 
Building resilient services in go
Building resilient services in goBuilding resilient services in go
Building resilient services in go
 
The why and how of moving to php 5.4/5.5
The why and how of moving to php 5.4/5.5The why and how of moving to php 5.4/5.5
The why and how of moving to php 5.4/5.5
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVM
 
Writing your Third Plugin
Writing your Third PluginWriting your Third Plugin
Writing your Third Plugin
 
PHP Performance Trivia
PHP Performance TriviaPHP Performance Trivia
PHP Performance Trivia
 
Industrial Strength Groovy - Tools for the Professional Groovy Developer: Pau...
Industrial Strength Groovy - Tools for the Professional Groovy Developer: Pau...Industrial Strength Groovy - Tools for the Professional Groovy Developer: Pau...
Industrial Strength Groovy - Tools for the Professional Groovy Developer: Pau...
 
Groovy - Grails as a modern scripting language for Web applications
Groovy - Grails as a modern scripting language for Web applicationsGroovy - Grails as a modern scripting language for Web applications
Groovy - Grails as a modern scripting language for Web applications
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
 
Groovy And Grails Introduction
Groovy And Grails IntroductionGroovy And Grails Introduction
Groovy And Grails Introduction
 
The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6
 
Psgi Plack Sfpm
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpm
 
Psgi Plack Sfpm
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpm
 
jQuery Internals + Cool Stuff
jQuery Internals + Cool StuffjQuery Internals + Cool Stuff
jQuery Internals + Cool Stuff
 
groovy & grails - lecture 7
groovy & grails - lecture 7groovy & grails - lecture 7
groovy & grails - lecture 7
 
Intro to PSGI and Plack
Intro to PSGI and PlackIntro to PSGI and Plack
Intro to PSGI and Plack
 
Advanced Topics in Continuous Deployment
Advanced Topics in Continuous DeploymentAdvanced Topics in Continuous Deployment
Advanced Topics in Continuous Deployment
 

Plus de Evgeny Goldin (8)

Alexa skills
Alexa skillsAlexa skills
Alexa skills
 
Polyglot Gradle with Node.js and Play
Polyglot Gradle with Node.js and PlayPolyglot Gradle with Node.js and Play
Polyglot Gradle with Node.js and Play
 
Node.js meets jenkins
Node.js meets jenkinsNode.js meets jenkins
Node.js meets jenkins
 
Release It!
Release It!Release It!
Release It!
 
Spock Extensions Anatomy
Spock Extensions AnatomySpock Extensions Anatomy
Spock Extensions Anatomy
 
10 Cool Facts about Gradle
10 Cool Facts about Gradle10 Cool Facts about Gradle
10 Cool Facts about Gradle
 
Start Writing Groovy
Start Writing GroovyStart Writing Groovy
Start Writing Groovy
 
Maven Plugins
Maven PluginsMaven Plugins
Maven Plugins
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 

Functional Programming in Groovy