O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required