SlideShare uma empresa Scribd logo
1 de 61
Baixar para ler offline
Concepts of
Functional Programming
 “JavaAbend”, October 2010


     Peter Kofler, ‘Code Cop’
         @codecopkofler
       www.code-cop.org

    Copyright Peter Kofler, licensed under CC-BY.
PETER KOFLER, CODE-COP.ORG                FANATIC ABOUT CODE QUALITY




                    ... for Java Brain(dead)s

   UML
   OOA
    OOP
    SOAP
PETER KOFLER, CODE-COP.ORG                  FANATIC ABOUT CODE QUALITY




                             Peter Kofler
  • dev since 11 years


  • “fanatic about
    code quality”


  • “Code Cop”
Other
Worlds
f(x)
PETER KOFLER, CODE-COP.ORG          FANATIC ABOUT CODE QUALITY




                                 History
                             • 193x Lambda
                             • 1958 LISP
                             • 197x Scheme
                             • 198x Haskell
                             • 199x Common
                               Lisp
                             • 200x Clojure
Ideas…
PETER KOFLER, CODE-COP.ORG   FANATIC ABOUT CODE QUALITY




                  evaluation of
                  mathematical
                   functions
<code>



         def f(x) x+2 end
         def f(x:Int) = x+2
         public int f(int x) {
           return x+2;
         }
                                 </code>
Lambda
Calculus
<math>




           x. x+2
         f(x) := x+2
                       </math>
PETER KOFLER, CODE-COP.ORG        FANATIC ABOUT CODE QUALITY




                     Definition f(x)
                        x. x + 2
                    application f(3)
                      (x. x + 2) 3
No
 Side
Effects!
Pure
Referential




Transparency
PETER KOFLER, CODE-COP.ORG    FANATIC ABOUT CODE QUALITY




                 no side effects
                     pure
Immutable
  Data
PETER KOFLER, CODE-COP.ORG                  FANATIC ABOUT CODE QUALITY




                                 final
                             is the new
                               private
                  (Joshua Bloch, Effective Java)
State
PETER KOFLER, CODE-COP.ORG   FANATIC ABOUT CODE QUALITY




                   avoids
                 state and
                mutable data
<math>




   n! := n*(n-1)!
   0! := 1
   f(n) := n*f(n-1)
   f(0) := 1      </math>
<scala>




    def fact(n:Int):Int =
     if (n==0) 1 else
     n * fact(n-1)
                        </scala>
Higher
 Order
<math>




         dx
          x f(x)
           
          f’(x)
                   </math>
Anonymous
 Functions
<ruby>




 array = [3, 5, 7, 9]
 array.find do |v|
   v*v > 30
 end
                        </ruby>
Closures
<javascript>




 function derivative(f, dx) {
   return function(x) {
      return (f(x+dx)-f(x))/dx;
   };
 }
                           </javascript>
PETER KOFLER, CODE-COP.ORG        FANATIC ABOUT CODE QUALITY




                      first-class &
                       anonymous
                       functions
Currying
<scala>


   f(x,y) := x+y 
   def f(x:Int,y:Int) = x+y

   f1(x) returns f2(y) 
   def f1(x:Int) = (i:Int) => x+i
   def f2(y) = xnow constant+y
                                    </scala>
PETER KOFLER, CODE-COP.ORG                   FANATIC ABOUT CODE QUALITY




                              “normal”
                              x y. x + y
                               curried
                             x. y. x + y
PETER KOFLER, CODE-COP.ORG          FANATIC ABOUT CODE QUALITY




                             Evaluation


strict
                             lazy
<code>




   length([2+1,1/0])

          Error
          “2”
                   </code>
PETER KOFLER, CODE-COP.ORG       FANATIC ABOUT CODE QUALITY




                     everything is
                      a function
<scala>


   def mywhile(cond: =>Boolean)
                 (body: =>Unit) {
     if (cond) {
       body; mywhile(cond)(body)
     }
   }
   ... mywhile( i > 0 ) { i -= 1 }
                                 </scala>
The almighty




List
               by Jonas Bonér
PETER KOFLER, CODE-COP.ORG                    FANATIC ABOUT CODE QUALITY




                             Iterating
                             (foreach/each)
PETER KOFLER, CODE-COP.ORG   FANATIC ABOUT CODE QUALITY




  Folding (foldLeft/inject)
  Reducing (reduceLeft)
<scala>




    def factorial(n:Int) =
    ((1 to n) : 1) ( _*_ )

                          </scala>
