1.0 - Introduction

Want to learn the basic concepts of functional programming on simple and straightforward examples? This book is your trusted guide through all the new ideas you need to grasp as a beginner in the world of pure functions, closures, immutability, idempotence and other more or less obscure topics. The purpose of this book is to explain the main concepts in functional programming as simply as possible in a language agnostic manner....

2.0 - Programming paradigms

The popular wisdom says that programming is like giving directions to an idiot. It is a rather handy analogy, and you can use to explain to your grandparents what you do for a living. However, it doesn’t quite hit the mark. I mean, even the dumbest person you could possibly imagine is still orders and orders of magnitude more complex than all your computers put together. So, it is not really like that....

2.1 - Imperative programming

Let’s say we need a cup of coffee. What we have at our disposal are one water kettle, one mini mixer, an empty cup and a teaspoon. We also have some coffee granules, sugar, water and milk. What we want as the outcome of the exercise is a cup of coffee and this is one way we can get it: Make coffee 1. Put a teaspoon of sugar into the cup 2....

2.2 - Declarative programming

Again, let’s say we need a cup of coffee. But, this time we just walk into the nearest coffee shop and say: One coffee with 1 teaspoon of sugar and milk, please! And there’s your coffee - no fuss over anything. You just declare the desired outcome and let the baristas take care of it. The main benefit of this approach is a very high level of abstraction. Practically all the details we were so careful about in imperative programming are of no consequence anymore....

2.3 Functional programming

Liked the taste of declarative programming? Want to generalize it and write arbitrary code in a declarative fashion, not just HTML pages, database queries or overpriced coffee? Let’s see what we can learn from all the important concepts employed by declarative programming and the evolution of imperative styles. Dependence on global state is bad. We should avoid affecting the global state as much as possible. If we don’t have any mutable state, the order of our code won’t matter....

3.0 - Theory

The very central concept in functional programming is an expression. Expression is a piece of code that can be evaluated, which is to say that it can be used to produce a value. In functional programming, everything is an expression and everything produces or returns a value. There are two types of expressions: plain values function calls Plain values are the elementary expressions, e.g. in JavaScript. 2 true "hello" {sugar: 2, coffee: 1, water: 0....

3.1 - Pure functions

We’ll start with the most commonly used definition of a pure function, then we’ll provide a few examples of pure and impure functions, with different causes of impurity. Then, we will try to illustrate function purity from a few different angles. Anyway, let’s start with the definition. Pure function is a function where the return value is: only determined by its input values, without observable side effects. If we continue with the pipe analogy, this means that if anything was to enter the pipe must do so at the entrance, as a parameter, and the only thing that leaves does so at the other end, as the return value....

3.2 - Well-behaved imperative procedures

Now that we’ve mastered the concept of pure functions, we’re going to deconstruct it and show how function purity is really just an illusion. However, one that does provide a very useful abstraction that helps us write cleaner and more manageable code. Let’s use a very simple pure function. function add(a, b){ return a + b; } As we’ve seen in the previous chapter, this JavaScript function is pure - its behavior depends only on its parameters and there are no side effects....

3.3 - Inherent impurities

No matter how hard we try, some impurities will always exist. We can abstract them away, ignore them or make sure that they won’t affect normal program usage, but we can’t ever get rid of them. The two main sources of such impurities are: Hardware - because all programs run on actual devices. Time - because everything happens is trapped in the one-directional flow of time. Hardware caused impurities are caused by physical constraints of the devices on which we run our programs....

3.4 - Intentional impurities

Ok, we get it - pure functions are good, side effects and mutable state are not. If we all only ever write pure code we’ll easily develop top quality code and live long and prosper. Well, actually… there’s a catch. We actually need both side effects and the global state. You know, like, to get stuff done! This is just a brief list of some impure things a typical software system needs to do:...