Let’s Talk Functional — A Problem Solving Approach
Let’s be honest, Functional programming is coming. If you are a Game of Thrones’ fan, you can say “winter is coming”. If you are not, just ignore that sentence; you are part of the 1% of the population that haven’t watched Game of Throne. It’s ok. (This is not a fact).
Functional programming is not a new thing, it came from lambda calculus in the 1930s. Yes, it’s even older than the C programming language! But why its popularity is ascending in the last couple of years? Programmers started to realize that they can solve problems easily in a Functional approach. I have to warn you; if you are an OOP person, like me, hang in there, don’t ignore this because FP (Functional Programming) is coming at you and it will only give you two options; learn it or quit.
We are using the Swift Programming Language.
Hopefully you are used to this code (see comments for explanation):
In the code above, we are simply taking an array of integers and multiplying its value by two. Something very simply, we are just mutating the elements in the array and multiplying them by two.
Now the FP approach:
If you compared the differences, you will see that we completed the same operation very easy using a lambda, also called closures. Ok, wait a minute, what am I talking about? This is basically the power of FP. Lambdas and closures share one concept, they are functions. Lambdas are anonymous functions, that is a function that it’s not bind to an identifier “a function defined with no name”. While a closure is a function that closes resources in an environment.
You can see that the value 5, passed when making the adder, is bundle in the adder. With these ideas, we can produce function during runtime, we will see more of this below.
In FP, functions are first-class citizen, which basically means that functions can be treated as a type/object. That is a function can be passed as an argument to another function, can be returned from a function, modified and assigned/bind to a variable (identifier). In Swift, a function type is declared and bind to an identifier as follow:
Here we did a little bit more than just declaring the function, we also initialized it on the spot. But it doesn’t stop there, check this out:
Here we defined ‘report’ which is a function that takes an array of integers and a function of type ‘(Int) -> Void’. Yes a function is a type/object. Our report function doesn’t really know what the passed function does, he is like “hopefully it prints something”. Now, when we call the report function, we pass our ‘numbers’ array and a closure. The closure takes a value and prints it. Note that this closure is anonymous because it is no binded to an identifier. As you can see, this give you the power to dynamically change the way you report your numbers on the fly. In case you are wondering if you can pass a defined function to ‘report’, yes you can. Here:
Hopefully, you can see that ‘report’ is not limited to only one way of printing the array. As long as the printer function agree to take an integer and returns nothing (void), the report function is happy about.
I am relative new to FP and I intend to keep learning more. This is only the first part of, what I believe, will be a 4-parts series on Functional Programming. To recap, we discussed what lambdas/closures are and how to use it in Swift. It is good to know that the ideas learned here (in Swift) can be translated to any other programming languages. A loop is a loop in C, C++, Python, Java, etc. A lambda is a lambda else where. If you find any mistake in this post, please let me know. I am not perfect, nor I am trying to be 😅.