Wednesday, May 9, 2012

Functional Javascript

There's been a bit of buzz about Functional javascript lately. It's easy to see why as javascript is such a flexible language that you can use it in both the functional and Imperative style. When I talk about functional style I'll mostly be referencing a style called point-free.

I'm a big advocate of the Closure Library and believe it will port well over to the functional style - even though it hasn't got all the right tools... yet.

So just what is the difference? Well Functional style is made up of functions (thus the name) and not objects. An object is something that has state (this "this" variables) and this is the enemy of the pure function. A pure function is what you strive for in a functional language and it's a pretty simple thing. A pure function is a function that when given an input will always return the same output for that input with no side effects. e.g:

// Pure function:
function pure(x) {return x;}

// Not a pure function:
var a = 0;
function impure(x) {return x+a};

now these functions are similar... until someone changes a. even if you change a (a = x) it is impure because now it has a side-effect (a is changed).

So why do we like pure functions? several reasons:


  • Pure function outputs can be memorized (as the same input gives the same output)
  • Pure functions are easy to test (no having to text different states)
  • Pure function allows code to run in parallel


Unfortunately the running in parallel doesn't really help us yet, but eventually javascript engines may be able to realize when a function is pure and run it. For example take the .map() function that runs a function on each element. If we know that function is pure we know that each function will not change the output on the next variable - so each map function can safely be run in parallel. If that function changes state however and the function uses that state for the output then we have to run them sequentially as the function needs to know what the state has changed to.

Right so pure functions are good, why can't we use them in the imperial style then? Well the fact is you probably already are. things like .map() .filter() and other new array functions come from functional style. Concepts also like partial application and currying are also from functional style. So we're already starting to use some concepts today.

So is there any more cool stuff we can use? Sure. Plenty. Function composition. In fact underscore has a nice little function that allows you to use it. What function composition allows you to do is chain together functions to create a single function you can use.

I hope that has whetted your appetite a little. I'll probably go over some more in later posts but for now there is more you can do learn. First check out this talk and visit the links. Teach yourself Haskell with a good book and stay tuned.

4 comments:

  1. Thanks for sharing ! I am starting a course in CS and the first module is on functional programming in javascript. What is interesting though is that this module was previously taught in scheme and now has been suddenly switched to javascript. Which kept me wondering why till I read your article.

    ReplyDelete
  2. I think you meant memoize instead of memorize in your bullet list.

    http://en.wikipedia.org/wiki/Memoization

    ReplyDelete
  3. JavaScript IDE Training JavaScript Training Courses JavaScript Courses teaches JavaScript IDE JavaScript Training JavaScript Training in Chennai Javascript Training in Chennai

    ReplyDelete