With Swift many people have been diving into the world of functional programming and value types. One of the many benefits of the former is the use of new abstractions in the way of functions: filter, map, and reduce.
I won’t talk about the pro’s and con’s of this approach, but for example, instead of writting custom loops:
- If you want to convert an array of Ints to an array of Strings, you map:
[1, 2, 3, 4].map({ String($0) }) // Produces ["1", "2", "3", "4"]
. - If you get the sum of all Ints of an array, you reduce:
[1, 2, 3, 4].reduce(0, combine: { $0 + $1 }) // Produces 10
. - If you want to know if an array doesn’t contain any object with a given flag, you filter and get the count. Or do you?