Skip to content

Instantly share code, notes, and snippets.

@oisdk
Last active August 29, 2015 14:24
Show Gist options
  • Save oisdk/e81e73cf2da4eb93275b to your computer and use it in GitHub Desktop.
Save oisdk/e81e73cf2da4eb93275b to your computer and use it in GitHub Desktop.
extension GeneratorType {
mutating func any(predicate: Element -> Bool) -> Bool {
return next().map { el in predicate(el) || any(predicate) } ?? false
}
mutating func all(predicate: Element -> Bool) -> Bool {
return next().map { el in predicate(el) && all(predicate) } ?? true
}
}
var i = 0
var g = anyGenerator { ++i }
func testIsOverFive(n: Int) -> Bool {
print(n) // 1, 2, 3, 4, 5, 6
return n > 5
}
let ans = g.any(testIsOverFive) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment