0001 // MARK: - Eager 0002 0003 public extension SequenceType { 0004 0005 // MARK: Reduce1 0006 0007 /// Return the result of repeatedly calling combine with an accumulated value 0008 /// initialized to the first element of self and each element of self, in turn, i.e. 0009 /// return combine(combine(...combine(combine(self[0], self[1]), 0010 /// self[2]),...self[count-2]), self[count-1]). 0011 /// ```swift 0012 /// [1, 2, 3].reduce(+) // 6 0013 /// ``` 0014 @warn_unused_result 0015 func reduce 0016 (@noescape combine: (accumulator: Generator.Element, element: Generator.Element) throws -> Generator.Element) 0017 rethrows -> Generator.Element? { 0018 var g = generate() 0019 guard let a = g.next() else { return nil } 0020 var accu = a 0021 while let next = g.next() { 0022 accu = try combine(accumulator: accu, element: next) 0023 } 0024 return accu 0025 } 0026 0027 // MARK: Scan 0028 0029 /// Return an array where every value is equal to combine called on the previous 0030 /// element, and the current element. The first element is taken to be initial. 0031 /// ```swift 0032 /// [1, 2, 3].scan(0, combine: +) 0033 /// 0034 /// [1, 3, 6] 0035 /// ``` 0036 @warn_unused_result 0037 func scan<T>(initial: T, @noescape combine: (accumulator: T, element: Generator.Element) throws -> T) rethrows -> [T] { 0038 var accu = initial 0039 return try map { e in 0040 accu = try combine(accumulator: accu, element: e) 0041 return accu 0042 } 0043 } 0044 0045 // MARK: Scan1 0046 0047 /// Return an array where every value is equal to combine called on the previous 0048 /// element, and the current element. 0049 /// ```swift 0050 /// [1, 2, 3].scan(+) 0051 /// 0052 /// [3, 6] 0053 /// ``` 0054 @warn_unused_result 0055 func scan(@noescape combine: (accumulator: Generator.Element, element: Generator.Element) throws -> Generator.Element) rethrows -> [Generator.Element] { 0056 var g = generate() 0057 guard let i = g.next() else { return [] } 0058 return try GeneratorSequence(g).scan(i, combine: combine) 0059 } 0060 } 0061 0062 // MARK: - Lazy 0063 0064 // MARK: Scan 0065 0066 /// :nodoc: 0067 public struct ScanGen
ScanReduce.swift:58 return try GeneratorSequence(g).scan(i, combine: combine)<G
ScanReduce.swift:87 public func generate() -> ScanGen<S.Generator, T> {ScanReduce.swift:88 return ScanGen(combine: combine, initial: initial, g: seq.generate()): GeneratorType, T
ScanReduce.swift:69 private let combine: (T, G.Element) -> TScanReduce.swift:71 private var g: G> : GeneratorType { 0068 0069 private let combine
ScanReduce.swift:69 private let combine: (T, G.Element) -> TScanReduce.swift:69 private let combine: (T, G.Element) -> TScanReduce.swift:70 private var initial: TScanReduce.swift:73 public mutating func next() -> T? {: (T, G.Element) -> T 0070 private var initial
ScanReduce.swift:75 initial = combine(initial, e): T 0071 private var g
ScanReduce.swift:75 initial = combine(initial, e)ScanReduce.swift:75 initial = combine(initial, e)ScanReduce.swift:76 return initial: G 0072 /// :nodoc: 0073 public mutating func next() -> T? { 0074 guard let e = g.next() else { return nil } 0075 initial = combine(initial, e) 0076 return initial 0077 } 0078 } 0079 0080 /// :nodoc: 0081 public struct LazyScanSeq
ScanReduce.swift:74 guard let e = g.next() else { return nil }<S
ScanReduce.swift:102 func scan<T>(initial: T, combine: (accumulator: T, element: Generator.Element) -> T) -> LazyScanSeq<Self, T> {ScanReduce.swift:103 return LazyScanSeq(seq: self, combine: combine, initial: initial): SequenceType, T
ScanReduce.swift:83 private let seq : SScanReduce.swift:84 private let combine: (T, S.Generator.Element) -> TScanReduce.swift:87 public func generate() -> ScanGen<S.Generator, T> {> : LazySequenceType { 0082 0083 private let seq
ScanReduce.swift:84 private let combine: (T, S.Generator.Element) -> TScanReduce.swift:84 private let combine: (T, S.Generator.Element) -> TScanReduce.swift:85 private let initial: TScanReduce.swift:87 public func generate() -> ScanGen<S.Generator, T> {: S 0084 private let combine
ScanReduce.swift:88 return ScanGen(combine: combine, initial: initial, g: seq.generate()): (T, S.Generator.Element) -> T 0085 private let initial
ScanReduce.swift:88 return ScanGen(combine: combine, initial: initial, g: seq.generate()): T 0086 /// :nodoc: 0087 public func generate() -> ScanGen<S.Generator, T> { 0088 return ScanGen(combine: combine, initial: initial, g: seq.generate()) 0089 } 0090 } 0091 0092 public extension LazySequenceType { 0093 0094 /// Return a lazy sequence where every value is equal to combine called on the previous 0095 /// element, and the current element. The first element is taken to be initial. 0096 /// ```swift 0097 /// lazy([1, 2, 3]).scan(0, combine: +) 0098 /// 0099 /// 1, 3, 6 0100 /// ``` 0101 @warn_unused_result 0102 func scan<T>(initial: T, combine: (accumulator: T, element: Generator.Element) -> T) -> LazyScanSeq<Self, T> { 0103 return LazyScanSeq(seq: self, combine: combine, initial: initial) 0104 } 0105 } 0106 0107 // MARK: Scan1 0108 /// :nodoc: 0109 public struct Scan1Gen
ScanReduce.swift:88 return ScanGen(combine: combine, initial: initial, g: seq.generate())<G
ScanReduce.swift:133 public func generate() -> Scan1Gen<S.Generator> {ScanReduce.swift:134 return Scan1Gen(combine: combine, generator: seq.generate()): GeneratorType> : GeneratorType { 0110 0111 private let combine
ScanReduce.swift:111 private let combine: (G.Element, G.Element) -> G.ElementScanReduce.swift:111 private let combine: (G.Element, G.Element) -> G.ElementScanReduce.swift:111 private let combine: (G.Element, G.Element) -> G.ElementScanReduce.swift:112 private var accu: G.Element?ScanReduce.swift:113 private var g: GScanReduce.swift:115 public mutating func next() -> G.Element? {ScanReduce.swift:121 private init(combine: (G.Element, G.Element) -> G.Element, generator: G) {ScanReduce.swift:121 private init(combine: (G.Element, G.Element) -> G.Element, generator: G) {ScanReduce.swift:121 private init(combine: (G.Element, G.Element) -> G.Element, generator: G) {ScanReduce.swift:121 private init(combine: (G.Element, G.Element) -> G.Element, generator: G) {: (G.Element, G.Element) -> G.Element 0112 private var accu
ScanReduce.swift:117 accu = combine(a, e)ScanReduce.swift:123 self.combine = combine: G.Element? 0113 private var g
ScanReduce.swift:116 guard let a = accu, e = g.next() else { return nil }ScanReduce.swift:117 accu = combine(a, e)ScanReduce.swift:118 return accu!ScanReduce.swift:124 accu = g.next(): G 0114 0115 public mutating func next() -> G.Element? { 0116 guard let a = accu, e = g.next() else { return nil } 0117 accu = combine(a, e) 0118 return accu! 0119 } 0120 0121 private init
ScanReduce.swift:116 guard let a = accu, e = g.next() else { return nil }ScanReduce.swift:122 g = generatorScanReduce.swift:124 accu = g.next()(combine: (G.Element, G.Element) -> G.Element, generator: G) { 0122 g = generator 0123 self.combine = combine 0124 accu = g.next() 0125 } 0126 } 0127 /// :nodoc: 0128 public struct LazyScan1Seq
ScanReduce.swift:134 return Scan1Gen(combine: combine, generator: seq.generate())<S
ScanReduce.swift:149 -> LazyScan1Seq<Self> {ScanReduce.swift:150 return LazyScan1Seq(seq: self, combine: combine): SequenceType> : LazySequenceType { 0129 0130 private let seq
ScanReduce.swift:130 private let seq: SScanReduce.swift:131 private let combine: (S.Generator.Element, S.Generator.Element) -> S.Generator.ElementScanReduce.swift:131 private let combine: (S.Generator.Element, S.Generator.Element) -> S.Generator.ElementScanReduce.swift:131 private let combine: (S.Generator.Element, S.Generator.Element) -> S.Generator.ElementScanReduce.swift:133 public func generate() -> Scan1Gen<S.Generator> {: S 0131 private let combine
ScanReduce.swift:134 return Scan1Gen(combine: combine, generator: seq.generate()): (S.Generator.Element, S.Generator.Element) -> S.Generator.Element 0132 /// :nodoc: 0133 public func generate() -> Scan1Gen<S.Generator> { 0134 return Scan1Gen(combine: combine, generator: seq.generate()) 0135 } 0136 } 0137 0138 public extension LazySequenceType { 0139 0140 /// Return a lazy sequence where every value is equal to combine called on the previous 0141 /// element, and the current element. 0142 /// ```swift 0143 /// lazy([1, 2, 3]).scan(+) 0144 /// 0145 /// 3, 6 0146 /// ``` 0147 @warn_unused_result 0148 func scan(combine: (accumulator: Generator.Element, element: Generator.Element) -> Generator.Element) 0149 -> LazyScan1Seq<Self> { 0150 return LazyScan1Seq(seq: self, combine: combine) 0151 } 0152 } 0153
ScanReduce.swift:134 return Scan1Gen(combine: combine, generator: seq.generate())