When programmers first encounter functional programming, their reaction is often one of disbelief—that it can’t be possible to write nontrivial programs without modifying variables. We’ll see that it is not only possible but very often simpler and easier than creating normal imperative code.
Today we’re going to concentrate on parallelism. We’ll construct a simple functional program and then show how, because it’s functional, it’s almost trivially easy to parallelize.
But first let’s look at a couple of examples in Java that show why it’s so helpful to avoid mutable state.
Here’s a class that doesn’t have any mutable state and should therefore be perfectly ...