Upcoming SlideShare
×

Functional Programming Concepts for Imperative Programmers

3,584 views

Published on

Inaugural presentation on functional programming for the JaxFunc meetup.

Published in: Technology
8 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
3,584
On SlideShare
0
From Embeds
0
Number of Embeds
26
Actions
Shares
0
93
0
Likes
8
Embeds 0
No embeds

No notes for slide

Functional Programming Concepts for Imperative Programmers

1. 1. Functional Programming Concepts<br />for Imperative Programmers<br />Imperatively Functional<br />chrislewis / chris@thegodcode.net<br />http://twitter.com/burningodzilla<br />http://github.com/chrislewis<br />
2. 2. Origins: The λ-Calculus<br />
3. 3. Origins: The λ-Calculus<br /><ul><li>Developed at Princeton in the 1930s by Alonzo Church (and others).
4. 4. Formal system for function definition, application, and recursion.
5. 5. Not focused on the physical world, but abstract problems about computation.</li></li></ul><li>Origins: The λ-Calculus<br /><ul><li>Functions may receive functions as parameters and return functions as results.
6. 6. A lambda may “close” over free variables to create a closure.
7. 7. Turing complete.</li></li></ul><li>Origins: The λ-Calculus<br /><ul><li>Functions may receive functions as parameters and return functions as results.
8. 8. A lambda may “close” over free variables to create a closure.
9. 9. Turing complete.</li></ul>true := λxy.x<br />false := λxy.y<br />if-then-else := λpab.p a b<br />
10. 10. Origins: Lisp<br /><ul><li>Developed at MIT by John McCarthy.
11. 11. An Implementation of the λ-calculus.
12. 12. Uniform treatment of data and functions;higher-order functions.</li></li></ul><li>Origins: Lisp<br /><ul><li>Extensive use of lambda expressions, closures, and recursion.
13. 13. In Lisp, as any programming language supporting them, lambdas are functions.</li></li></ul><li>Origins: Lisp<br />(define (is-even? x)<br /> (= 0 (modulo x 2)))<br />(define (when-even x f1 f2)<br /> (if (is-even? x)<br /> (f1 x)<br /> (f2 x)))<br />(define (down-to-one x)<br /> (if (<= x 1)<br />x<br /> (down-to-one (- x 1))))<br />(when-even 4<br /> down-to-one<br /> (lambda (x) (+ x 1)))<br />
14. 14. There are many facets of functional programming.We will focus primarily onhigher-order functions and thetreatment of functions as data.<br />
15. 15. Functions are Data<br />varincrement = function(i) {<br /> return i + 1;<br />}<br />varsquare = function(i) {<br /> return i * i;<br />}<br />function doSomething(i, f) {<br /> return f(i);<br />}<br />
16. 16. Functions are Data<br />doSomething(5, increment);<br />// -> 6<br />doSomething(5, square);<br />// -> 25<br />
17. 17. Functions are Data<br />valincrement = (i: Int) => i + 1<br />valsquare = (i: Int) => i * i<br />def doSomething[A, B](x: A, f: A => B) = f(x)<br />doSomething(5, increment)<br />// -> 6<br />doSomething(5, square)<br />// -> 25<br />
18. 18. Notes on Lambdas<br /><ul><li>A lambda is an anonymous function; a function without an identifier.
19. 19. A closure is a function whose definition “closes” over free variables.</li></li></ul><li>Notes on Lambdas<br /><ul><li>A lambda expression that doesn’t close over free variables isn’t a closure, strictly speaking.
20. 20. A closure isn’t necessarily anonymous!
21. 21. Closures enable function composition …… and that’s a big deal!</li></li></ul><li>λ: In Programming Languages<br /><ul><li>JavaScript has named and anonymous function expressions, as well as Function objects (with a constructor).
22. 22. Ruby most notably has blocks, but also lambdas (keyword) and procs!
23. 23. Scala has methods, function traits, and shorthand yielding trait instances.
24. 24. How a programming language refers to such a construct is entirely preferential.</li></li></ul><li>Function Composition<br />
25. 25. <ul><li>com·po·si·tion-noun1. the act of combining parts or elements to form a whole2. the resulting state or product3. manner of being composed;structure: This painting has an orderly composition.</li></li></ul><li><ul><li>com·po·si·tion-noun1. the act of combining parts or elements to form a whole2. the resulting state or product3. manner of being composed;structure: This painting has an orderly composition.</li></ul>Let’s glance back at the math…<br />
26. 26. Function Composition<br /><ul><li>y = f(x) or f: x -> y
27. 27. “y is a function of x”
28. 28. The value of y depends on x; a simple mapping from one value, x, to another, y.</li></li></ul><li>Function Composition<br /><ul><li>y = f(x) or f: x -> y
29. 29. “y is a function of x”
30. 30. The value of y depends on x; a simple mapping from one value, x, to another, y.
31. 31. f: x -> yand g: y -> z
32. 32. The domains of these functions are suitable for composition.</li></li></ul><li>Function Composition<br /><ul><li>We can combine functions with compatible domains.</li></li></ul><li>Function Composition<br /><ul><li>We can combine functions with compatible domains.
33. 33. (g . f) = g(f(x)) or (g ∘ f) = g(f(x))
34. 34. Combine the functions g and f into a new function that applies g to the result of applying f to x.</li></li></ul><li>Function Composition<br /><ul><li>We can combine functions with compatible domains.
35. 35. (g . f) = g(f(x)) or (g ∘ f) = g(f(x))
36. 36. Combine the functions g and f into a new function that applies g to the result of applying f to x.</li></ul>Back to the code!<br />
37. 37. In JavaScript:<br />function compose(f, g) {<br /> // a closure, closing over the free<br /> // variables f and g<br /> return function(x) {<br /> return g(f(x));<br /> }<br />}<br />// recursive implementation:<br />// http://gist.github.com/366614<br />
38. 38. In Scala:<br />def compose[A, B, C](g: B => C, f: A => B) =<br /> (x: A) => g(f(x)) // the closure<br />
39. 39. Higher-Order Functionsand Lists<br /><ul><li>A function that takes another function as an argument and/or returns a function as its result.
40. 40. Ruby, via Rails, brought higher-order functions that operate onlists mainstream.
41. 41. Higher-order rockstars:each, map, filter, fold many more!</li></li></ul><li>Because we know how to pass functions around, we know how to implement these …<br />
42. 42. Because we know how to pass functions around, we know how to implement these …… but we’ll probably duplicate code.<br />
43. 43. Because we know how to pass functions around, we know how to implement these …… but we’ll probably duplicate code.Let’s talk about folding.<br />
44. 44. In JavaScript:<br />function foldr(list, init, f) {<br />if(list.length == 0)<br /> return init;<br /> else {<br /> return foldr(list.slice(1), f(init, list[0]), f);<br /> }<br />}<br />
45. 45. In Scala:<br />def foldr[A, B](list: List[A], init: B, f: (B, A) => B): B =<br /> list match {<br /> case Nil => init<br /> case head :: tail => foldr(tail, f(init, head), f)<br /> }<br />
46. 46. Some List Functions That can be Defined in Terms of Folding<br /><ul><li>sum = foldr (+) 0
47. 47. product = foldr (*) 1
48. 48. anytrue = foldr (∨) false
49. 49. alltrue = foldr (∧) true
50. 50. map = foldr (cons . f) []
51. 51. By partially applying folds, we can define new list functions with very little programming!</li></li></ul><li>Real-World Examples<br /><ul><li>Higher-order functions pay dividends (strategy pattern, factory, factories of factories, …)
52. 52. “loan” pattern.
53. 53. Event handling in the Lift framework.
54. 54. List tasks (partitioning, filtering, transforming).</li></li></ul><li>What we Didn’t Cover<br /><ul><li>Currying
55. 55. Pattern matching
56. 56. Type inference, higher-kinded types, and general type systems
57. 57. Partial function application