0001
0003
0005 public extension SequenceType {
0006
0007 @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
0024 public extension SequenceType {
0025
0026 @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
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
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
0080 public func breakAt(n: Int) -> (SubSequence, SubSequence) {
0081 return (prefixUpTo(n),suffixFrom(n))
0082 }
0083 }
0084 public extension SequenceType {
0085
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
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
0119 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 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 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 public func generate() -> WhileGen<S.Generator> {
0140 return WhileGen(g: seq.generate(), condition: condition)
0141 }
0142 }
0143
0144 public extension LazySequenceType {
0145
0146 @warn_unused_result
0154 func prefixWhile(condition: Generator.Element -> Bool) -> WhileSeq<Self> {
0155 return WhileSeq(seq: self, condition: condition)
0156 }
0157 }
0158
0159 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 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 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 public func generate() -> DropWhileGen<S.Generator> {
0191 return DropWhileGen(g: seq.generate(), predicate: predicate)
0192 }
0193 }
0194
0195 public extension LazySequenceType {
0196
0197 @warn_unused_result
0205 func dropWhile(predicate: Generator.Element -> Bool) -> DropWhileSeq<Self> {
0206 return DropWhileSeq(predicate: predicate, seq: self)
0207 }
0208 }
0209