PETER KOFLER, CODE-COP.ORG   FANATIC ABOUT CODE QUALITY




  Mapping (map/collect)
  Binding (flatMap)
<ruby>




     a = [ "a", "b", "c"]
     a.collect { |x| x+"!" }

     => # ["a!", "b!", "c!"]
                           </ruby>
<scala>



          def list(folder:File) = {
            folder.listFiles.
                 filter(_.isDirectory).
                 flatMap(list (_)) ++
            folder.listFiles.
                 filter(_.isFile)
          }
                                          </scala>
Infinite
 Lists
Continuations
<scheme>



         (* (+ 1 2) 3)
                =
            (+ 1 2)
      with continuation
             (* [] 3)
                      </scheme>
<ruby>


   def loop(interrupt)
    for i in 1..10
     puts "Value of i: #{i}”
     if i == interrupt
       callcc {|c| return c}
     end
    end
   end
                         </ruby by Bruce Tate>
<ruby>

   irb(main):007> cont = loop 5
   Value of i: 1
   ...
   Value of i: 5
   => #<Continuation:0x2b5a358>
   irb(main):008> cont.call
   Value of i: 6
   ...
   Value of i: 10
                        </ruby by Bruce Tate>
Uhhh ?!
Monads
Skipped...
•   Monads
•   Category Theory
•   Pattern Matching
•   Algebraic Data Types
•   Type Inference
•   Hindley Miller
•   ...
PETER KOFLER, CODE-COP.ORG     FANATIC ABOUT CODE QUALITY




                             Thank
                              You
PETER KOFLER, CODE-COP.ORG                      FANATIC ABOUT CODE QUALITY




                      Acknowledgements
  • This presentation was supported by
    System One (as Research Day)
       (http://www.systemone.net/en/) ... 20%



  • This presentation was supported by sIT
    Solutions (for Developer Round Table)
       (http://www.s-itsolutions.at/) ... 50%
PETER KOFLER, CODE-COP.ORG          FANATIC ABOUT CODE QUALITY




                             Peter Kofler

                    @codecopkofler

      www.code-cop.org
PETER KOFLER, CODE-COP.ORG                                                          FANATIC ABOUT CODE QUALITY




                                             Links
  •    http://alan.dipert.org/post/307586762/polyglot-folding-ruby-clojure-scala
  •    http://blog.tmorris.net/what-does-functional-programming-mean/
  •    http://blogs.tedneward.com/2010/03/23/How+To+And+Not+To+Give+A+Talk+On+F.aspx
  •    http://deadprogrammersociety.blogspot.com/2007/02/ruby-blocks-closures-and-continuations.html

  •    http://en.wikipedia.org/wiki/Functional_programming
  •    http://stackoverflow.com/questions/1112773/what-are-the-core-concepts-in-functional-programming

  •    http://www.defmacro.org/ramblings/fp.html
  •    http://www.ibm.com/developerworks/java/library/j-cb03216/ (Continuations)
  •    http://www.infoq.com/presentations/Functional-Languages-101
PETER KOFLER, CODE-COP.ORG                               FANATIC ABOUT CODE QUALITY




                             CC Images #1
  •    zombie: http://www.flickr.com/photos/vincetemplement/3333125657/
  •    spray face: http://www.flickr.com/photos/iangallagher/4115047191/
  •    other worlds: http://www.flickr.com/photos/tohoscope/43818252/
  •    new ideas: http://www.flickr.com/photos/jpovey/2051196149/
  •    history: http://www.flickr.com/photos/shadowgate/2679760160/
  •    functions: http://www.flickr.com/photos/stefz/2159280574/
  •    lambda: http://www.flickr.com/photos/itsgreg/419031515/
  •    effects: http://www.flickr.com/photos/delgrossodotcom/3094902951/
  •    pure: http://www.flickr.com/photos/10451396@N00/429388973/
  •    dices: http://www.flickr.com/photos/dicemanic/19743895/
  •    lock: http://www.flickr.com/photos/bpc009/3328427457/
  •    states: http://www.flickr.com/photos/krazydad/2986774792/
  •    recursion: http://www.flickr.com/photos/torley/2361164281/
PETER KOFLER, CODE-COP.ORG                                FANATIC ABOUT CODE QUALITY




                             CC Images #2
  •    higher: http://www.flickr.com/photos/brent_nashville/2204427649/
  •    unknown: http://www.flickr.com/photos/laughingsquid/2443128847/
  •    closed: http://www.flickr.com/photos/functoruser/244208110/
  •    currying: http://www.flickr.com/photos/dumbeast/315869395/
  •    strict: http://www.flickr.com/photos/williac/99551756/
  •    lazy: http://www.flickr.com/photos/ucumari/2813269535/
  •    möbius: http://www.flickr.com/photos/serafa/2590342868/
  •    uhh: http://www.flickr.com/photos/jswaby/1178547691/
  •    seaside: http://www.flickr.com/photos/rgtmum/2280206308/
  •    skyscraper: http://www.flickr.com/photos/sanbeiji/81110217/
  •    monads: http://www.flickr.com/photos/adamrice/3266888223/
  •    questions: http://www.flickr.com/photos/seandreilinger/2326448445/

Mais conteúdo relacionado

Mais procurados

Can PL/SQL be Clean? (2013)
Can PL/SQL be Clean? (2013)Can PL/SQL be Clean? (2013)
Can PL/SQL be Clean? (2013)
Peter Kofler
 
The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)
Peter Kofler
 
Code Retreat Graz, Austria 2013
Code Retreat Graz, Austria 2013Code Retreat Graz, Austria 2013
Code Retreat Graz, Austria 2013
Peter Kofler
 
Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)
Peter Kofler
 
Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)
Peter Kofler
 
Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)
Peter Kofler
 

