filmov
tv
#codeIndia #swift #map #compactmap #flatmap #xcode |Higher order fuctions map ,flatmap & compactmap|

Показать описание
#codeIndia
map(), compactMap() and flatMap()
they might sound similar they do very different things.
There are lots of places in Swift that return optionals, including try?, as?, and any failable initializer like creating an integer from a string – these are all great candidates for compactMap().
For example, if you have a UIView and want to read out all subviews that are image views, you can write this:
Or if you have an array of strings and want to know which ones are valid URLs, you can write this:
Map returns an array containing the results of mapping the given closure over the sequence’s elements.
compactMap returns an array containing the non-nil results of calling the given transformation with each element of this sequence.
flatMap returns an array containing the concatenated results of calling the given transformation with each element of this sequence.
The flatMap() method is designed to allow you to transform optionals and elements inside a collection while also decreasing the amount of containment that happens. For example, if you transform an optional in a way that will also return an optional, using map() would give you an optional optional (a double optional),
whereas flatMap() is able to combine those two optionals into a single optional.
Decreasing the amount of containment also makes flatMap() a simple way of converting multi-dimensional arrays into single-dimensional arrays:
The flatMap() method of optionals allows you to transform the optional if it has a value, or do nothing if it is empty. This makes for shorter and more expressive code than doing a regular unwrap, and doesn’t require you to change your data type.
Using flatMap() with optionals is similar to using map(), with one important difference: if your transformation closure returns an optional, flatMap() will combine that optional with the existing optional, whereas map() will keep them both.
If you need to simply transform a value to another value, then use map .
If you need to remove nil values, then use compactMap .
If you need to flatten your result one level down, then use flatMap .
It is also possible to chain these functions to achieve the intended result.
So, again: map() will take a value out of its container, transform it using the code you specify, then put it back in its container. compactMap() does the same thing, but if your transformation returns an optional it will be unwrapped and have any nil values discarded.
* If you need to simply transform a value to another value, then use map.
* If you need to remove nil values, then use compactMap.
* If you need to flatten your result one level down, then use flatMap.
* It is also possible to chain these functions to achieve the intended result.
map(), compactMap() and flatMap()
they might sound similar they do very different things.
There are lots of places in Swift that return optionals, including try?, as?, and any failable initializer like creating an integer from a string – these are all great candidates for compactMap().
For example, if you have a UIView and want to read out all subviews that are image views, you can write this:
Or if you have an array of strings and want to know which ones are valid URLs, you can write this:
Map returns an array containing the results of mapping the given closure over the sequence’s elements.
compactMap returns an array containing the non-nil results of calling the given transformation with each element of this sequence.
flatMap returns an array containing the concatenated results of calling the given transformation with each element of this sequence.
The flatMap() method is designed to allow you to transform optionals and elements inside a collection while also decreasing the amount of containment that happens. For example, if you transform an optional in a way that will also return an optional, using map() would give you an optional optional (a double optional),
whereas flatMap() is able to combine those two optionals into a single optional.
Decreasing the amount of containment also makes flatMap() a simple way of converting multi-dimensional arrays into single-dimensional arrays:
The flatMap() method of optionals allows you to transform the optional if it has a value, or do nothing if it is empty. This makes for shorter and more expressive code than doing a regular unwrap, and doesn’t require you to change your data type.
Using flatMap() with optionals is similar to using map(), with one important difference: if your transformation closure returns an optional, flatMap() will combine that optional with the existing optional, whereas map() will keep them both.
If you need to simply transform a value to another value, then use map .
If you need to remove nil values, then use compactMap .
If you need to flatten your result one level down, then use flatMap .
It is also possible to chain these functions to achieve the intended result.
So, again: map() will take a value out of its container, transform it using the code you specify, then put it back in its container. compactMap() does the same thing, but if your transformation returns an optional it will be unwrapped and have any nil values discarded.
* If you need to simply transform a value to another value, then use map.
* If you need to remove nil values, then use compactMap.
* If you need to flatten your result one level down, then use flatMap.
* It is also possible to chain these functions to achieve the intended result.