X
11Jan

Swift Mapping and Flat Mapping

ScreenShot2018-01-11...

 

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.

Lets look few more use cases of Swift Mapping


More Examples:

1.Formatting Date

Old way of formatting Date:

ScreenShot2018-01-11...

And with Map

ScreenShot2018-01-11...

 

2.Values in string literals

Old Way

ScreenShot2018-01-11...

New Way

ScreenShot2018-01-11...

 

3.Find a product in an Array

Initialization

ScreenShot2018-01-11...

Old Way

ScreenShot2018-01-11...

New Way

ScreenShot2018-01-11...

 

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 is realy good in handling optionals.let me describe how?.For example you have an array with 4 elements in which 2 elements are empty.if you were to MAP over this you would get back an array  with 2 optional elements and 2 nil elements.In case if you were to do a flatmap on this, you would get back an array with only 2 non-nil elements and these elements wouldn't be of type optional anymore.

let's see with the help of Map

 

ScreenShot2018-01-11...

and with flat map

 

ScreenShot2018-01-11...

 

Old way of removing nil values of an array would be something like this

 

ScreenShot2018-01-11...

If you compare old way of implementing with flat map . Flat map way code looks clean, neat and takes less number of lines of code.

Conclusion:
That's it finally we came to end of this blog.The map and flatMap functions can be more powerful and It makes your code a lot easier and simpler to use.
 

Related

Swift Closures

In this blog post we will learn what is a closure, it's syntax and types of closures in Swift with e...

Read More >

Access Control in Swift 3

End of this blog you will be able to understand the Access control specifiers open, public,internal,...

Read More >

Share

Post a Comment

Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Text/HTML
Text/HTML
Contact Us
  • *
  • *