Map, Filter and Reduce
What are they? They are functions that take functions as an argument.
Map
Map is used to transform the elements in an array or set. Say you want to multiply each element in an array by 2. How would you do this using a for-loop?
This is how you would do the same using a map function on the array:
Now, what’s the big difference here? Map evaluates the statement inside those curly braces and assigns that value to the corresponding element in the array.
Filter
Say you want to remove all numbers below 100 from an array. How would you do this using a for loop?
This is how it is done using filter:
Boom! Just one line of code and done.
Reduce
Say you want to get the mean of all elements in an array. You know how you would do it with a for loop, so I’ll skip to the reduce implementation.
The first argument in the funtion is the initial element and the second argument is a function that specifies how to combine two elements. Since + is a funtion in swift we don’t even need to write {$0 + $1}