-
-
Save daehn/9e4ddb2d92427d23a756 to your computer and use it in GitHub Desktop.
This file contains 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 IteratorProtocol { | |
mutating public func any(_ predicate: (Element) throws -> Bool) rethrows -> Bool { | |
guard let current = next() else { return false } | |
return try predicate(current) || any(predicate) | |
} | |
mutating public func all(_ predicate: (Element) throws -> Bool) rethrows -> Bool { | |
guard let current = next() else { return true } | |
return try predicate(current) && all(predicate) | |
} | |
} | |
public extension Sequence { | |
public func any(_ predicate: (Iterator.Element) throws -> Bool) rethrows -> Bool { | |
var iterator = makeIterator() | |
return try iterator.any(predicate) | |
} | |
public func all(_ predicate: (Iterator.Element) throws -> Bool) rethrows -> Bool { | |
var iterator = makeIterator() | |
return try iterator.all(predicate) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment