introduction to functional programming in javascript

44
Functional Programming Tommy “is awesome” Montgomery 2009-03-06

Upload: tmont

Post on 28-Nov-2014

14.322 views

Category:

Technology


3 download

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

Functional Programming

Tommy “is awesome” Montgomery

2009-03-06

Page 2: Introduction to Functional Programming in JavaScript

What is functional programming? Uses functions Lambda Calculus Very academic Kinda goofy

Page 3: Introduction to Functional Programming in JavaScript

Elements of functional languages Recursion Functions (der) No state

Page 4: Introduction to Functional Programming in JavaScript

Functional Languages Haskell Erlang F# OCaml Scheme Smalltalk

J K Mathematica XSLT LISP (kinda)

Page 5: Introduction to Functional Programming in JavaScript

Fun Fact #1 Tommy was once a professional musician

Page 6: Introduction to Functional Programming in JavaScript

λ-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

Example

λ x . x + 2

Page 8: Introduction to Functional Programming in JavaScript

Example

λ x . x + 2

input

Page 9: Introduction to Functional Programming in JavaScript

Example

λ x . x + 2

input return value

Page 10: Introduction to Functional Programming in JavaScript

Example

λ x . x + 2

input return value

f(x) = x + 2

Page 11: Introduction to Functional Programming in JavaScript

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

Higher order function example Mathematical derivative

xxdx

d22

Page 13: Introduction to Functional Programming in JavaScript

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

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

Fun Fact #2 Tommy used to be a gangsta

Page 16: Introduction to Functional Programming in JavaScript

Bound variables The λ operator binds

its variables to its scope

All other variables are “free”

Freedom

Page 17: Introduction to Functional Programming in JavaScript

Bound variables

λ x . x + y

Page 18: Introduction to Functional Programming in JavaScript

Bound variables

λ x . x + y bound variable bound variable free variable

Page 19: Introduction to Functional Programming in JavaScript

Bound variables

λ x . x + y bound variable bound variable free variable

f(x) = x + y

Page 20: Introduction to Functional Programming in JavaScript

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

very painfully

Page 22: Introduction to Functional Programming in JavaScript

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

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

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

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

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

Fun fact #3 Wombats are unfairly cute

Page 28: Introduction to Functional Programming in JavaScript

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

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

Holy crap http://uxul.wordpress.com/2009/03/02/generating-church-numbers-with-javascript/

Page 31: Introduction to Functional Programming in JavaScript

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

The Y Combinator Cool name Also known as “The Paradoxical Operator”

Page 33: Introduction to Functional Programming in JavaScript

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

Got that?

Page 35: Introduction to Functional Programming in JavaScript

Recursion f = g(f) f is a fixed point of g Known as the Y-Combinator

Page 36: Introduction to Functional Programming in JavaScript

The Y-Combinator

Y = λ g . (λ x . g (x x)) (λ x . g (x x))

Page 37: Introduction to Functional Programming in JavaScript

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

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

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

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
Page 42: Introduction to Functional Programming in JavaScript

Other topics… Currying Mapping Reduction Substitution Elaboration on statelessness Imperative vs. Functional Performance

Page 43: Introduction to Functional Programming in JavaScript
Page 44: Introduction to Functional Programming in JavaScript

Questions…