Swift - You do Functional Programming but you don't Know ??

Swift - You do Functional Programming but you don't Know ??

You heard about functional programming either in an interview or from a geek friend or in some talk somewhere and because you are a good developer, you added "search about functional programming." in your To-Do list ??

If you are with Swift in a good relationship, I am here to prove that you do functional programming but you do not know ??

What's it all about?

  • Breaking your code into small units, Functions.
  • Treat a function like an object that you can assign to a variable so that you can pass it to another function or return it from, Higher-Order Function.
  • It has no side-effects (does not change anything) outside of it and its output only depends on its input (mathematical function), Pure Function.
  • You encapsulate one function within another, Partial Function.
  • Some function calls itself as part of its body, Recursive Function.
That's it! 

If you apply these concepts as you work then "You do functional programming."

Let's get a little bit closer.

No alt text provided for this image

I need to show the geeks' names in alphabetic order.

No alt text provided for this image

I believe that a well Swift developer will develop an algorithm in an away that somehow looks like the one above otherwise, I think he needs to get his relationship with Swift closer!

Why this algorithm is considered as a functional programming solution?

Let's examine it ??

1. Modularity

Working with modularity is like playing with children’s building bricks. You have a box of simple bricks that you can use to build a large and complex system by joining them together. Each brick has a single job. (raywenderlich.com)

You have divided your code into chunks, each chunk has only one job to do!

let geeksNames = getGeeksNames(from: geeks, .ascending)

This function does only one thing: "get geeks names from geeks array in ascending order". The one thing is a statement that maybe some related steps ??

  1. Map geeks array into an array of names.
  2. Sort the mapped array based on the order selected by the user.

A simple test case for this function will be something like that:

No alt text provided for this image

This function is a testable, modular (handles a particular responsibility) and reusable ??

2. Treat a function like an object, Higher-Order function

As we mentioned earlier, the higher-order function is simply a function that operates on other functions by either taking a function as a parameter or returning a function. In our function, we have used two common higher-order functions:

  1. Map, map geeks array to an array of strings (names).
  2. Sorted, sort names array.

3. Pure Function

Our function is a pure function because:

  1. It does not change anything outside its scope (zero side-effects) ??
  2. It produces the same output for the given input, Y = f(X) ??

Final Shot ??

One of the best things caused by FP techniques is the declarative code style. The resulting code is declarative, meaning it’s a self-explanatory and reads like the problem statement it solves ??

No alt text provided for this image

In one line of code, you’ve told Swift what to do. Map the geeks array to an array of names then sort them so that the first element in the mapped array is less than the second one (ascending order) ??

Whenever your logic appears, do not forget to play with FP techniques. That makes your logic much testable ??

It's over! That was a fast walkthrough with you to prove that you do functional programming and if not, you should ??

I hope that helps you a little bit. See you geeks ????

要查看或添加评论,请登录

Essam Fahmy的更多文章

社区洞察

其他会员也浏览了