What is functional programming?

Functional programming is a programming paradigm, a way in which computer programs and languages are structured. If you've ever taken any math classes, functional programming can be seen as programming with mathematical functions. The output of a function f(x) is based only on the arguments that are passed into the function, there are NO side effects. This simplifies code by eliminating any outputs other than those that depend on function inputs, simplified code is often shorter and usually easier to understand because behavior is more predictable.

What languages are functional?

If you are looking to pick up a functional programming language for whatever reason, you are in luck! Some of the more prominent functional languages include Haskell, Lisp, Scheme, Clojure, OCaml, and Erlang. Notice that I did not include JavaScript in the aforementioned list, that is because JS is not a purely functional language (neither is Scala even though it is usually written in a heavily functional way).

Why use functional style in an imperative language?

As I mentioned above, JavaScript and many other languages that may be written functionally (Scala comes to mind) are double-dipping in programming paradigms. So why use functional style?

  1. Using functional style is popular! If you were to take a look at the top-three most depended on packages with npm, you would Underscore by Josh Ashkenas at #1 and Lodash by John-David Dalton at #3. These two libraries include functional methods like _.each and _.reduce.
  2. Using functional style can teach you important things about languages like JS! There is a reason that bootcamps like Hack Reactor have you thoroughly understand libraries like Underscore and dive into actual source code. Some of the things that are reinforced are the use of callbacks, closure scope, and the concept of first-class and higher-order functions.
  3. So fresh and so clean, clean! Different strokes for different folks, but you can cut out a lot of repetitive code in basic for loops by using forEach or another similar function. This makes code concise and readable while also eliminating the chance to introduce errors into your code. Have you ever had an off-by-one error while iterating over something? I have.
  4. Recursion. While this may be addressed in a previous point, I'm giving recursion its own list point because it is a huge deal. Imperative languages lean heavily on for loops to iterate through things and perform manipulations. While loops are completely reasonable and even preferred for strucures such as arrays, structures like trees lend themselves more easily to recursion. Obviously, skipping for loops is not the magic ticket to learning recursion, thinking in a more functional fashion will definitely accelerate the process.

Sneak-peek into functional JavaScript with curry

What is function currying you ask? Well, it is involves turning a function that takes multiple arguments into an equivalent function that takes few arguments (the term arity is often used here to refer to the number of arguments a function can take). We can see a very simple example below written in JS,

var adorableThings = function (firstArg) {  
    return function (secondArg) {
        var result = firstArg.concat(" and ").concat(secondArg);

        return result;

var puppies = adorableThings('puppies');

puppies('kitties'); // 'puppies and kitties'  

Obviously this is a very contrived and simple implementation, but then again this is just a sneak-peek. Tune in later for more folks!


Hack Reactor , Javascript , Functional Programming , Recursion

About the author
comments powered by Disqus