Canarys | IT Services

Blogs

Swift Mapping and Flat Mapping

Date:
Author:
Tags:
Share

In this blog post, I will introduce some of useful tips and techniques of using swift mapping and flat mapping, you might already experienced the basic usage of swift map and flat map but in this blog we can see some additional benefits and areas where we can use the map and flat map to gain full advantage of swift higher order function efficiently.

let’s start with a very basic example code implemented in old way  and  new way of coding to accomplish certain task.

Well, lets gets started.

Swift Mapping:

Map is useful to apply common operation to all of its elements in an array or dictionary.

In the old way ,we’d have to initialize an array of empty Integer and loop over array of integer to apply logic.

Old way Example:

ScreenShot2018-01-04...

New way Example with Mapper:

ScreenShot2018-01-04...

Mapping with Dictionary:

so far we have seen example of arrays, let’s see how we can apply map function to dictionary.

For dictionaries , the first argument would be the key and second argument would be the value. Suppose if you want to retrieve all values of dictionaries as a array just map $1 , which returns array of values.

ScreenShot2018-01-04...

Mapping with Optionals:

Even we can perform mapping on optionals, Normally if we try to add any number to optional we end up in error saying like “value optional type Int? can’t be added directly” that means we can’t add number to integer without unwrapping it.

 

ScreenShot2018-01-04...

lets try same with mapper

ScreenShot2018-01-04...

So, with the help of mapper no need to unwrap the optional while performing addition operation.

That’s it about Mapper, lets start dig deeper into next high order function Flat Mapper.

Flat Mapping:

What’s Flat Mapping? let’s see what Apple say about this 

flatMap(_:)
Evaluates the given closure when this Optional instance is not nil, passing the unwrapped value as a parameter.

By the time i read the above line , i was like surprise

Later I understood , we need to use flat map when we need to transform the contents of an array of arrays, into a linear array.

still in confusion? lets look for one example

ScreenShot2018-01-05...

flatMap transform arrays of array [[1,2],[3,4],[5,6]] to [1,2,3,4,5,6].

You may wonder what is $0 here , it’s actually each array.while doing flat mapping some function/logic applied to each array later layer of nesting removed to return one single array as output.

suppose if you want to double each number in an array , you can try some thing like this

ScreenShot2018-01-05...

But This won’t work it will generate an error, like i mentioned above first argument $0 is an array, so you can’t multiply an array with integer.
To solve this we should use map with flat map .

ScreenShot2018-01-05...

FlatMapping with Optionals:

FlatMapping best suited for optionals. Suppose you have an array with 4 elements and 2 elements are nil. if you were to map over this you would get back an array that would give you 2 of its elements value is nil and other 2 elements are optional values . Now, if you were to do a flatMap on this, you would instead get back an array with just the non-nil values. The array wouldn’t be of type optional anymore, either , they would be non nil values and no nils. It’s very clean way of writing code.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.