introduction to functional programming in javascript

Post on 28-Nov-2014

14.323 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

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

Functional Programming

Tommy “is awesome” Montgomery

2009-03-06

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

Elements of functional languages Recursion Functions (der) No state

Functional Languages Haskell Erlang F# OCaml Scheme Smalltalk

J K Mathematica XSLT LISP (kinda)

Fun Fact #1 Tommy was once a professional musician

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

Example

λ x . x + 2

Example

λ x . x + 2

input

Example

λ x . x + 2

input return value

Example

λ x . x + 2

input return value

f(x) = x + 2

Higher order functions Functions that take functions as arguments and return

functions Where the real power of functional programming lies

Higher order function example Mathematical derivative

xxdx

d22

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; }}

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

Fun Fact #2 Tommy used to be a gangsta

Bound variables The λ operator binds

its variables to its scope

All other variables are “free”

Freedom

Bound variables

λ x . x + y

Bound variables

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

Bound variables

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

f(x) = x + y

Natural numbers Everything is a function, remember? How do we define the natural numbers in

functional programming (1, 2, 3, …)?

very painfully

Church numerals 0 ≡ λ f . λ x . x 1 ≡ λ f . λ x . f x 2 ≡ λ f . λ x . f (f x) 3 ≡ λ f . λ x . f (f (f x)) ...

Church numerals in JavaScript//identity function// λ x . xfunction identity(x) { return x;}

//Church numeral zero// λ f . λ x . xfunction zero(f) { return identity;}

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)); } }}

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;}

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;}

Fun fact #3 Wombats are unfairly cute

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)); } } }}

Addition using successor// λ m . λ n . m succ n

function addWithSucc(m) {

return function(n) {

return m(succ)(n);

}

}

Holy crap http://uxul.wordpress.com/2009/03/02/generating-church-numbers-with-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

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

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.

Got that?

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

The Y-Combinator

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

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/

Functional Factorial

Y factorial n

• Y : definition of Y-Combinator• factorial: functional definition of factorial• n : integer

Functional Factorialfunction factorial(f) { return function(n) {

return (n == 0) ? 1 : n * f(n – 1);

}

}

//call like so:

alert(Y(factorial)(5)); //alerts 120

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…

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

Questions…

top related