Updated April 11, 2023
Definition of Swift map function
Swift map function comes from the background of functional programming where the map reduces and filter the function with appropriate collections like arrays and dictionaries to loop over them without using any for loop. Swift map function is also known as high-order functions as they take most of the functions as input. Input that will be provided on Swift map function needs to be analyzed properly as it will further transform the entire data into one. Once the swift map function is used for iteration then it makes the function easy to manipulate.
Syntax:
let rs_collction = i_p_collection.map
{
(elemnt_of_collection) -> Rslt_type in
return transformed_data;
}
Where, rs_collction is the variable or storage variable which is used for storing the value into it with the variable to be defined into the frame cell. Followed by input map collection to be putting the value once the iteration is performed as per for loop using (elemnt_of_collection) in Rslt_type in the variable considered. At last it will return transformed_data which is the transformed data as per requirement.
How map function works in Swift?
- Map function in swift works with variations according to the requirement and the approach that is chosen by the programmer at the time of implementation.
- Swift programming language is a kind of language where the sequence or any ordering with the elements matter a lot with the fact that collection types that are inherited from the Collection like array, Set and dictionary matters a lot for storing values.
- High order function is considered one of the best features of Swift programming language as it provides functions like map, sort, filter, flat map and reduce which can be used on various types of collections and sets.
- High order function loops over a collection and allows the elements to perform the same set of operation in fact it should be exactly same without any variation.
- There are scenarios when there is no need of all the elements rather elements should be present in the form of single set then in that case it should be ready in a way that it will remove most of the unnecessary or nil elements from the collections and the implement sequence is optional in nature. When implemented on collections or sequences the map is considered as a flattened collection.
- Filter is a function which can be incorporated with the map function in order to provide some useful operations that allows to filter all the existing collection of elements based on requirement filtering.
- Reduce is the function which is used for reducing of the result which gets generated from the set or arrays of elements to a single and computed value from the collection.
- There are some closures that are applied on the sets or the collections for making transformation on the values or elements present in the transformation closure while traversal then at that time it is very much needed to keep the Swift map sync with the other operations and transformational closures.
- Then comes variations to be applied on top of the map which are called as FlatMaps and CompactMaps all depends on the scenario of usage and requirement.
- FlatMaps come into picture whenever the need is to make the variation on plain map to flatten and compact the result while applying with a closure will return the appropriate sequence.
- Flat maps can take non-nil values or nil-values as well depending on the return type whether to return an optional value or no value.
- Flat maps are also known as compact maps both are same but having difference in the naming convention due to the fact that from swift version 4.1 onwards this same flat maps naming got transformed as compact maps with very slight or minimal difference.
Examples
Let us discuss examples of Swift map.
Example #1
This program demonstrates the conversion of Celsius into Fahrenheit using swift map function which makes use of the closure to transform into the transformed set of data as shown in the output.
let clsius = [-8.0, 13.0, 14.0, 32.0, 42.2]
let frhight = clsius.map { $0 * (9/5) + 32 }
print(frhight)
Output:
Example #2
This program demonstrates the precise and concise coding paradigm to reduce the boiler plate of implementing the map specially arr_of_int using the below format of map as shown in the output.
let arr_of_int = [5,2,6,2,7,8,3]
let map_new_array = arr_of_int.map { $0 * 10 }
print(map_new_array);
Output:
Example #3
This program demonstrates the concept of iterating over the dictionary and which in turn will return the keys only from which the values will get retrieved easily as shown in the output.
let countries = [1:"Austria", 2:"India", 3: "Australia", 4: "Zimbabwe", 5: "Africa", 6: "America", 7: "USA", 8: "United_Kingdom" ]
let countries_Dctnry_Keys = countries.map
{
(k_e_y, val_ue) -> Int in
return k_e_y
}
print(countries_Dctnry_Keys)
Output:
Example #4
This program demonstrates the concept of iterating over the dictionary and which in turn will return the values only from which the values will get retrieved easily as shown below.
let countries = [1:"Austria", 2:"India", 3: "Australia", 4: "Zimbabwe", 5: "Africa", 6: "America", 7: "USA", 8: "United_Kingdom" ]
let countries_Dctnry_values = countries.map
{
(k_e_y, val_ue) -> String in
return val_ue
}
print(countries_Dctnry_values)
Output:
Example #5
This program demonstrates the usage of flat Map on a sequence with a closure that returns a sequence of values.
let rslt_mrks = [[2,3,5], [6,8], [10,18,16]]
let totl_rslt_marks = rslt_mrks.flatMap { $0 }
print(totl_rslt_marks)
let passng_marks = rslt_mrks.flatMap { $0.filter { $0 > 3} }
print(passng_marks)
Output:
Example #6
This program demonstrates the reduce function with a concatenate function that can be applied incorporation with the map function in a similar manner that will print the following output as shown below.
let cds = ["jkl","mno","pqrs"]
let txt_0 = cds.reduce("", +)
print(cds)
print(txt_0)
Output:
Conclusion
Swift map is the type of function which have a lot of benefits associated with it. It helps in overcoming the complex challenges with for loop by making the iteration property simpler with swift map function. It makes the overall language quite flexible, optimized, and easy to understand while implementation. Swift map has improvised the entire traversal and organization property of array, dictionary, and sets.
Recommended Articles
We hope that this EDUCBA information on “Swift map” was beneficial to you. You can view EDUCBA’s recommended articles for more information.