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      /// Returns the first element in self that satisfies a predicate, or nil if it doesn't
0009      /// exist
0010      @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      /// Returns the number of elements in `self` that satisfy `predicate`
0017      @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      /// Returns true iff every element in `self` satisfies `predicate`
0025      @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      /// Returns the last element in self that satisfies a predicate, or nil if it doesn't
0036      /// exist
0037      @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      /**
0048      Returns the index of the final element in `self` which satisfies 
0049      `isElement`, or `nil` if it doesn't exist.
0050      */
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      /**
0062      Returns the indices of elements of `self` that satisfy the predicate `isElement`
0063      */
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