Mais procurados (20)

Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
 
Coding Dojo: Baby Steps Push Challenge (2021)
Coding Dojo: Baby Steps Push Challenge (2021)Coding Dojo: Baby Steps Push Challenge (2021)
Coding Dojo: Baby Steps Push Challenge (2021)
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test Framework
 
Can PL/SQL be Clean? (2013)
Can PL/SQL be Clean? (2013)Can PL/SQL be Clean? (2013)
Can PL/SQL be Clean? (2013)
 
The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)
 
Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)
 
Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)
 
TDD as if You Meant It (2013)
TDD as if You Meant It (2013)TDD as if You Meant It (2013)
TDD as if You Meant It (2013)
 
Code Retreat Graz, Austria 2013
Code Retreat Graz, Austria 2013Code Retreat Graz, Austria 2013
Code Retreat Graz, Austria 2013
 
Coding Dojo: Bank OCR Outside-In (2015)
Coding Dojo: Bank OCR Outside-In (2015)Coding Dojo: Bank OCR Outside-In (2015)
Coding Dojo: Bank OCR Outside-In (2015)
 
Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)
 
Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)
 
Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)
 
Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)
 
Clean Readable Specifications (ETC 2016)
Clean Readable Specifications (ETC 2016)Clean Readable Specifications (ETC 2016)
Clean Readable Specifications (ETC 2016)
 
Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)
 
JUnit Boot Camp (GeeCON 2016)
JUnit Boot Camp (GeeCON 2016)JUnit Boot Camp (GeeCON 2016)
JUnit Boot Camp (GeeCON 2016)
 
Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)
 
Pragmatic Introduction to Python Unit Testing (PyDays 2018)
Pragmatic Introduction to Python Unit Testing (PyDays 2018)Pragmatic Introduction to Python Unit Testing (PyDays 2018)
Pragmatic Introduction to Python Unit Testing (PyDays 2018)
 
Code Quality Assurance
Code Quality AssuranceCode Quality Assurance
Code Quality Assurance
 

Semelhante a Concepts of Functional Programming for Java Brains (2010)

Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)
Peter Kofler
 

Semelhante a Concepts of Functional Programming for Java Brains (2010) (20)

Metaprogramming in julia
Metaprogramming in juliaMetaprogramming in julia
Metaprogramming in julia
 
LLVM Internal Architecture par Michel Guillet
LLVM Internal Architecture par Michel GuilletLLVM Internal Architecture par Michel Guillet
LLVM Internal Architecture par Michel Guillet
 
Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)
 
Sync considered unethical
Sync considered unethicalSync considered unethical
Sync considered unethical
 
Introduction to Kotlin
Introduction to KotlinIntroduction to Kotlin
Introduction to Kotlin
 
Angular2 for Beginners
Angular2 for BeginnersAngular2 for Beginners
Angular2 for Beginners
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
 
Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?
 
The Present and Future of the Web Platform
The Present and Future of the Web PlatformThe Present and Future of the Web Platform
The Present and Future of the Web Platform
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010
 
201705 metaprogramming in julia
201705 metaprogramming in julia201705 metaprogramming in julia
201705 metaprogramming in julia
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android Update
 
