It’s About Functions

At its core, functional programming is about programming with pure, side-effect-free functions.

This is a pure function:

 f(x) = <raise power=​"2"​>x</raise>

So is this:

 public​ ​int​ incrementCounter(​int​ counter) {
 return​ counter++;
 }

This is not:

 private​ ​int​ counter = 0;
 public​ ​void​ incrementMutableCounter() {
  counter++;
 }

The first two examples increment a counter by returning a new integer that’s one higher than the passed-in integer. The third example does the same thing but does it by mutating a bit of state that may be shared across many pieces of a program.

A definition: a function like incrementCounter that doesn’t rely on mutating state is called a pure function. And this purity ...

Get Functional Programming: A PragPub Anthology now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.