Updated 30 November 2020
Higher order functions in Swift are extremely powerful tools to have in your developer toolkit.
And Higher order functions are functions that operate on other functions by taking function as argument or returning function.
In this tutorial, I’m going to present the most famous and mostly used higher order functions in iOS swift.
So use of this we can achieve the solution in a quick way.
Because of the higher-order function, we can save our time.
-> Map
Returns an array containing the results of mapping the given closure over the sequence’s elements.
1 2 3 4 5 6 |
var numberData = [1,2,3,4,5,6,7,8,9] var mapData = numberData.map({ (number) -> Int in return number }) print(mapData) "1,2,3,4,5,6,7,8,9" |
Sorted using the given predicate as the comparison between elements.
So use of this function you can sort the array in an easy way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
var numberData = [2,6,3,4,9,1,7,8,5] var sortAscending = numberData.sortted() print(numberData) print(sortAccending) 2,6,3,4,9,1,7,8,5 1,2,3,4,5,6,7,8,9 Default sorted function give the data in ascending order var numberData = [2,6,3,4,9,1,7,8,5] var sortDescending = numberData.sortted{ (num1, num2) -> Bool in { return num1 > num2 } print(numberData) print(sortDescending) 2,6,3,4,9,1,7,8,5 9,8,7,6,5,4,3,2,1 The above data is descending order data. |
Returns an array containing, in order, the elements of the sequence that satisfy the given predicate.
So this filters the array in an easy way. because of the filter function, we can create the optimized code.
1 2 3 4 5 6 |
var numberData = [1,2,3,4,5,6,7,8,9] var filterData = numberData.filter({ ($0 > 7) -> Int in return number }) print(filterData) "8,9" |
Returns the result of combining the elements of the sequence using the given closure
So use the reduce we can add the array element in fast way
1 2 3 4 5 6 7 8 9 |
var numberData = [1,2,3,4,5,6,7,8,9] var reduceData = numberData.reduce(0, {$0 + $1}) print("sum=", reduceData) sum = "45" var numberData = [1,2,3,4,5,6,7,8,9] var reduceData = numberData.reduce(0,+) print("sum=", reduceData) sum = "45" |
-> Remove All
Removes all the elements that satisfy the given predicate.
Use this method to remove every element in a collection that meets particular criteria
And this works very fast and provides a very quick solution.
1 2 3 4 5 |
var numbers = [2, 1, 2, 3, 2, 4, 2, 5] var removeData = numbers.removeAll { $0 == 2 } print(removeData) 1, 3, 4, 5 |
Returns the bool value if any array contains data or not with a given predicate.
A closure that takes an element of the sequence as its argument and returns a Boolean value that indicates whether the passed element represents a match.
1 2 3 4 5 6 7 8 |
enum NumData { case number1 case number2 } let numValue: [NumData] = [.dog, .dog] let num = numValue.contains(.cat) print(num) false |
And Thanks for reading this blog, You can get other blogs from here
But This will help you to use the higher-order function and if you feel any issue or suggestion then leave a comment.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.