#78: allSatisfy in Swift

allSatisfy answers a very common question: does every element match this condition?

let values = [2, 4, 6, 8]
let allEven = values.allSatisfy { $0.isMultiple(of: 2) }

This is easier to read than inverting contains(where:) or writing manual loops.

It short-circuits as soon as the predicate fails, so it is not just cleaner but also efficient.


I'm also on Twitter and GitHub.

Subscribe via RSS.