Compare commits

...
1 Commits
3 changed files with 18 additions and 36 deletions

No files matched your search

+3 -6
View File
@@ -12,12 +12,9 @@ let exclusiveRunningTotal = (1...5).reductions(0, +)
print(exclusiveRunningTotal) print(exclusiveRunningTotal)
// prints [0, 1, 3, 6, 10, 15] // prints [0, 1, 3, 6, 10, 15]
var value = 0 let intoRunningTotal = (1...5).reductions(into: 0, +=)
let intoRunningTotal = (1...5).reductions(into: &value, +=)
print(intoRunningTotal) print(intoRunningTotal)
// prints [0, 1, 3, 6, 10, 15] // prints [0, 1, 3, 6, 10, 15]
print(value)
// prints 15
let inclusiveRunningTotal = (1...5).reductions(+) let inclusiveRunningTotal = (1...5).reductions(+)
print(inclusiveRunningTotal) print(inclusiveRunningTotal)
@@ -38,7 +35,7 @@ extension LazySequenceProtocol {
) -> ExclusiveReductions<Result, Self> ) -> ExclusiveReductions<Result, Self>
public func reductions<Result>( public func reductions<Result>(
into initial: inout Result, into initial: Result,
_ transform: @escaping (inout Result, Element) -> Void _ transform: @escaping (inout Result, Element) -> Void
) -> ExclusiveReductions<Result, Self> ) -> ExclusiveReductions<Result, Self>
@@ -57,7 +54,7 @@ extension Sequence {
) rethrows -> [Result] ) rethrows -> [Result]
public func reductions<Result>( public func reductions<Result>(
into initial: inout Result, into initial: Result,
_ transform: (inout Result, Element) throws -> Void _ transform: (inout Result, Element) throws -> Void
) rethrows -> [Result] ) rethrows -> [Result]
+9 -12
View File
@@ -39,9 +39,7 @@ extension LazySequenceProtocol {
_ initial: Result, _ initial: Result,
_ transform: @escaping (Result, Element) -> Result _ transform: @escaping (Result, Element) -> Result
) -> ExclusiveReductions<Result, Self> { ) -> ExclusiveReductions<Result, Self> {
return reductions(into: initial) { result, element in
var result = initial
return reductions(into: &result) { result, element in
result = transform(result, element) result = transform(result, element)
} }
} }
@@ -69,7 +67,7 @@ extension LazySequenceProtocol {
/// - Complexity: O(1) /// - Complexity: O(1)
@inlinable @inlinable
public func reductions<Result>( public func reductions<Result>(
into initial: inout Result, into initial: Result,
_ transform: @escaping (inout Result, Element) -> Void _ transform: @escaping (inout Result, Element) -> Void
) -> ExclusiveReductions<Result, Self> { ) -> ExclusiveReductions<Result, Self> {
ExclusiveReductions(base: self, initial: initial, transform: transform) ExclusiveReductions(base: self, initial: initial, transform: transform)
@@ -117,9 +115,7 @@ extension Sequence {
_ initial: Result, _ initial: Result,
_ transform: (Result, Element) throws -> Result _ transform: (Result, Element) throws -> Result
) rethrows -> [Result] { ) rethrows -> [Result] {
return try reductions(into: initial) { result, element in
var result = initial
return try reductions(into: &result) { result, element in
result = try transform(result, element) result = try transform(result, element)
} }
} }
@@ -160,7 +156,7 @@ extension Sequence {
/// - Complexity: O(_n_), where _n_ is the length of the sequence. /// - Complexity: O(_n_), where _n_ is the length of the sequence.
@inlinable @inlinable
public func reductions<Result>( public func reductions<Result>(
into initial: inout Result, into initial: Result,
_ transform: (inout Result, Element) throws -> Void _ transform: (inout Result, Element) throws -> Void
) rethrows -> [Result] { ) rethrows -> [Result] {
@@ -168,6 +164,7 @@ extension Sequence {
output.reserveCapacity(underestimatedCount + 1) output.reserveCapacity(underestimatedCount + 1)
output.append(initial) output.append(initial)
var initial = initial
for element in self { for element in self {
try transform(&initial, element) try transform(&initial, element)
output.append(initial) output.append(initial)
@@ -595,10 +592,10 @@ extension LazySequenceProtocol {
@available(*, deprecated, message: "Use reductions(into:_:) instead.") @available(*, deprecated, message: "Use reductions(into:_:) instead.")
@inlinable @inlinable
public func scan<Result>( public func scan<Result>(
into initial: inout Result, into initial: Result,
_ transform: @escaping (inout Result, Element) -> Void _ transform: @escaping (inout Result, Element) -> Void
) -> ExclusiveReductions<Result, Self> { ) -> ExclusiveReductions<Result, Self> {
reductions(into: &initial, transform) reductions(into: initial, transform)
} }
} }
@@ -616,10 +613,10 @@ extension Sequence {
@available(*, deprecated, message: "Use reductions(into:_:) instead.") @available(*, deprecated, message: "Use reductions(into:_:) instead.")
@inlinable @inlinable
public func scan<Result>( public func scan<Result>(
into initial: inout Result, into initial: Result,
_ transform: (inout Result, Element) throws -> Void _ transform: (inout Result, Element) throws -> Void
) rethrows -> [Result] { ) rethrows -> [Result] {
try reductions(into: &initial, transform) try reductions(into: initial, transform)
} }
} }
@@ -27,17 +27,11 @@ final class ReductionsTests: XCTestCase {
XCTAssertEqualCollections([1].lazy.reductions(0, +), [0, 1]) XCTAssertEqualCollections([1].lazy.reductions(0, +), [0, 1])
XCTAssertEqualCollections(EmptyCollection<Int>().lazy.reductions(0, +), [0]) XCTAssertEqualCollections(EmptyCollection<Int>().lazy.reductions(0, +), [0])
var value = 0 XCTAssertEqual([1, 2, 3, 4].lazy.reductions(into: 0, +=), [0, 1, 3, 6, 10])
XCTAssertEqual([1, 2, 3, 4].lazy.reductions(into: &value, +=), [0, 1, 3, 6, 10])
XCTAssertEqual(value, 10)
value = 0 XCTAssertEqual([1].lazy.reductions(into: 0, +=), [0, 1])
XCTAssertEqual([1].lazy.reductions(into: &value, +=), [0, 1])
XCTAssertEqual(value, 1)
value = 0 XCTAssertEqual(EmptyCollection<Int>().lazy.reductions(into: 0, +=), [0])
XCTAssertEqual(EmptyCollection<Int>().lazy.reductions(into: &value, +=), [0])
XCTAssertEqual(value, 0)
XCTAssertLazySequence((1...).prefix(1).lazy.reductions(0, +)) XCTAssertLazySequence((1...).prefix(1).lazy.reductions(0, +))
XCTAssertLazySequence([1].lazy.reductions(0, +)) XCTAssertLazySequence([1].lazy.reductions(0, +))
@@ -49,17 +43,11 @@ final class ReductionsTests: XCTestCase {
XCTAssertEqual([1].reductions(0, +), [0, 1]) XCTAssertEqual([1].reductions(0, +), [0, 1])
XCTAssertEqual(EmptyCollection<Int>().reductions(0, +), [0]) XCTAssertEqual(EmptyCollection<Int>().reductions(0, +), [0])
var value = 0 XCTAssertEqual([1, 2, 3, 4].reductions(into: 0, +=), [0, 1, 3, 6, 10])
XCTAssertEqual([1, 2, 3, 4].reductions(into: &value, +=), [0, 1, 3, 6, 10])
XCTAssertEqual(value, 10)
value = 0 XCTAssertEqual([1].reductions(into: 0, +=), [0, 1])
XCTAssertEqual([1].reductions(into: &value, +=), [0, 1])
XCTAssertEqual(value, 1)
value = 0 XCTAssertEqual(EmptyCollection<Int>().reductions(into: 0, +=), [0])
XCTAssertEqual(EmptyCollection<Int>().reductions(into: &value, +=), [0])
XCTAssertEqual(value, 0)
XCTAssertNoThrow(try [].reductions(0) { _, _ in throw TestError() }) XCTAssertNoThrow(try [].reductions(0) { _, _ in throw TestError() })
XCTAssertThrowsError(try [1].reductions(0) { _, _ in throw TestError() }) XCTAssertThrowsError(try [1].reductions(0) { _, _ in throw TestError() })