0001 prefix operator ! {}
0002 public prefix func !<A>(f: A -> Bool) -> A -> Bool {
0003 return { x in !f(x) }
0004 }
0005
0006 public extension SequenceType {
0007
0008 @warn_unused_result
0011 func first(@noescape thatSatisfies: Generator.Element throws -> Bool) rethrows -> Generator.Element? {
0012 for el in self where try thatSatisfies(el) { return el }
0013 return nil
0014 }
0015
0016 @warn_unused_result
0018 func count(@noescape predicate: Generator.Element throws -> Bool) rethrows -> Int {
0019 var i = 0
0020 for el in self where try predicate(el) { i += 1 }
0021 return i
0022 }
0023
0024 @warn_unused_result
0026 func all(@noescape predicate: Generator.Element throws -> Bool) rethrows -> Bool {
0027 for x in self { guard try predicate(x) else { return false } }
0028 return true
0029 }
0030 }
0031
0032 public extension CollectionType where Index : BidirectionalIndexType {
0033
0034
0035 @warn_unused_result
0038 func last(@noescape predicate: Generator.Element throws -> Bool) rethrows -> Generator.Element? {
0039 for el in reverse() {
0040 if try predicate(el) {
0041 return el
0042 }
0043 }
0044 return nil
0045 }
0046
0047
0051 @warn_unused_result
0052 func lastIndexOf(@noescape isElement: Generator.Element throws -> Bool) rethrows -> Index? {
0053 for i in indices.reverse() where try isElement(self[i]) {
0054 return i
0055 }
0056 return nil
0057 }
0058 }
0059
0060 extension CollectionType {
0061
0064 @warn_unused_result
0065 func indicesOf(@noescape isElement: Generator.Element throws -> Bool) rethrows -> [Index] {
0066 return try indices.filter { i in try isElement(self[i]) }
0067 }
0068 }
0069
0070 public extension SequenceType {
0071 @warn_unused_result
0072 func partition(@noescape predicate: Generator.Element throws -> Bool) rethrows -> ([Generator.Element], [Generator.Element]) {
0073 var t,f: [Generator.Element]
0074 (t,f) = ([],[])
0075 for e in self {
0076 if try predicate(e) { t.append(e) } else { f.append(e) }
0077 }
0078 return (t,f)
0079 }
0080 }
0081