Let’s start with FlatMap where few point need to cover. So lets begin,
Flatmap
Flatmap is used to flatten a collection of collections.
let temp_Dictionary = [["k1":"v1","k2":"v2"],["k3":"v3","k4":"v4"]] let flatMap_1 = temp_Dictionary.flatMap { $0 } //[(key: "k2", value: "v2"), (key: "k1", value: "v1"), (key: "k3", value: "v3"), (key: "k4", value: "v4")]
It returns an array of tuples after flatmapping. We have to convert it to an array
var flatMap_dictionary = [String: String]() flatMap_1.forEach { flatMap_dictionary[$0.0] = $0.1 } //["k2": "v2", "k4": "v4", "k3": "v3", "k1": "v1"]
Sort
Sort an array simply by describing how you want the sort to work. You can do this simply by indicating the direction of the sort with a > or <.
So let’s try to sort simple name array as:
let names = ["Sagar", "Savita", "Trupti", "Shravan", "Abdul", "Pragati", "amrita"] //["Abdul", "Pragati", "Sagar", "Savita", "Shravan", "Trupti", "amrita"]
Great, but what about “amrita” starts with a lower case letter????.
To ignore case, compare the lower case versions of the names:
sortedNames = [] sortedNames = names.sorted(by: { $0.lowercased() < $1.lowercased() } //["Abdul", "amrita", "Pragati", "Sagar", "Savita", "Shravan", "Trupti"]
That WORKS!!!!!!!!????
Now let’s try out something more complex like below:,???
For user-visible sorted lists, we should always use localized comparisons.
Fortunately, strings have another property called localizedLowercase.
let sortedAcc_lastName = people.sorted(by: { $0.lastName.localizedLowercase < $1.lastName.localizedLowercase } ) //[HigherOrderFunctions.ViewController.Nano_Emp(firstName: "Shravan", lastName: "Gundawar"), HigherOrderFunctions.ViewController.Nano_Emp(firstName: "Savita", lastName: "Kakade"), HigherOrderFunctions.ViewController.Nano_Emp(firstName: "Trupti", lastName: "Karale"), HigherOrderFunctions.ViewController.Nano_Emp(firstName: "Sager", lastName: "Rode"), HigherOrderFunctions.ViewController.Nano_Emp(firstName: "paragati", lastName: "Rode"), HigherOrderFunctions.ViewController.Nano_Emp(firstName: "Adul", lastName: "Shaikh")]
This will sort according to lastName only.Obviously the first names need to be compared as well. You can achieve this by putting the two comparisons into a tuple.
let sortedComplete = people.sorted(by: { ($0.firstName.localizedLowercase,$0.lastName.localizedLowercase) < ($1.firstName.localizedLowercase,$1.lastName.localizedLowercase) } ) //[HigherOrderFunctions.ViewController.Nano_Emp(firstName: "Adul", lastName: "Shaikh"), HigherOrderFunctions.ViewController.Nano_Emp(firstName: "paragati", lastName: "Rode"), HigherOrderFunctions.ViewController.Nano_Emp(firstName: "Sager", lastName: "Rode"), HigherOrderFunctions.ViewController.Nano_Emp(firstName: "Savita", lastName: "Kakade"), HigherOrderFunctions.ViewController.Nano_Emp(firstName: "Shravan", lastName: "Gundawar"), HigherOrderFunctions.ViewController.Nano_Emp(firstName: "Trupti", lastName: "Karale")]
Contains
Generate a true/false by checking if any element in your array satisfies a condition. Related to filter
, but returns a Bool
, rather than an array.
eg. Let’s say we need to know if our fruits array contains a seven letter name.
let sevenCharWord = names.contains { $0.characters.count == 7 } //true
Drop
Drops elements from your array while a condition is true, stops checking when it encounters an element that shouldn’t be dropped.
Let’s say we want to drop all elements at the beginning of the array that doesn’t contain the letter ‘s’.
let name = names.drop { $0.contains("s") } //[ "Trupti", "Abdul", "Pragati", "amrita"]
First
You’re probably familiar with the first property that retrieves the first element of an array, but did you know you can pass in a condition to get only the first element that meets that condition?
Let’s say we want the first element of the array that contains the letter ‘r’.
let nameWithR = names.first { $0.contains("r") } //"Trupti"
ForEach
The forEach
higher order function is a cool tool for your programming arsenal – basically short-hand for the for-in
loop.
Let’s say we want print the lowercase version of every name in our Names array. names.forEach { print($0.lowercased(), terminator: " ") } //sagar savita trupti shravan abdul pragati amrita
Partition
The partition
method partitions the elements of your array based on a condition. Elements that meet the condition are placed last in the array.
names.partition(by: { $0.contains("i") }) //["Sagar", "Abdul", "Shravan", "Trupti", "Savita", "Pragati", "amrita"]
Split
You may be familiar with the components
method on String, used to split a String based on a separator.
To use the split
method on a String, you would use it on the String.characters
property, which is a String.CharacterType
, which adopts the Collection
protocol, giving characters
access to many of the same cool higher order functions that Array has access to. Once you’ve separated String characters with split, you’ll have an array of something called a SubSequence
, that you can pass in when initialising a String – you can do this on each element of your new array using the map
higher order function to end up with an array of Strings.
For Example,
let str = "I can't believe it! These higher order functions are like magic. Don't you think? Well, maybe not magic, but pretty useful all the same." let sen1 =str.characters.spilt { $0 == "." || $0 == "!" || $0 == "?" } let sen = sen1.map{ String($0) } //["I can\'t believe it", " These higher order functions are like magic", " Don\'t you think", " Well, maybe not magic, but pretty useful all the same"]
So here we have seen most of Higher Order Function. Hope you like this.
Thanks For Reading ??