Swift High Order Functions

emre gürses
4 min readJun 29, 2021

I talked more on my vlog please clicked.

Sort, Sorted(by:), Filter, Map, Reduce, flatMap, compactMap functions called high order functions. This functions calculate massive transaction in a simple line. In term of:

Higher order functions are simply functions that operate on other functions by either taking a function as an argument, or returning a function.

We talked about Sort and sorted(by:) function before. You can read from this link.Closure-2.

Swift array types have some methods which use closures to allow us to pass in functionality that can then determine how we want the method to sort, map, filter, or reduce an array of objects.

Filter: take one parameter and return an array. Also It can pass your filter specified in your closure.

For instance, we have array that contains integers numbers. We want to filter this array only will contain even numbers. We can use filter functions for this purpose. Also I will show you 3 different usage for filter function.

With return keyword.

If we have only one line statement, we can omit return keyword in closure. Look at the below code snippet.

Omit return keyword.

Inline syntax : We can use default parameters as a $0,$1 instead of item. the name of this short usage is inline syntax.

Inline syntax

As a result, we got same result with above 3 different usage. Generally, we prefer the third one. It will seem a little strange when you see it for the first time. But in time you will get used to it

Map: take one parameter and return an array. Also It can pass your filter specified in your closure. Map method iterate all items and return them new array one. Assume that, we have an array consist of integer numbers. We want to double these number with map function.

Inline syntax

Assuming we have an array of numbers, and we want to convert this number to string in array.

Inline syntax

Reduce: The reduce function accept one initial value and one closure. It allows you to combine all the elements in an array and return an object of any type.

Assuming we have an array of numbers from 1 to 10, we want to sum of value all array with reduce function.

Initial value is Zore, sum all numbers
Inline syntax
return concat string

We can use reduce function on dictionary. Let’s go deep with below code snippet. Our initial value 2, and we sum all value. Result is 2 + 10 + 7000 = 7012.

Return value
Return key

FlatMap: Flatmap is used to flatten a collection of collections.

Returns an array containing the concatenated results of calling the given transformation with each element of this sequence

chaning flatMap with filter function

CompactMap: Compact map return non-nil value from an array.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

emre gürses
emre gürses

Written by emre gürses

Denizbank — Intertech, Mobil Uygulama Geliştiricisi(iOS)

No responses yet

Write a response