Concepts ofFunctional Programming “JavaAbend”, October 2010     Peter Kofler, ‘Code Cop’         @codecopkofler       www....
PETER KOFLER, CODE-COP.ORG                FANATIC ABOUT CODE QUALITY                    ... for Java Brain(dead)s   UML   ...
PETER KOFLER, CODE-COP.ORG                  FANATIC ABOUT CODE QUALITY                             Peter Kofler  • dev sin...
OtherWorlds
f(x)
PETER KOFLER, CODE-COP.ORG          FANATIC ABOUT CODE QUALITY                                 History                    ...
Ideas…
PETER KOFLER, CODE-COP.ORG   FANATIC ABOUT CODE QUALITY                  evaluation of                  mathematical      ...
<code>         def f(x) x+2 end         def f(x:Int) = x+2         public int f(int x) {           return x+2;         }  ...
LambdaCalculus
<math>           x. x+2         f(x) := x+2                       </math>
PETER KOFLER, CODE-COP.ORG        FANATIC ABOUT CODE QUALITY                     Definition f(x)                        x...
No SideEffects!
Pure
ReferentialTransparency
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              ...
State
PETER KOFLER, CODE-COP.ORG   FANATIC ABOUT CODE QUALITY                   avoids                 state and                ...
<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;   }; }                    ...
PETER KOFLER, CODE-COP.ORG        FANATIC ABOUT CODE QUALITY                      first-class &                       anon...
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) ...
PETER KOFLER, CODE-COP.ORG                   FANATIC ABOUT CODE QUALITY                              “normal”             ...
PETER KOFLER, CODE-COP.ORG          FANATIC ABOUT CODE QUALITY                             Evaluationstrict               ...
<code>   length([2+1,1/0])          Error          “2”                   </code>
PETER KOFLER, CODE-COP.ORG       FANATIC ABOUT CODE QUALITY                     everything is                      a funct...
<scala>   def mywhile(cond: =>Boolean)                 (body: =>Unit) {     if (cond) {       body; mywhile(cond)(body)   ...
The almightyList               by Jonas Bonér
PETER KOFLER, CODE-COP.ORG                    FANATIC ABOUT CODE QUALITY                             Iterating            ...
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).            ...
Infinite Lists
Continuations
<scheme>         (* (+ 1 2) 3)                =            (+ 1 2)      with continuation             (* [] 3)            ...
<ruby>   def loop(interrupt)    for i in 1..10     puts "Value of i: #{i}”     if i == interrupt       callcc {|c| return ...
<ruby>   irb(main):007> cont = loop 5   Value of i: 1   ...   Value of i: 5   => #<Continuation:0x2b5a358>   irb(main):008...
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 p...
PETER KOFLER, CODE-COP.ORG          FANATIC ABOUT CODE QUALITY                             Peter Kofler                   ...
PETER KOFLER, CODE-COP.ORG                                                          FANATIC ABOUT CODE QUALITY            ...
PETER KOFLER, CODE-COP.ORG                               FANATIC ABOUT CODE QUALITY                             CC Images ...
PETER KOFLER, CODE-COP.ORG                                FANATIC ABOUT CODE QUALITY                             CC Images...
Concepts of Functional Programming for Java Brains (2010)
Concepts of Functional Programming for Java Brains (2010)
Concepts of Functional Programming for Java Brains (2010)
Concepts of Functional Programming for Java Brains (2010)
Upcoming SlideShare
Loading in …5
×

Concepts of Functional Programming for Java Brains (2010)

1,498 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,498
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Concepts of Functional Programming for Java Brains (2010)

  1. 1. Concepts ofFunctional Programming “JavaAbend”, October 2010 Peter Kofler, ‘Code Cop’ @codecopkofler www.code-cop.org Copyright Peter Kofler, licensed under CC-BY.
  2. 2. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY ... for Java Brain(dead)s UML OOA OOP SOAP
  3. 3. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Peter Kofler • dev since 11 years • “fanatic about code quality” • “Code Cop”
  4. 4. OtherWorlds
  5. 5. f(x)
  6. 6. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY History • 193x Lambda • 1958 LISP • 197x Scheme • 198x Haskell • 199x Common Lisp • 200x Clojure
  7. 7. Ideas…
  8. 8. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY evaluation of mathematical functions
  9. 9. <code> def f(x) x+2 end def f(x:Int) = x+2 public int f(int x) { return x+2; } </code>
  10. 10. LambdaCalculus
  11. 11. <math> x. x+2 f(x) := x+2 </math>
  12. 12. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Definition f(x) x. x + 2 application f(3) (x. x + 2) 3
  13. 13. No SideEffects!
  14. 14. Pure
  15. 15. ReferentialTransparency
  16. 16. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY no side effects pure
  17. 17. Immutable Data
  18. 18. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY final is the new private (Joshua Bloch, Effective Java)
  19. 19. State
  20. 20. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY avoids state and mutable data
  21. 21. <math> n! := n*(n-1)! 0! := 1 f(n) := n*f(n-1) f(0) := 1 </math>
  22. 22. <scala> def fact(n:Int):Int = if (n==0) 1 else n * fact(n-1) </scala>
  23. 23. Higher Order
  24. 24. <math> dx x f(x)  f’(x) </math>
  25. 25. Anonymous Functions
  26. 26. <ruby> array = [3, 5, 7, 9] array.find do |v| v*v > 30 end </ruby>
  27. 27. Closures
  28. 28. <javascript> function derivative(f, dx) { return function(x) { return (f(x+dx)-f(x))/dx; }; } </javascript>
  29. 29. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY first-class & anonymous functions
  30. 30. Currying
  31. 31. <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>
  32. 32. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY “normal” x y. x + y curried x. y. x + y
  33. 33. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Evaluationstrict lazy
  34. 34. <code> length([2+1,1/0])  Error  “2” </code>
  35. 35. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY everything is a function
  36. 36. <scala> def mywhile(cond: =>Boolean) (body: =>Unit) { if (cond) { body; mywhile(cond)(body) } } ... mywhile( i > 0 ) { i -= 1 } </scala>
  37. 37. The almightyList by Jonas Bonér
  38. 38. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Iterating (foreach/each)
  39. 39. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Folding (foldLeft/inject) Reducing (reduceLeft)
  40. 40. <scala> def factorial(n:Int) = ((1 to n) : 1) ( _*_ ) </scala>
  41. 41. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Mapping (map/collect) Binding (flatMap)
  42. 42. <ruby> a = [ "a", "b", "c"] a.collect { |x| x+"!" } => # ["a!", "b!", "c!"] </ruby>
  43. 43. <scala> def list(folder:File) = { folder.listFiles. filter(_.isDirectory). flatMap(list (_)) ++ folder.listFiles. filter(_.isFile) } </scala>
  44. 44. Infinite Lists
  45. 45. Continuations
  46. 46. <scheme> (* (+ 1 2) 3) = (+ 1 2) with continuation (* [] 3) </scheme>
  47. 47. <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>
  48. 48. <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>
  49. 49. Uhhh ?!
  50. 50. Monads
  51. 51. Skipped...• Monads• Category Theory• Pattern Matching• Algebraic Data Types• Type Inference• Hindley Miller• ...
  52. 52. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Thank You
  53. 53. 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%
  54. 54. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Peter Kofler @codecopkofler www.code-cop.org
  55. 55. 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
  56. 56. 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/
  57. 57. 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/

×