The collection is something which is used by almost everyone. It makes our life easy. Array
, Set
, and Dictionary
are the best examples of them.
To iterate, filter, or modify the existing collection object, Swift provides us with a few in builds transform operators. Map operator is one of them. Let’s try to understand the Map operator in detail.
What is Map operator?
Map operator is used to creating a new collection object by iterating over the existing collection object and applying some transformation to it.
The transformation that needs to be applied, is passed as a higher-order function named transform
to the map function.
Since the map operator is present in Iterator, it can be used with all three collections ie Array
, Set
, and Dictionary
.
/// Returns an array containing the results of mapping the given closure over the sequence's elements.
/// - Parameter transform: A mapping closure. `transform` accepts an
/// element of this sequence as its parameter and returns a transformed value of the same or of a different type.
/// - Returns: An array containing the transformed elements of this sequence.
@inlinable public func map<T>(_ transform: (Element) throws -> T) rethrows -> [T]
Keep in mind, that this map function never holds the exceptions but it throws
them to the caller. If there are any chances of any kind of exception at runtime, we need to wrap this inside a try-catch block.
Map with Array
map operator can be used over an Array to apply the transformation on its elements and return the new Array. It can be used with a named parameter or using $0.
let array = [1, 2, 3, 4, 5, 6]
let mappedArray = array.map { item in
//this will convert all the elements to its square
item * item
}
//or
let mappedArray = array.map {
//this will convert all the elements to its square
$0 * $0
}
print(mappedArray)
It can also be used with enumerated for positional-based access.
let array = [1, 2, 3, 4, 5, 6]
let mappedArray = array.enumerated().map { index, item in
//this will convert all the elements to its square
item * item
}
//or
let mappedArray = arrayenumerated.map {
//this will convert all the elements to its square
$1 * $1
}
print(mappedArray)
Map with Set
Similar to Array, the map operator can be used along with a Set
to iterate over its elements, apply some transformation on its elements and return the new Set
. It can be used with a named parameter or using $0.
let set : Set = [1, 2, 3, 4, 5, 6]
let filteredSet = set.map { item in
item * item
}
//or
let filteredSet = set.map {
$0 * $0
}
print(filteredSet)
There is no point in using an enumerated map with the Set
as it never guarantees the sequence in a Set
.
Map with Dictionary
Similar to Array
and Set
, the map operator can be used over a Dictionary
to iterate over the Dictionary
, apply some transformation to its elements and return the new Dictionary
. It can be used with a named parameter or using $0.
let map = [1: "One", 2: "Two", 3: "Three"]
let filteredMap = map.map { item in
(item.key, item.value.uppercased())
}
//or
let filteredMap = map.map {
($0.key, $0.value.uppercased())
}
print(filteredMap)
There is no point in using an enumerated map with the Dictionary
as it never guarantees the sequence in a Dictionary
.