ECMAScript.Next ECMAScipt 6
ECMAScript.Next ECMAScipt 6ECMAScript.Next ECMAScipt 6
ECMAScript.Next ECMAScipt 6
 
Es.next
Es.nextEs.next
Es.next
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)
 
JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016
 
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem novaKotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
 

Mais de Peter Kofler

Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)
Peter Kofler
 
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Peter Kofler
 
Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)
Peter Kofler
 
Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)
Peter Kofler
 

Mais de Peter Kofler (13)

Refactoring the Tennis Kata v2 (2016)
Refactoring the Tennis Kata v2 (2016)Refactoring the Tennis Kata v2 (2016)
Refactoring the Tennis Kata v2 (2016)
 
Designing Test Cases for the Gilded Rose Kata v3 (2016)
Designing Test Cases for the Gilded Rose Kata v3 (2016)Designing Test Cases for the Gilded Rose Kata v3 (2016)
Designing Test Cases for the Gilded Rose Kata v3 (2016)
 
Coding Dojo: Asynchronous Clock-In (2016)
Coding Dojo: Asynchronous Clock-In (2016)Coding Dojo: Asynchronous Clock-In (2016)
Coding Dojo: Asynchronous Clock-In (2016)
 
Mob Programming (2016)
Mob Programming (2016)Mob Programming (2016)
Mob Programming (2016)
 
Code Retreat Venice (2016)
Code Retreat Venice (2016)Code Retreat Venice (2016)
Code Retreat Venice (2016)
 
Coding Dojo: Data Munging (2016)
Coding Dojo: Data Munging (2016)Coding Dojo: Data Munging (2016)
Coding Dojo: Data Munging (2016)
 
GDCR15 in Las Palmas, Gran Canaria
GDCR15 in Las Palmas, Gran CanariaGDCR15 in Las Palmas, Gran Canaria
GDCR15 in Las Palmas, Gran Canaria
 
Pair Programming (2015)
Pair Programming (2015)Pair Programming (2015)
Pair Programming (2015)
 
Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)
 
Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)
 
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
 
Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)
 
Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
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
 

