Source: Google Search

Using Higher order functions: A practical example in Swift

Jahid Hasan Polash
Feb 14, 2018 · 2 min read

Higher order functions are described to be pretty advanced in the sense of how it works and why it works. But seriously, who cares, right? Of course as long as you know how to use it. People might argue with me, but I think its more important to know where to use something than how to use it (Hint: IDEs do most of the things right?).

“Oh, Cut the crap! show us some code!”

Sure. I will show you a more practical example of where and how to use it. First accept this is valid.

let var = myFunction() -> Void

We will experiment more about that later. 😉 For now, lets hear a story.

Suppose, in a controller somewhere we have few functions calling network request with a authentication token named token . If the token is expired we don’t want to disturb the user. We try login automatically and try doing what we are doing. Now as we are calling the same tryLogin() function from several different functions, we need to keep track which one should we call back after fetching new authentication token. There are some ways to do it, we can choose any. Here is a demonstration how we can do that the easy way.

See how I used switch to determine which function to call in the tryLogin() function. The problem is that, as the number of calling functions increases that switch gets larger too. Also that tracking gets very massy if you want to modify functions (delete or reorder senderId). 😨

Now, I can show you a better approach where we will remove that dependancy from tryLogin() function. Of course using Higher Order Functions.

Here what I did, passed the function itself as a parameter of tryLogin() function instead of using a sender Id.

The argument parameter tryLogin(callingFunction:) callingFunction itself is the function we sent. So calling it will do the callback we deserve.

Happy now? 😎

Well, if you are not satisfied, i am guessing you are more of a traditional learner. Thats ok. Here is a video tutorial that helped me a lot understanding the concept.

Remember the line of code I said is valid? Hope you will find some connection between the implementation code snippet and the statement. Well we can do some awesome stuff with that (practically 😄). But maybe later.

If you like the article give some claps. If you don’t, please don’t hate me.

Happy Coding… :)

InfancyIT

InfancyIT

InfancyIT is a software company providing consulting and software solution services. We help our clients to grow their respective businesses by automating their daily tasks.

Jahid Hasan Polash

Written by

Loves everything fashionable yet simple.

InfancyIT

InfancyIT is a software company providing consulting and software solution services. We help our clients to grow their respective businesses by automating their daily tasks.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store