Understanding Higher Order Functions in Swift

Updated on October 6th, 2020

⏱ Reading Time: 2 mins

Welcome to a new and interesting enough programming tutorial! Swift is a rich-featured language and it couldn’t be missing a great feature such as the higher order functions.

According to Wikipedia:

In mathematics and computer science, a higher-order function is a function that does at least one of the following:

– takes one or more functions as arguments (i.e. procedural parameters),

– returns a function as its result.

All other functions are first-order functions.

From the practical point of view in real world programming, higher order functions consist all together of a great tool that we can only have benefits from; we can avoid implementing functionalities that are already available in Swift, and we can achieve more with less, meaning that we can get to the same results with fewer lines of code. Higher order functions are applied usually in collections (arrays, dictionaries, sets) and as you will see in the upcoming parts, their purpose is to act on the contained elements in various ways. Even though they’re called “functions”, they’re accessed as methods using the dot syntax through the collection object that they’re going to be used with.

If there is one downside in higher order functions, then that is the bit unusual way to write them comparing to other more “Swifty” code. But being uncomfortable with them is not a reason for not making your life easier. It’s all just a matter of habit and training. If higher order functions are not included in your programming repertoire, then please try to start using them; they’ll become an everyday tool that will speed up your coding tasks.

Higher order functions are based on closures, but you don’t have to be a closures ninja in order to understand what comes next. However, just in case you need to refresh your memory or to just read a little bit about them, then you’d better take a look at Apple’s documentation before you get started here. What makes higher order functions great is that they can be written in really short ways, and as a result we can manage to write in just one line what we would normally have written in five or six lines. Shorthand arguments such as $0, $1 make that possible, and we’re going to meet them right next (they’re presented in the previous link as well); no need for further discussion about them now.

In this tutorial I’m going to present the most famous and most commonly used higher order functions in Swift:

  • map
  • compactMap
  • flatMap
  • filter
  • reduce
  • forEach
  • contains
  • removeAll
  • sorted
  • split

By the end of this post you’ll have learnt all the necessary stuff in order to start using higher order functions in your projects. So, don’t wait any longer, just dive in to get started!

→ Keep Reading at AppCoda

Stay Up To Date

Subscribe to my newsletter and get notifiied instantly when I post something new on SerialCoder.dev.

    We respect your privacy. Unsubscribe at any time.