Your SlideShare is downloading. ×
  • Like
Concepts of Functional Programming for Java Brains (2010)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Concepts of Functional Programming for Java Brains (2010)

  • 1,045 views
Published

 

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,045
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
7
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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