Skip to content

Instantly share code, notes, and snippets.

@daehn
Forked from oisdk/PrettyAnyAll.Swift
Last active May 10, 2017 15:20
Show Gist options
  • Save daehn/9e4ddb2d92427d23a756 to your computer and use it in GitHub Desktop.
Save daehn/9e4ddb2d92427d23a756 to your computer and use it in GitHub Desktop.
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