introduction to functional programming in javascript
DESCRIPTION
A presentation I did for work on functional programming. It's meant as an introduction to functional programming, and I implemented the fundamentals of functional programming (Church Numerals, Y-Combinator, etc.) in JavaScript.TRANSCRIPT
![Page 1: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/1.jpg)
Functional Programming
Tommy “is awesome” Montgomery
2009-03-06
![Page 2: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/2.jpg)
What is functional programming? Uses functions Lambda Calculus Very academic Kinda goofy
![Page 3: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/3.jpg)
Elements of functional languages Recursion Functions (der) No state
![Page 4: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/4.jpg)
Functional Languages Haskell Erlang F# OCaml Scheme Smalltalk
J K Mathematica XSLT LISP (kinda)
![Page 5: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/5.jpg)
Fun Fact #1 Tommy was once a professional musician
![Page 6: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/6.jpg)
λ-calculus Anonymous functions
JavaScript PHP 4.0.1 – PHP 5.2.x (kinda) PHP 5.3 (more kinda) C# 2.0 Java sucks, as usual
Unary Functions take one argument, and return one
value
![Page 7: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/7.jpg)
Example
λ x . x + 2
![Page 8: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/8.jpg)
Example
λ x . x + 2
input
![Page 9: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/9.jpg)
Example
λ x . x + 2
input return value
![Page 10: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/10.jpg)
Example
λ x . x + 2
input return value
f(x) = x + 2
![Page 11: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/11.jpg)
Higher order functions Functions that take functions as arguments and return
functions Where the real power of functional programming lies
![Page 12: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/12.jpg)
Higher order function example Mathematical derivative
xxdx
d22
![Page 13: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/13.jpg)
Higher order function example
//f is a functionfunction derivative(f) { return function(x) { //approximation of derivative return (f(x + 0.00001) – f(x)) / 0.00001; }}
![Page 14: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/14.jpg)
Higher order function example
//evaluate derivative of x2:var deriv_x_squared = derivative( function(x) { return x*x; });
alert(deriv_x_squared(3)); //alerts 6ish
![Page 15: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/15.jpg)
Fun Fact #2 Tommy used to be a gangsta
![Page 16: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/16.jpg)
Bound variables The λ operator binds
its variables to its scope
All other variables are “free”
Freedom
![Page 17: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/17.jpg)
Bound variables
λ x . x + y
![Page 18: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/18.jpg)
Bound variables
λ x . x + y bound variable bound variable free variable
![Page 19: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/19.jpg)
Bound variables
λ x . x + y bound variable bound variable free variable
f(x) = x + y
![Page 20: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/20.jpg)
Natural numbers Everything is a function, remember? How do we define the natural numbers in
functional programming (1, 2, 3, …)?
![Page 21: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/21.jpg)
very painfully
![Page 22: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/22.jpg)
Church numerals 0 ≡ λ f . λ x . x 1 ≡ λ f . λ x . f x 2 ≡ λ f . λ x . f (f x) 3 ≡ λ f . λ x . f (f (f x)) ...
![Page 23: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/23.jpg)
Church numerals in JavaScript//identity function// λ x . xfunction identity(x) { return x;}
//Church numeral zero// λ f . λ x . xfunction zero(f) { return identity;}
![Page 24: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/24.jpg)
Church numerals in JavaScript//successor function (succ(n) = n + 1)// λ n . λ f . f (n f x)function succ(n) { return function(f) { return function(x) { return f(n(f)(x)); } }}
![Page 25: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/25.jpg)
Church numerals in JavaScript//gets a function representing//the nth church numberfunction getChurchNumber(n) { var ret = zero; for (var i = 0; i < n; i++) { ret = succ(ret); } return ret;}
![Page 26: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/26.jpg)
Church numerals in JavaScript//gets the nth church numberfunction getNaturalNumber(n) { var value = 0; for (var i = 0; i < n; i++) { value += getChurchNumber(i)( function(x) { return x + 1; } )(0); } return value;}
![Page 27: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/27.jpg)
Fun fact #3 Wombats are unfairly cute
![Page 28: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/28.jpg)
Addition// λ m . λ n . λ f . n f (m f x)function add(m) { return function(n) { return function(f) { return function(x) { return n(f)(m(f)(x)); } } }}
![Page 29: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/29.jpg)
Addition using successor// λ m . λ n . m succ n
function addWithSucc(m) {
return function(n) {
return m(succ)(n);
}
}
![Page 30: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/30.jpg)
Holy crap http://uxul.wordpress.com/2009/03/02/generating-church-numbers-with-javascript/
![Page 31: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/31.jpg)
Recursion Local variables are the devil! Also, a paradox In lambda calculus, you cannot define a
function that includes itself i.e. the definition of recursion
![Page 32: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/32.jpg)
The Y Combinator Cool name Also known as “The Paradoxical Operator”
![Page 33: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/33.jpg)
Recursion
To properly define recursion, a recursive function g must take as an argument a function f, which expands to g which takes an argument f.
![Page 34: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/34.jpg)
Got that?
![Page 35: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/35.jpg)
Recursion f = g(f) f is a fixed point of g Known as the Y-Combinator
![Page 36: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/36.jpg)
The Y-Combinator
Y = λ g . (λ x . g (x x)) (λ x . g (x x))
![Page 37: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/37.jpg)
The Y-Combinator in JavaScriptfunction Y(g) { return function(x) { return g( function(y) { return x(x)(y); } ); }( function(x) { return g( function(y) { return x(x)(y); } ); } );}
http://matt.might.net/articles/implementation-of-recursive-fixed-point-y-combinator-in-javascript-for-memoization/
![Page 38: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/38.jpg)
Functional Factorial
Y factorial n
• Y : definition of Y-Combinator• factorial: functional definition of factorial• n : integer
![Page 39: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/39.jpg)
Functional Factorialfunction factorial(f) { return function(n) {
return (n == 0) ? 1 : n * f(n – 1);
}
}
//call like so:
alert(Y(factorial)(5)); //alerts 120
![Page 40: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/40.jpg)
What is happening? Recursive calls are abstracted to an inner
function, which is evaluated as a lambda function (which by very definition are not recursive, remember?)
Interesting, but not all that useful…
![Page 41: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/41.jpg)
![Page 42: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/42.jpg)
Other topics… Currying Mapping Reduction Substitution Elaboration on statelessness Imperative vs. Functional Performance
![Page 43: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/43.jpg)
![Page 44: Introduction to Functional Programming in JavaScript](https://reader033.vdocuments.site/reader033/viewer/2022061201/5479d9b8b4af9faf158b4927/html5/thumbnails/44.jpg)
Questions…