What is higher order functions in Swift. ?

HIGHER ORDER FUNCTIONS

Always go high


This blog contains :
1. What is Higher order functions?
2. How many higher order functions are in Swift.?
3. Why we use it?

So, Let's get started:

1. What is Higher order function?
Higher order functions are simply functions that operate on other functions by either taking a function as an argument, or returning a function.

In Mathematics and computer science, a Higher order function is a function that does at least one of the following:
1. Take one or more functions as argument..
2. Returns a function as its result.

2. How many higher order functions are in Swift?
Actually there is no fixed number of Higher order functions. But yes I am going to describe some of them which is very useful and often we use in our logic's.

  • map 
  • compactMap
  • flatMap 
  • filter
  • reduce
  • forEach
  • contains
  • removeAll
  • sorted
  • split

Lets Describe one by one:

Map 
Map is most commonly used function. which is mainly used when you want to apply the same operation to each element of a collection. 

Example: 

CompactMap
 A compactMap is preety much similar to map but the difference is only compactMap does not contains nil in array.

Example: 

















































Comments