Monday, January 20, 2014

What JavaScript Can Teach Us About Functional Programming

"Follow your function, go and batten on"
-- Shakespeare, Coriolanus

What is functional programming?  

What is functional programming, looking online we find a few differing definitions:

"Functional programming is programming without assignment statements."
-- Uncle Bob, FP Basics E1

"[Functional Programming,] treats computation as the evaluation of mathematical functions and avoids state and mutable data."
-- Wikipedia, Functional Programming

"Functional programming is a style of programming which models computations as the evaluation of expressions."
-- HaskellWiki, Functional Programming

"Functional Programming is when functions, not objects or procedures, are the fundamental building blocks of a program."

The definitions above ascribe the following attributes to functional programming:
  • no mutations
  • no states
  • computations are done by functions
What does this mean to me?

"Functional Programming, is programming with functions as first class citizens which do not expose state outside of the function."
-- me, right here

I believe a few examples would be nice right about now and since I feel rebellious I'll see what JavaScript can tell us about functional programming.

Examples of JavaScript being used in a functional style

Let's fire up Node.js.

C:\Documents and Settings\Mike Harris>node
> var x = 5, f = function(x) { return x + 2; };
> console.log(x);
> console.log(f);

Looking above we see that we can pass the function f along like we do with the variable x.

> var strangeAdd = function(f, x) { return f(x) + x; };
> strangeAdd(f, x);
> strangeAdd(function() { return 42; }, 1);

Moving on, we see that we can create a function called strangeAdd which takes a function and a value as its arguments and adds the two arguments together passing the value as an argument to the function argument.  With the first call to strangeAdd we pass the already defined function f and the variable x giving the result of 12.  While in the second call to strangeAdd we pass in an anonymous function and the value 1 giving us the result of 43.

> var messAround = function(x) { x = "Hello"; console.log(x); };
> messAround(x); console.log("Global x = " + x);
Global x = 5

We see that in JavaScript we can mutate the parameter being passed in without worrying about changing the value of the parameter outside of the function.

> var closureAdd = function(c) { return function(x) { return c + x; }; };
> var add5 = closureAdd(5);
> add5(2);
> var helloer = closureAdd("Hello ");
> helloer("Mike Harris");
'Hello Mike Harris'

How about encapsulation?  In functional programming to encapsulate a value we use a closure.  A closure wraps a value passed into a function which returns another function.  We see that when we call closureAdd with a 5 we get a function which adds 5 to whatever value is passed in.  Next, we use the concatenation functionality of the + function in JavaScript and create a function which says Hello to whatever is passed in.

I believe this demonstrates what we ascribed to functional programming.

Closing thoughts on functional programming with JavaScript

Do not get me wrong JavaScript is no Haskell, but it does allow for functional style of programming.  In fact with Bilby you get much of what function programmers rave about.

"[L]essons from functional programming languages like Clojure and Haskell that are directly applicable to JavaScript"