Created
February 18, 2017 01:05
-
-
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 }
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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