High Order Functions Swift : Higher order function are the function which operate on other function by taking the argument or return the function
To reduce the complicity in swift, they have introduced Higher order function in swift. Higher order function have some benefits they are
- Closure
- Collection
- Unwrapping optional
- Argument Shortened
The Various Types of swift Higher order Function are
- map
- removeAll
- compactMap
- flatMap
- split
- filter
- reduce
- contains
- forEach
- sorted
High Order Functions Swift are
Map :
let arrayNo = [ 2,4,6,8,10,12]
let arrayMapped = arrayNo.map { $0 / 2 }
print(arrayMapped)
Filter :
let arrayNo = [ 2,4,6,8,10,12]
let arrayfilter = arrayNo.filter{ $0 > 5}
print(arrayfilter)
CompactMap
CompactMap is the same as the Map function but with optional handling capability.
let arrayNo = [ 2,4,6,8,10,12]
let arraycomapct = arrayNo.compactMap { $0 > 5 }
print(arraycomapct)
Sorted :
let arraydata = [ 12,4,10,8,10,2]
let arraySorted = arraydata.sorted()
print(arraySorted)
Reduce :
let arraydata = [ 12, 4, 10,8,10,2]
let arraySorted = arraydata.reduce(“”) { (result, value) in
return result + String(value)
}
print(arraySorted)