Swift has a set of awesome high order functions like
reduce. Although reduce is relatively complex, it has immense
power. Reduce has flexibility to such an extent that it can do all possible array operations.
Array methods re implemented using reduce
You can check out some of the array functionalities that I have re implemented using reduce
map using reduce
If this wasnt impressive enough, check out how
map can be implemented using reduce.
filter using reduce
filter implementation is
Now what ?
reduce can be used to implement native array methods. It can also be used as map and filter. But what was the point ?
Point was to show how powerful reduce function is. It happens that we unknowingly chain map and filter as requirement changes. This chaining becomes cumbersome. If you have very specific requirements you can always use reduce. You get the desired outcome in one array traversal.