Swift High Order Functions
Sort, Sorted(by:), Filter, Map, Reduce, flatMap, compactMap functions called high order functions. This functions calculate massive transaction in a simple line. In term of:
Higher order functions are simply functions that operate on other functions by either taking a function as an argument, or returning a function.
We talked about Sort and sorted(by:) function before. You can read from this link.Closure-2.
Swift array types have some methods which use closures to allow us to pass in functionality that can then determine how we want the method to sort, map, filter, or reduce an array of objects.
Filter: take one parameter and return an array. Also It can pass your filter specified in your closure.
For instance, we have array that contains integers numbers. We want to filter this array only will contain even numbers. We can use filter functions for this purpose. Also I will show you 3 different usage for filter function.

If we have only one line statement, we can omit return keyword in closure. Look at the below code snippet.

Inline syntax : We can use default parameters as a $0,$1 instead of item. the name of this short usage is inline syntax.

As a result, we got same result with above 3 different usage. Generally, we prefer the third one. It will seem a little strange when you see it for the first time. But in time you will get used to it
Map: take one parameter and return an array. Also It can pass your filter specified in your closure. Map method iterate all items and return them new array one. Assume that, we have an array consist of integer numbers. We want to double these number with map function.

Assuming we have an array of numbers, and we want to convert this number to string in array.

Reduce: The reduce function accept one initial value and one closure. It allows you to combine all the elements in an array and return an object of any type.
Assuming we have an array of numbers from 1 to 10, we want to sum of value all array with reduce function.



We can use reduce function on dictionary. Let’s go deep with below code snippet. Our initial value 2, and we sum all value. Result is 2 + 10 + 7000 = 7012.


FlatMap: Flatmap is used to flatten a collection of collections.
Returns an array containing the concatenated results of calling the given transformation with each element of this sequence


CompactMap: Compact map return non-nil value from an array.
