During my school and university years, I was stupid enough to constantly repeat one, very common mistake. The mistake is to think, that there are stupid questions.. The bad thing is, that the “stupid” questions are the most important ones.
There are no stupid questions!
The article is not about questions. The article is about Functional Programming. Before writing it, I googled “functional programming swift” and the similar. Most of the articles on this topic create more questions than answers. Authors often skip important definitions and principles, and starts directly from
.reduce tutorial. Skipping the concept steals important building blocks, which leads readers knowledge house to be unstable.
If a topic could not be explained in a freshman lecture, it was not yet fully understood.
The Functional programming definition is widely discussed over the Internet. After some research, I found the article, which I strongly recommend for reading. It contains different Functional programming definitions analysis.
As you see, it’s hard to give the “one true” definition. I want to analyze the Wikipedia one. I think it’s quite precise but needs some explanations.
Functional programming (FP) is a declarative programming paradigm, which means programming is done with expressions. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function
ftwice with the same value for an argument
xwill produce the same result
f(x)each time. Eliminating side effects, i.e. changes in state that do not depend on the function inputs, can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming.
Let’s decompose it into small blocks, and try to make them understandable.
Declarative programming paradigm
Saying with simple words, you use Declarative programming when you write your code in such a way that it describes what you want to do instead of how you want to do it. It’s up to the compiler to figure out the how.
The simplest example of declarative programming from iOS developers life is the Autolayout. Using the Autolayout you only declare rules, or, in other words, tell compiler What you want, e. g. the view A should always be top-left and should be equal width with view B. Compiler understands the rules, and creates instructions for views management. On the other hand, you can manage views frames by yourself, using Imperative approach.
Functions and Side effects
In mathematics, a function is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output.
In programming terms, the function is a self-contained chunk of code, which can accept arguments and return results. If the function has no side effects, the result of multiple evaluations with the same arguments will be the same.
We say Function has side effects, if it’s evaluation changes state outside of the function scope. Like the class instance property modification, modification of global variable, e. t. c. Function without side effects does not impact the global state. This is a huge benefit, because of program behavior predictability and optimization opportunities.
Sticking everything together
So, the Functional programming is a programming paradigm, when you tell compiler what to do, instead of writing exact instructions of how to do. For telling the compiler what to do, you use functions, which behavior is always predictable and does not change the global state.
Good news, we can use the functional approach in non-functional languages, including Objective-C and Swift.
This article does not cover the wide range of FP benefits, does not tell anything about techniques of functional programming. Just believe me, it was hard to keep the line inside of the definition frames. The purpose of this article (and all my other articles) is to understand the concept and discuss it with like-minded people.
If you have any suggestions, found the mistake or just want to talk - @limlab_io. Thank you for reading!