Higher order functions (Filter, Map, Reduce) [고차함수란?]

Doyeon
doyeona
Published in
3 min readDec 22, 2020

Higher order functions are the most famous and most commonly used in swift. It’s basically a function but with a simpler way of function.

Filter

Filter is used when you want to have a result with matched condition

let numbers = [1, 2, 3, 4, 3, 3]
let filtered = numbers.filter({return $0 == 3})

As you can see from the code above, the condition is checking if the numbers array’s element is equal to 3 then return the value into filtered constant.

when you run the code, the output will be:

[3, 3, 3]

$0 means the first parameter passed into the closure [shorten version of writing closure]. You can also skip the return keyword as well if there’s only single code in the closure or method.

Map

Map is used when you want to apply the same operation to each element of a collection.

let sumElements = [1, 2, 3, 4].map({return $0 +3})

It’s the same format as the filter function above but the difference is that applying the same operation to each of the elements which is adding 3 to the value stored in the array. Therefore the output will be 👇🏻

[4, 5, 6, 7]

Reduce

Reduce is used when you want to combine all the elements in a collection into one value.

//version1
let sumAllElements = [1, 2, 3, 4].reduce(0, {sum, number in
sum + number})
//version2
let sum = [1, 2, 3, 4].reduce(0, {$0 + $1})
//version3
let sum = [1, 2, 3, 4].reduce(0,+)

Above code is to add all the elements and return to the constant sumAllElements. but the little difference from both map and filter function is it has an initial value “0” to know where to start.

output for all the versions will be 👇🏻

10

Okay, You got to know a little bit of those but my question is when to use them and how to apply them in real coding?

I brought a simple example using a higher order function to give you an idea of when to use it.

class Person {
let name: String
let isStudent: Bool
init(name: String, isStudent: Bool) {
self.name = name
self.isStudent = isStudent
}
}
var array = [
Person(name: "john", isStudent: true),
Person(name: "jessica", isStudent: true),
Person(name: "annie", isStudent: false),
Person(name: "iris", isStudent: true),
Person(name: "valerie", isStudent: false),
Person(name: "chris", isStudent: true),
Person(name: "christine", isStudent: false)
]

I have a class named Person and has two constants which are name and isStudent to check if that person is a student or not. so in the array variable, it’s initializing multiple values. so,, here’s my questions are

  1. How to get person who are student ONLY
  2. How to print out all the names who are student

probably without using high order functions, you can use a for loop to check if each of the elements is a student. however, if you want to make the code shorter and more convenient way to improve it, simply you can use filter function to filtering out the element if FirstPassedParameter($0).isStudent is true

array = array.filter({ return $0.isStudent})

what about #2 questions? You can simply solve this using map function.

let nameOfStudents = array.map({ $0.name})print("\(nameOfStudents)")

I know using higher order functions is still hard to implement in real life projects and this might require some practice and studies on closure, recursion and many other basic concepts. but since you already know at least what higher order functions are, you can try to implement it to your code and try to make your code useful! I will try to add and share more information about it once i get better soon 👏🏻

--

--

Doyeon
doyeona

Passionate iOS developer creating extraordinary apps. Innovative, elegant, and collaborative. Constantly pushing boundaries.Let's build the future together📱✨🚀