Skip to content

Instantly share code, notes, and snippets.

@nbasham
Created February 18, 2017 01:05
Show Gist options
  • Save nbasham/8bf5287c5d0269e61987811525d73448 to your computer and use it in GitHub Desktop.
Save nbasham/8bf5287c5d0269e61987811525d73448 to your computer and use it in GitHub Desktop.
every function tests all elements in a Swift 3 sequence. Example usage: var allOdd = [1, 5, 37].every { $0 % 2 == 1 }
public extension Sequence {
// return true if all elements return true
func every<T>(predicate:(T) -> Bool) -> Bool {
for item in self {
if !predicate(item as! T) {
return false
}
}
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment