Bharti Sanjeev
3 min readJun 14, 2021

--

map, flat map and compact map in swift

Oh Boy, here I go. I ever wanted to write blogs about technical stuff which I know but always gave excuse like let’s start from tomorrow(which never happened). Enough about the excuses let’s dive into the topic. map is a higher order function like filter and reduce. As per wiki

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

It might not seem like it at first, but a lot of the code that we write as app developers is about transforming data and values into different shapes and forms. For example, we might transform a URL into a piece of data by performing a network request, and then transform that data into an array of models, which we then finally transform into a list-based UI.

One way of performing such value transformations is by mapping a collection of values into an array of new values, using a transform. The Swift standard library offers three main APIs for that kind of mapping map, flat map and compact map. Let’s get the balls rolling

Let’s print the tables using map. Below is the list of 10 elements in array and we want a new array with multiple of some number

let list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Here is a function which takes the Integer you want to multiply each number and returns new array with all the changes.

func multiplyEachNumber(by number: Int) -> [Int] {

return list.map { $0 * number }

}

Now we can easily print table of any number starting from 1–10

let tableOf2 = multiplyEachNumber(by: 2) //prints: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

let tableOf5 = multiplyEachNumber(by: 2) //prints: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

This is simple example to explain how map can easily transform an array to desired output.

Here is another example where we are converting Integer list to String list. We are using the above declared integer’s list. Below is the function which converts each int elements to string

func stringsVersion(of numberList: [Int]) -> [String] {

return numberList.map { “\($0)”}

}

flatMap:- as the name suggest it flattens the collections into one colleciton. For example we have multiple lists and we want to perform some operation to all of the elements and return back the formatted collection.

let lists = [

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],

[11, 12, 13, 14, 15, 16, 17, 18, 19, 20],

[21, 22, 23, 24, 25, 26, 27, 28, 29, 30],

[31, 32, 33, 34, 35, 36, 37, 38, 39, 40]

]

This is an array of array’s and let’s say I want to double each value in collections and want to populate them in single list. flatMap are perfect for such kind of situations. Below is the function which takes two params one is number(which user want to multiply) and other is the list(to which the operation is to be performed)

func transformLists(by number: Int) -> [[nt] {

return lists.flatMap {

multiply(each: number, elements: $0)

}

}

Adding to that I am writing method which is implemented above but this time it is taking list as a param. The operation will be performed on elements of this list.

func multiply(each number: Int, elements: [Int]) -> [Int] {

return elements.map { $0 * number }

}

print(“flat list of elements: \(flatList)”)

Output: [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152, 156, 160]

This is one dimensional list of all the elements which are multiplied by 4

compactMap: Compact map removes the nil values from the collection if any. for example

let values = [“test1”, “test2”, “test3”, nil, “test5”, nil, nil, “test8”]

let newValues = values.compactMap {$0}

print(“\(newValues)”)

output: [“test1”, “test2”, “test3”, “test5”, “test8”]

--

--