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.
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:
New way Example with Mapper:
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.
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.
lets try same with mapper
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
Old way of formatting Date:
And with Map
2.Values in string literals
3.Find a product in an Array
That's it about Mapper, lets start dig deeper into next high order function Flat Mapper.
What's Flat Mapping? let's see what Apple say about this
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
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
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
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 .
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
and with flat map
Old way of removing nil values of an array would be something like this
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.
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.