#### Functional Programming in 5 Minutes

By G. Kay Lee

Perhaps you've been told by someone before that this is one of those "advanced JavaScript techniques" you'll need to learn to become a true JavaScript ninja.

But the truth is...

http://commons.wikimedia.org/wiki/File:Phanaeng_kai.jpg

Functional

Object-oriented

**has half of its root in it.**

**AKA Anonymous Function**

**originated from a mathematical system called**

**is about simplifying mathematical expressions into
**

**to unveil the true nature of underlying computations**

**Functions**

**...and can only have**

**To handle a**

**it needs to first go through the process of...**

http://commons.wikimedia.org/wiki/File:Curry_Ist.jpg

(1900 – 1982)

also immortalized as the name of

the popular pure functional language

http://en.wikipedia.org/wiki/File:Haskell-Logo.svg

So

can be curried into

Or, in classical notation

can be curried into

**a multi-argument function**

function(x, y) {

return x + y;

}

**when curried, will become**

**a chain of functions, each**

**with a single argument**

function(x) {

return function(y) {

return x + y;

};

}

**if we give them names**

function f(x, y) {

return x + y;

}

**then...**

function g(x) {

return function(y) {

return x + y;

};

}

f(1, 2) === g(1)(2);

**now, since functions are**

**first-class citizens...**

**we can also do it**

**like this**

function g(h) {

return function(y) {

return h(h(y));

};

}

81 === g(function(x) {

return x * x;

})(3);

function g(h) {

return function(y) {

return h(h(y));

};

}

**if we only supply**

**one of the arguments**

**to the curried function...**

var n = g(function(x) {

return x * x;

});

**we'll be able to reuse it**

**like this**

337 === n(3) + n(4);

**This is called**

**and is not the same with currying**

**actually, for functions with**

**an arity greater than 2**

function f(x, y, z) {

return x + y + z;

}

**the correct way to**

**partially apply is through**

**bind()**

var f1 = f.bind(null, 1);

**call to a partially applied**

**function returns the result,**

**not another function down**

**the currying chain**

6 === f1(2, 3);

**Now I guess we're
**

**
**

**
**

**so...**

that the reverse process of currying

is closely related with

a more familiar concept

**Nah, we're running outta time. Seriously.**

**This slide can be found on
**

By G. Kay Lee