0001    // MARK: - Eager
0002    
0003    // MARK: prefixWhile
0004    
0005    public extension SequenceType {
0006      
0007      /// Returns an array of self up until the first element that returns false for condition
0008      /// ```swift
0009      /// [1, 2, 3, 4, 5, 2].prefixWhile { $0 < 4 }
0010      ///
0011      /// [1, 2, 3]
0012      /// ```
0013      @warn_unused_result
0014      func prefixWhile(@noescape condition: Generator.Element throws -> Bool) rethrows -> [Generator.Element] {
0015        var ret : [Generator.Element] = []
0016        var g = generate()
0017        while let next = g.next() where try condition(next) { ret.append(next) }
0018        return ret
0019      }
0020    }
0021    
0022    // MARK: DropWhile
0023    
0024    public extension SequenceType {
0025      
0026      /// Returns an array of self with the first elements that return true for condition
0027      /// dropped
0028      /// ```swift
0029      /// [1, 2, 3, 4, 5, 2].dropWhile { $0 < 4 }
0030      ///
0031      /// [4, 5, 2]
0032      /// ```
0033      @warn_unused_result
0034      func dropWhile(@noescape condition: Generator.Element throws -> Bool) rethrows -> [Generator.Element] {
0035        var g = generate()
0036        while let next = g.next() {
0037          if try !condition(next) {
0038            return [next] + GeneratorSequence(g)
0039          }
0040        }
0041        return []
0042      }
0043    }
0044    
0045    public extension SequenceType {
0046      /**
0047      Returns a tuple of the prefix and suffix of `self`, the first element
0048      being the prefix up to `n`.
0049      */
0050      public func breakAt(n: Int) -> ([Generator.Element],[Generator.Element]) {
0051        let r = max(0, underestimateCount() - n)
0052        var f,b : [Generator.Element]
0053        (f,b) = ([],[])
0054        f.reserveCapacity(n)
0055        b.reserveCapacity(r)
0056        var g = generate()
0057        for _ in 0..<n {
0058          if let e = g.next() { f.append(e) } else { return (f,b) }
0059        }
0060        while let e = g.next() { b.append(e) }
0061        return (f,b)
0062      }
0063    }
0064    
0065    public extension CollectionType {
0066      /**
0067      Returns a tuple of the prefix and suffix of `self`, the first element
0068      being the prefix up to `n`.
0069      */
0070      public func breakAt
TakeDrop.swift:114
    return try indexOf(isBreak).map(breakAt) ?? (suffixFrom(startIndex),prefixUpTo(startIndex))
(n: Index) -> (SubSequence, SubSequence) { 0071 return (prefixUpTo(n),suffixFrom(n)) 0072 } 0073 } 0074 0075 public extension CollectionType where Index == Int { 0076 /** 0077 Returns a tuple of the prefix and suffix of `self`, the first element 0078 being the prefix up to `n`. 0079 */ 0080 public func breakAt(n: Int) -> (SubSequence, SubSequence) { 0081 return (prefixUpTo(n),suffixFrom(n)) 0082 } 0083 } 0084 public extension SequenceType { 0085 /** 0086 Returns a tuple of the prefix and suffix of `self`, the first element 0087 being the prefix up to the first elements of `self` which returns 0088 `true` for `isBreak`. 0089 */ 0090 public func breakAt(@noescape isBreak: Generator.Element throws -> Bool) 0091 rethrows -> ([Generator.Element],[Generator.Element]) { 0092 var f,b : [Generator.Element] 0093 (f,b) = ([],[]) 0094 var g = generate() 0095 while let e = g.next() { 0096 if try isBreak(e) { 0097 b.append(e) 0098 while let be = g.next() { b.append(be) } 0099 return (f,b) 0100 } else { 0101 f.append(e) 0102 } 0103 } 0104 return (f,b) 0105 } 0106 } 0107 public extension CollectionType { 0108 /** 0109 Returns a tuple of the prefix and suffix of `self`, the first element 0110 being the prefix up to the first elements of `self` which returns 0111 `true` for `isBreak`. 0112 */ 0113 public func breakAt(@noescape isBreak: Generator.Element throws -> Bool) rethrows -> (SubSequence, SubSequence) { 0114 return try indexOf(isBreak).map(breakAt) ?? (suffixFrom(startIndex),prefixUpTo(startIndex)) 0115 } 0116 } 0117 // MARK: - Lazy 0118 0119 // MARK: TakeWhile 0120 /// :nodoc: 0121 public struct WhileGen
TakeDrop.swift:139
  public func generate() -> WhileGen<S.Generator> {
TakeDrop.swift:140
    return WhileGen(g: seq.generate(), condition: condition)
<G
TakeDrop.swift:123
  private var g: G
TakeDrop.swift:124
  private let condition : G.Element -> Bool
TakeDrop.swift:126
  mutating public func next() -> G.Element? {
: GeneratorType> : GeneratorType { 0122 0123 private var g
TakeDrop.swift:127
    if let next = g.next() where condition(next) {
: G 0124 private let condition
TakeDrop.swift:127
    if let next = g.next() where condition(next) {
: G.Element -> Bool 0125 /// :nodoc: 0126 mutating public func next() -> G.Element? { 0127 if let next = g.next() where condition(next) { 0128 return next 0129 } 0130 return nil 0131 } 0132 } 0133 /// :nodoc: 0134 public struct WhileSeq
TakeDrop.swift:154
  func prefixWhile(condition: Generator.Element -> Bool) -> WhileSeq<Self> {
TakeDrop.swift:155
    return WhileSeq(seq: self, condition: condition)
<S
TakeDrop.swift:136
  private let seq: S
TakeDrop.swift:137
  private let condition: S.Generator.Element -> Bool
TakeDrop.swift:139
  public func generate() -> WhileGen<S.Generator> {
: SequenceType> : LazySequenceType { 0135 0136 private let seq
TakeDrop.swift:140
    return WhileGen(g: seq.generate(), condition: condition)
: S 0137 private let condition
TakeDrop.swift:140
    return WhileGen(g: seq.generate(), condition: condition)
: S.Generator.Element -> Bool 0138 /// :nodoc: 0139 public func generate() -> WhileGen<S.Generator> { 0140 return WhileGen(g: seq.generate(), condition: condition) 0141 } 0142 } 0143 0144 public extension LazySequenceType { 0145 0146 /// Returns a lazy sequence of self up until the first element that returns false for 0147 /// condition 0148 /// ```swift 0149 /// [1, 2, 3, 4, 5, 2].lazy.takeWhile { $0 < 4 } 0150 /// 0151 /// 1, 2, 3 0152 /// ``` 0153 @warn_unused_result 0154 func prefixWhile(condition: Generator.Element -> Bool) -> WhileSeq<Self> { 0155 return WhileSeq(seq: self, condition: condition) 0156 } 0157 } 0158 0159 // MARK: DropWhile 0160 /// :nodoc: 0161 public struct DropWhileGen
TakeDrop.swift:190
  public func generate() -> DropWhileGen<S.Generator> {
TakeDrop.swift:191
    return DropWhileGen(g: seq.generate(), predicate: predicate)
<G
TakeDrop.swift:163
  private let predicate: G.Element -> Bool
TakeDrop.swift:164
  private var nG: G?
TakeDrop.swift:165
  private var oG: G
TakeDrop.swift:167
  private init(g: G, predicate: G.Element -> Bool) {
TakeDrop.swift:167
  private init(g: G, predicate: G.Element -> Bool) {
TakeDrop.swift:173
  public mutating func next() -> G.Element? {
: GeneratorType> : GeneratorType { 0162 0163 private let predicate
TakeDrop.swift:170
    self.predicate = predicate
TakeDrop.swift:176
      if !predicate(next) {
: G.Element -> Bool 0164 private var nG
TakeDrop.swift:168
    nG = nil
TakeDrop.swift:174
    guard nG == nil else { return nG!.next() }
TakeDrop.swift:174
    guard nG == nil else { return nG!.next() }
TakeDrop.swift:177
        nG = oG
: G? 0165 private var oG
TakeDrop.swift:169
    oG = g
TakeDrop.swift:175
    while let next = oG.next() {
TakeDrop.swift:177
        nG = oG
: G 0166 0167 private init
TakeDrop.swift:191
    return DropWhileGen(g: seq.generate(), predicate: predicate)
(g: G, predicate: G.Element -> Bool) { 0168 nG = nil 0169 oG = g 0170 self.predicate = predicate 0171 } 0172 /// :nodoc: 0173 public mutating func next() -> G.Element? { 0174 guard nG == nil else { return nG!.next() } 0175 while let next = oG.next() { 0176 if !predicate(next) { 0177 nG = oG 0178 return next 0179 } 0180 } 0181 return nil 0182 } 0183 } 0184 /// :nodoc: 0185 public struct DropWhileSeq
TakeDrop.swift:205
  func dropWhile(predicate: Generator.Element -> Bool) -> DropWhileSeq<Self> {
TakeDrop.swift:206
    return DropWhileSeq(predicate: predicate, seq: self)
<S
TakeDrop.swift:187
  private let predicate: S.Generator.Element -> Bool
TakeDrop.swift:188
  private let seq: S
TakeDrop.swift:190
  public func generate() -> DropWhileGen<S.Generator> {
: SequenceType> : LazySequenceType { 0186 0187 private let predicate
TakeDrop.swift:191
    return DropWhileGen(g: seq.generate(), predicate: predicate)
: S.Generator.Element -> Bool 0188 private let seq
TakeDrop.swift:191
    return DropWhileGen(g: seq.generate(), predicate: predicate)
: S 0189 /// :nodoc: 0190 public func generate() -> DropWhileGen<S.Generator> { 0191 return DropWhileGen(g: seq.generate(), predicate: predicate) 0192 } 0193 } 0194 0195 public extension LazySequenceType { 0196 0197 /// Returns a lazy sequence of self with the first elements that return true for 0198 /// condition dropped 0199 /// ```swift 0200 /// [1, 2, 3, 4, 5, 2].lazy.dropWhile { $0 < 4 } 0201 /// 0202 /// 4, 5, 2 0203 /// ``` 0204 @warn_unused_result 0205 func dropWhile(predicate: Generator.Element -> Bool) -> DropWhileSeq<Self> { 0206 return DropWhileSeq(predicate: predicate, seq: self) 0207 } 0208 } 0209