concepts of functional programming for java brains (2010)
DESCRIPTION
TRANSCRIPT
![Page 1: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/1.jpg)
Concepts of Functional Programming“JavaAbend”, October 2010
Peter Kofler, ‘Code Cop’@codecopkofler
www.code-cop.org
Copyright Peter Kofler, licensed under CC-BY.
![Page 2: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/2.jpg)
UML OOA OOP SOAP
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
... for Java Brain(dead)s
![Page 3: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/3.jpg)
![Page 4: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/4.jpg)
Peter Kofler
• dev since 11 years
• “fanatic aboutcode quality”
• “Code Cop”
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 5: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/5.jpg)
Other Worlds
![Page 6: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/6.jpg)
f(x)
![Page 7: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/7.jpg)
History
• 193x Lambda• 1958 LISP• 197x Scheme• 198x Haskell• 199x Common
Lisp• 200x Clojure
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 8: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/8.jpg)
Ideas…
![Page 9: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/9.jpg)
evaluation of mathematical
functions
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 10: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/10.jpg)
![Page 11: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/11.jpg)
def f(x) x+2 enddef f(x:Int) = x+2public int f(int x) { return x+2;
}
<code>
</code>
![Page 12: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/12.jpg)
Lambda Calculus
![Page 13: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/13.jpg)
x. x+2f(x) := x+2
<math>
</math>
![Page 14: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/14.jpg)
Definition f(x)x. x + 2
application f(3)(x. x + 2) 3
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 15: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/15.jpg)
No Side
Effects!
![Page 16: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/16.jpg)
Pure
![Page 17: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/17.jpg)
Transparency
Referential
![Page 18: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/18.jpg)
no side effectspure
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 19: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/19.jpg)
Immutable Data
![Page 20: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/20.jpg)
final is the new
private(Joshua Bloch, Effective Java)
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 21: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/21.jpg)
State
![Page 22: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/22.jpg)
avoidsstate and
mutable data
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 23: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/23.jpg)
![Page 24: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/24.jpg)
n! := n*(n-1)!0! := 1f(n) := n*f(n-1)f(0) := 1
<math>
</math>
![Page 25: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/25.jpg)
def fact(n:Int):Int = if (n==0) 1 else n * fact(n-1)
<scala>
</scala>
![Page 26: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/26.jpg)
Higher Order
![Page 27: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/27.jpg)
f(x)f’(x)
dxx
<math>
</math>
![Page 28: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/28.jpg)
AnonymousFunctions
![Page 29: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/29.jpg)
array = [3, 5, 7, 9]array.find do |v| v*v > 30end
<ruby>
</ruby>
![Page 30: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/30.jpg)
Closures
![Page 31: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/31.jpg)
function derivative(f, dx) { return function(x) { return (f(x+dx)-f(x))/dx; };}
<javascript>
</javascript>
![Page 32: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/32.jpg)
first-class & anonymousfunctions
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 33: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/33.jpg)
Currying
![Page 34: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/34.jpg)
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+idef f2(y) = xnow constant+y
<scala>
</scala>
![Page 35: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/35.jpg)
“normal”x y. x + y
curriedx. y. x + y
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 36: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/36.jpg)
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Evaluation
lazy
strict
![Page 37: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/37.jpg)
Error “2”
<code>
</code>
length([2+1,1/0])
![Page 38: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/38.jpg)
everything isa function
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 39: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/39.jpg)
def mywhile(cond: =>Boolean) (body: =>Unit) { if (cond) { body; mywhile(cond)(body) }}... mywhile( i > 0 ) { i -= 1 }
<scala>
</scala>
![Page 40: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/40.jpg)
The almighty
Listby Jonas Bonér
![Page 41: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/41.jpg)
Iterating(foreach/each)
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 42: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/42.jpg)
Folding (foldLeft/inject)
Reducing (reduceLeft)
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 43: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/43.jpg)
</scala>
<scala>
def factorial(n:Int) = ((1 to n) :\ 1) ( _*_ )
![Page 44: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/44.jpg)
Mapping (map/collect)
Binding (flatMap)
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 45: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/45.jpg)
</ruby>
<ruby>
a = [ "a", "b", "c"] a.collect { |x| x+"!" } => # ["a!", "b!", "c!"]
![Page 46: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/46.jpg)
</scala>
<scala>
def list(folder:File) = { folder.listFiles. filter(_.isDirectory). flatMap(list (_)) ++ folder.listFiles. filter(_.isFile)}
![Page 47: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/47.jpg)
Infinite Lists
![Page 48: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/48.jpg)
Continuations
![Page 49: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/49.jpg)
(* (+ 1 2) 3)=
(+ 1 2) with continuation
(* [] 3)
<scheme>
</scheme>
![Page 50: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/50.jpg)
def loop(interrupt) for i in 1..10 puts "Value of i: #{i}” if i == interrupt callcc {|c| return c} end endend
<ruby>
</ruby by Bruce Tate>
![Page 51: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/51.jpg)
irb(main):007> cont = loop 5Value of i: 1...Value of i: 5=> #<Continuation:0x2b5a358>irb(main):008> cont.callValue of i: 6...Value of i: 10
<ruby>
</ruby by Bruce Tate>
![Page 52: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/52.jpg)
Uhhh ?!
![Page 53: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/53.jpg)
![Page 54: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/54.jpg)
Monads
![Page 55: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/55.jpg)
Skipped...
• Monads• Category Theory• Pattern Matching• Algebraic Data Types• Type Inference• Hindley Miller• ...
![Page 56: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/56.jpg)
Thank You
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 57: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/57.jpg)
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
![Page 58: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/58.jpg)
Peter Kofler
@codecopkofler
www.code-cop.org
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
![Page 59: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/59.jpg)
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
![Page 60: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/60.jpg)
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
![Page 61: Concepts of Functional Programming for Java Brains (2010)](https://reader033.vdocuments.net/reader033/viewer/2022042813/54c41fe54a795978638b457e/html5/thumbnails/61.jpg)
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/
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY