|
45 | 45 | /// /// (1..<6).scan(0, +) // [0, 1, 3, 6, 10, 15]
|
46 | 46 | /// ///
|
47 | 47 | /// /// - Complexity: O(n)
|
48 |
| -/// func scan<ResultElement>( |
49 |
| -/// _ initial: ResultElement, |
50 |
| -/// _ nextPartialResult: (ResultElement, Element) -> ResultElement |
51 |
| -/// ) -> [ResultElement] { |
| 48 | +/// func scan<Result>( |
| 49 | +/// _ initial: Result, |
| 50 | +/// _ nextPartialResult: (Result, Element) -> Result |
| 51 | +/// ) -> [Result] { |
52 | 52 | /// var result = [initial]
|
53 | 53 | /// for x in self {
|
54 | 54 | /// result.append(nextPartialResult(result.last!, x))
|
|
60 | 60 | /// You can build a sequence type that lazily computes the elements in the
|
61 | 61 | /// result of a scan:
|
62 | 62 | ///
|
63 |
| -/// struct LazyScanSequence<Base: Sequence, ResultElement> |
| 63 | +/// struct LazyScanSequence<Base: Sequence, Result> |
64 | 64 | /// : LazySequenceProtocol
|
65 | 65 | /// {
|
66 |
| -/// let initial: ResultElement |
| 66 | +/// let initial: Result |
67 | 67 | /// let base: Base
|
68 | 68 | /// let nextPartialResult:
|
69 |
| -/// (ResultElement, Base.Element) -> ResultElement |
| 69 | +/// (Result, Base.Element) -> Result |
70 | 70 | ///
|
71 | 71 | /// struct Iterator: IteratorProtocol {
|
72 | 72 | /// var base: Base.Iterator
|
73 |
| -/// var nextElement: ResultElement? |
| 73 | +/// var nextElement: Result? |
74 | 74 | /// let nextPartialResult:
|
75 |
| -/// (ResultElement, Base.Element) -> ResultElement |
| 75 | +/// (Result, Base.Element) -> Result |
76 | 76 | ///
|
77 |
| -/// mutating func next() -> ResultElement? { |
| 77 | +/// mutating func next() -> Result? { |
78 | 78 | /// return nextElement.map { result in
|
79 | 79 | /// nextElement = base.next().map {
|
80 | 80 | /// nextPartialResult(result, $0)
|
|
87 | 87 | /// func makeIterator() -> Iterator {
|
88 | 88 | /// return Iterator(
|
89 | 89 | /// base: base.makeIterator(),
|
90 |
| -/// nextElement: initial as ResultElement?, |
| 90 | +/// nextElement: initial as Result?, |
91 | 91 | /// nextPartialResult: nextPartialResult)
|
92 | 92 | /// }
|
93 | 93 | /// }
|
94 | 94 | ///
|
95 | 95 | /// Finally, you can give all lazy sequences a lazy `scan(_:_:)` method:
|
96 | 96 | ///
|
97 | 97 | /// extension LazySequenceProtocol {
|
98 |
| -/// func scan<ResultElement>( |
99 |
| -/// _ initial: ResultElement, |
100 |
| -/// _ nextPartialResult: @escaping (ResultElement, Element) -> ResultElement |
101 |
| -/// ) -> LazyScanSequence<Self, ResultElement> { |
102 |
| -/// return LazyScanSequence<Self, ResultElement>( |
| 98 | +/// func scan<Result>( |
| 99 | +/// _ initial: Result, |
| 100 | +/// _ nextPartialResult: @escaping (Result, Element) -> Result |
| 101 | +/// ) -> LazyScanSequence<Self, Result> { |
| 102 | +/// return LazyScanSequence( |
103 | 103 | /// initial: initial, base: self, nextPartialResult: nextPartialResult)
|
104 | 104 | /// }
|
105 | 105 | /// }
|
|
0 commit comments