What is Functional Programming?

Functional programming is a programming paradigm that favors the use of functions to perform computation.
I came to it through Javascript — partly through React and Redux and partly through pure JS functions like map(), filter(), & reduce().  These functions are the common collection that remains after sifting through the paradigm for only the core and most necessary elements for practical use. But let’s go deeper…

Other languages exist that certainly utilize the paradigm more robustly, such as Haskell, Elm, Clojure, Lisp, Erlang, Elxir, Scala, and so on.  There exists a varying degree of ‘pureness’ as some of these languages are multiparadigm and some are strictly functional.

Key Concepts of Functional Programming

Functional programming relies on composing pure functions and avoiding shared state, mutable data and side effects.  It is expressed in a declarative style, rather than an imperative one.  Declarative can be seen as ‘what to do’, where as imperative can be seen as ‘how to do things’.

In Object Oriented programming, state is shared and colocated with methods inside objects. This is one of the key differences, because in functional programming, application state flows through pure functions.
Pure functions are functions with no side effects that you can rely on to always give you the same out put when given the same input.  They just perform their operation and that’s it.  Mutable, shared state can cause problems for your program as it gets more difficult to determine what is the latest state when it can be changed in multiple ways.

Function composition, as mentioned before, is exactly what it sounds like; composing functions together to create the program. Functions working to help functions.  FP favors this over imperative flow control like if…else and switch statements.  Using conditional expressions and recursion, FP can achieve the same results in a much more concise and reusable way.

Referential Transparency

This brings me to a concept called referential transparency.  Essentially, it refers to the feature of a pure function being able to be replaced with its resulting value without changing the meaning of the program.  This feature makes the function context independent, as it can be run anywhere and will always mean the same thing. Consider the while() loop.  No matter where you use it, it will always work the same way.

while ({condition is true}) {
// do something...

Higher Order Functions and Lazy Evaluation

Two key features of functional programming are Higher Order functions and Lazy Evaluation.

Higher Order Functions are functions that either takes a function as an argument, returns a function, or both.  They enable reusabilty and modularity.

Lazy Evaluation is the delaying of the evaluation of an expression until its value is needed.

What the Functor?

You might have looked up functional programming and found odd sounding terms like Monads and Functors.
The terminology is almost another language all on its own. But if we demystify it a bit, we might find that its more familiar than we originally thought.
I will just scratch the surface in hopes of connecting the dots to the map(), filter() and reduce() functions I mentioned at the outset and the depths of the mathematical underlayings of functional programming.

A Functor, for example, is something that can be mapped over. 

Kind of like this:

var array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

// expected output: Array [2, 8, 18, 32]

But hold up a minute! Its not the map() function thats the functor. It’s actually the array.
Functional programming terminology is probably alot of stuff you already know, but just haven’t referred to it that way.

For more, here are a few links:

You can find some great coverage of this material with Brian Lonsdorf’s book: Professor Frisby’s Mostly Adequate Guide to Functional Programming.

Eric Meijer also gives some great, impassioned explanations from the depths of the mathematical side of functional programming.  I believe there is also some great stuff of his up on Channel 9.

Eric Elliott’s notes on functional programming in JS have also been helpful to me.

%d bloggers like this: