What is functional programming?

We know functional programming matters, but what is it really? There is a lot of hype related to functional programming and there are a lot of definitions about it, but simply it is a style of programming that models computations as the evaluation of expressions. Functional programming is a declarative programming style, as opposed to OOP that is categorized as imperative programming.

Theoretically, functional programming employs the concepts of category theory, which is a branch of mathematics. It is not necessary to know the category theory to be able to program functionally but studying it will help us grasp some of the more advanced concepts such as functorsapplicative functors, and monads. We will get into category theory and its relationship with functional programming later, so for now we are not going to talk math and we will scratch the surface of functional programming pragmatically.

Let's start with an example to understand the differences between functional programming and OOP styles. The following example gives two different approaches to array element multiplication:

let numbers = [9, 29, 19, 79]

// Imperative example
var tripledNumbers:[Int] = []
for number in numbers {
    tripledNumbers.append(number * 3)
}
print(tripledNumbers)

// Declarative example
let tripledIntNumbers = numbers.map({ number in 3 * number })
print(tripledIntNumbers)

In the imperative example, we give a command to go through each item in the array, multiply each item by 3, and add it to a new array. In the declarative example, we declare how numbers should be mapped. We will have more examples of declarative programming in upcoming chapters.

In functional programming, functions are the fundamental building blocks. In OOP, programs are composed of classes and statements, which change the state of classes when executed.

Functional programming avoids using mutable states. Avoiding mutable states makes it easier to test, read, and understand the code although it is not easy to avoid mutable states in some cases such as file and database operations.

Functional programming requires functions to be first-class. First-class functions are treated like any other values and can be passed to other functions or returned as a result of a function.

Functions can be formed as higher-order functions that take other functions as their arguments. Higher-order functions are used to refactor code and reduce the amount of repetition. Higher-order functions can be used to implement domain-specific languages (DSL).

Functions are pure so they do not depend on any data outside of themselves and do not change any data outside of themselves. Pure functions provide the same result each time that they are executed. This property of pure functions is called referential transparency and makes it possible to conduct equational reasoning on the code.

In functional programming, expressions can be evaluated lazily. For instance, in the following code example, only the first element in the array is evaluated:

let oneToFour = [1, 2, 3, 4]
let firstNumber = oneToFour.lazy.map({ $0 * 3}).first!
print(firstNumber) // The result is going to be 3

The lazy keyword is used to get a lazy version of the collection in this example so only the first item in the array is multiplied by 3 and the rest of the items are not mapped.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset