[stdlib] any/all algorithms, keyword for contains

Expose any, all on Array and all the Lazy sequence adapters.  Make the
'contains' algorithm that takes a predecate unavailable in favor of
'any', which does the same thing.

Fixes <rdar://problem/18190149> [algorithm] `contains` syntax is ambiguous

Swift SVN r21810
This commit is contained in:
Dave Abrahams
2014-09-09 20:39:39 +00:00
parent d37855e5c5
commit c467825bc2
8 changed files with 180 additions and 20 deletions

View File

@@ -348,8 +348,16 @@ public func lexicographicalCompare<
}
/// Return `true` iff an element in `seq` satisfies `predicate`.
@availability(*, unavailable, renamed="any")
public func contains<
S: SequenceType, L: BooleanType
>(seq: S, predicate: (S.Generator.Element)->L) -> Bool {
return any(seq, predicate)
}
/// Return `true` iff an element in `seq` satisfies `predicate`.
public func any<
S: SequenceType, L: BooleanType
>(seq: S, predicate: (S.Generator.Element)->L) -> Bool {
for a in seq {
if predicate(a) {
@@ -359,11 +367,18 @@ public func contains<
return false
}
/// Return `true` iff all elements in `seq` satisfy `predicate`.
public func all<
S: SequenceType, L: BooleanType
>(seq: S, predicate: (S.Generator.Element)->L) -> Bool {
return !any(seq) { !predicate($0) }
}
/// Return `true` iff `x` is in `seq`.
public func contains<
S: SequenceType where S.Generator.Element: Equatable
>(seq: S, x: S.Generator.Element) -> Bool {
return contains(seq, { $0 == x })
>(seq: S, element x: S.Generator.Element) -> Bool {
return any(seq, { $0 == x })
}
/// Return the result of repeatedly calling `combine` with an