Último (20)

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.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
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Concepts of Functional Programming for Java Brains (2010)

  • 1. Concepts of Functional Programming “JavaAbend”, October 2010 Peter Kofler, ‘Code Cop’ @codecopkofler www.code-cop.org Copyright Peter Kofler, licensed under CC-BY.
  • 2. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY ... for Java Brain(dead)s UML OOA OOP SOAP
  • 3.
  • 4. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Peter Kofler • dev since 11 years • “fanatic about code quality” • “Code Cop”
  • 7. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY History • 193x Lambda • 1958 LISP • 197x Scheme • 198x Haskell • 199x Common Lisp • 200x Clojure
  • 9. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY evaluation of mathematical functions
  • 10.
  • 11. <code> def f(x) x+2 end def f(x:Int) = x+2 public int f(int x) { return x+2; } </code>
  • 13. <math> x. x+2 f(x) := x+2 </math>
  • 14. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Definition f(x) x. x + 2 application f(3) (x. x + 2) 3
  • 16. Pure
  • 18. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY no side effects pure
  • 20. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY final is the new private (Joshua Bloch, Effective Java)
  • 21. State
  • 22. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY avoids state and mutable data
  • 23.
  • 24. <math> n! := n*(n-1)! 0! := 1 f(n) := n*f(n-1) f(0) := 1 </math>
  • 25. <scala> def fact(n:Int):Int = if (n==0) 1 else n * fact(n-1) </scala>
  • 27. <math> dx x f(x)  f’(x) </math>
  • 29. <ruby> array = [3, 5, 7, 9] array.find do |v| v*v > 30 end </ruby>
  • 31. <javascript> function derivative(f, dx) { return function(x) { return (f(x+dx)-f(x))/dx; }; } </javascript>
  • 32. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY first-class & anonymous functions
  • 34. <scala> f(x,y) := x+y  def f(x:Int,y:Int) = x+y f1(x) returns f2(y)  def f1(x:Int) = (i:Int) => x+i def f2(y) = xnow constant+y </scala>
  • 35. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY “normal” x y. x + y curried x. y. x + y
  • 36. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Evaluation strict lazy
  • 37. <code> length([2+1,1/0])  Error  “2” </code>
  • 38. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY everything is a function
  • 39. <scala> def mywhile(cond: =>Boolean) (body: =>Unit) { if (cond) { body; mywhile(cond)(body) } } ... mywhile( i > 0 ) { i -= 1 } </scala>
  • 40. The almighty List by Jonas Bonér
  • 41. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Iterating (foreach/each)
  • 42. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Folding (foldLeft/inject) Reducing (reduceLeft)
  • 43. <scala> def factorial(n:Int) = ((1 to n) : 1) ( _*_ ) </scala>
  • 44. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Mapping (map/collect) Binding (flatMap)
  • 45. <ruby> a = [ "a", "b", "c"] a.collect { |x| x+"!" } => # ["a!", "b!", "c!"] </ruby>
  • 46. <scala> def list(folder:File) = { folder.listFiles. filter(_.isDirectory). flatMap(list (_)) ++ folder.listFiles. filter(_.isFile) } </scala>
  • 49. <scheme> (* (+ 1 2) 3) = (+ 1 2) with continuation (* [] 3) </scheme>
  • 50. <ruby> def loop(interrupt) for i in 1..10 puts "Value of i: #{i}” if i == interrupt callcc {|c| return c} end end end </ruby by Bruce Tate>
  • 51. <ruby> irb(main):007> cont = loop 5 Value of i: 1 ... Value of i: 5 => #<Continuation:0x2b5a358> irb(main):008> cont.call Value of i: 6 ... Value of i: 10 </ruby by Bruce Tate>
  • 53.
  • 55. Skipped... • Monads • Category Theory • Pattern Matching • Algebraic Data Types • Type Inference • Hindley Miller • ...
  • 56. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Thank You
  • 57. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Acknowledgements • This presentation was supported by System One (as Research Day) (http://www.systemone.net/en/) ... 20% • This presentation was supported by sIT Solutions (for Developer Round Table) (http://www.s-itsolutions.at/) ... 50%
  • 58. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Peter Kofler @codecopkofler www.code-cop.org
  • 59. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Links • http://alan.dipert.org/post/307586762/polyglot-folding-ruby-clojure-scala • http://blog.tmorris.net/what-does-functional-programming-mean/ • http://blogs.tedneward.com/2010/03/23/How+To+And+Not+To+Give+A+Talk+On+F.aspx • http://deadprogrammersociety.blogspot.com/2007/02/ruby-blocks-closures-and-continuations.html • http://en.wikipedia.org/wiki/Functional_programming • http://stackoverflow.com/questions/1112773/what-are-the-core-concepts-in-functional-programming • http://www.defmacro.org/ramblings/fp.html • http://www.ibm.com/developerworks/java/library/j-cb03216/ (Continuations) • http://www.infoq.com/presentations/Functional-Languages-101
  • 60. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY CC Images #1 • zombie: http://www.flickr.com/photos/vincetemplement/3333125657/ • spray face: http://www.flickr.com/photos/iangallagher/4115047191/ • other worlds: http://www.flickr.com/photos/tohoscope/43818252/ • new ideas: http://www.flickr.com/photos/jpovey/2051196149/ • history: http://www.flickr.com/photos/shadowgate/2679760160/ • functions: http://www.flickr.com/photos/stefz/2159280574/ • lambda: http://www.flickr.com/photos/itsgreg/419031515/ • effects: http://www.flickr.com/photos/delgrossodotcom/3094902951/ • pure: http://www.flickr.com/photos/10451396@N00/429388973/ • dices: http://www.flickr.com/photos/dicemanic/19743895/ • lock: http://www.flickr.com/photos/bpc009/3328427457/ • states: http://www.flickr.com/photos/krazydad/2986774792/ • recursion: http://www.flickr.com/photos/torley/2361164281/
  • 61. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY CC Images #2 • higher: http://www.flickr.com/photos/brent_nashville/2204427649/ • unknown: http://www.flickr.com/photos/laughingsquid/2443128847/ • closed: http://www.flickr.com/photos/functoruser/244208110/ • currying: http://www.flickr.com/photos/dumbeast/315869395/ • strict: http://www.flickr.com/photos/williac/99551756/ • lazy: http://www.flickr.com/photos/ucumari/2813269535/ • möbius: http://www.flickr.com/photos/serafa/2590342868/ • uhh: http://www.flickr.com/photos/jswaby/1178547691/ • seaside: http://www.flickr.com/photos/rgtmum/2280206308/ • skyscraper: http://www.flickr.com/photos/sanbeiji/81110217/ • monads: http://www.flickr.com/photos/adamrice/3266888223/ • questions: http://www.flickr.com/photos/seandreilinger/2326448445/