Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions Guides/Reductions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ let exclusiveRunningTotal = (1...5).reductions(0, +)
print(exclusiveRunningTotal)
// prints [0, 1, 3, 6, 10, 15]

var value = 0
let intoRunningTotal = (1...5).reductions(into: &value, +=)
let intoRunningTotal = (1...5).reductions(into: 0, +=)
print(intoRunningTotal)
// prints [0, 1, 3, 6, 10, 15]
print(value)
// prints 15

let inclusiveRunningTotal = (1...5).reductions(+)
print(inclusiveRunningTotal)
Expand All @@ -38,7 +35,7 @@ extension LazySequenceProtocol {
) -> ExclusiveReductions<Result, Self>

public func reductions<Result>(
into initial: inout Result,
into initial: Result,
_ transform: @escaping (inout Result, Element) -> Void
) -> ExclusiveReductions<Result, Self>

Expand All @@ -57,7 +54,7 @@ extension Sequence {
) rethrows -> [Result]

public func reductions<Result>(
into initial: inout Result,
into initial: Result,
_ transform: (inout Result, Element) throws -> Void
) rethrows -> [Result]

Expand Down
21 changes: 9 additions & 12 deletions Sources/Algorithms/Reductions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ extension LazySequenceProtocol {
_ initial: Result,
_ transform: @escaping (Result, Element) -> Result
) -> ExclusiveReductions<Result, Self> {

var result = initial
return reductions(into: &result) { result, element in
return reductions(into: initial) { result, element in
result = transform(result, element)
}
}
Expand Down Expand Up @@ -69,7 +67,7 @@ extension LazySequenceProtocol {
/// - Complexity: O(1)
@inlinable
public func reductions<Result>(
into initial: inout Result,
into initial: Result,
_ transform: @escaping (inout Result, Element) -> Void
) -> ExclusiveReductions<Result, Self> {
ExclusiveReductions(base: self, initial: initial, transform: transform)
Expand Down Expand Up @@ -117,9 +115,7 @@ extension Sequence {
_ initial: Result,
_ transform: (Result, Element) throws -> Result
) rethrows -> [Result] {

var result = initial
return try reductions(into: &result) { result, element in
return try reductions(into: initial) { result, element in
result = try transform(result, element)
}
}
Expand Down Expand Up @@ -160,14 +156,15 @@ extension Sequence {
/// - Complexity: O(_n_), where _n_ is the length of the sequence.
@inlinable
public func reductions<Result>(
into initial: inout Result,
into initial: Result,
_ transform: (inout Result, Element) throws -> Void
) rethrows -> [Result] {

var output = [Result]()
output.reserveCapacity(underestimatedCount + 1)
output.append(initial)

var initial = initial
for element in self {
try transform(&initial, element)
output.append(initial)
Expand Down Expand Up @@ -595,10 +592,10 @@ extension LazySequenceProtocol {
@available(*, deprecated, message: "Use reductions(into:_:) instead.")
@inlinable
public func scan<Result>(
into initial: inout Result,
into initial: Result,
_ transform: @escaping (inout Result, Element) -> Void
) -> ExclusiveReductions<Result, Self> {
reductions(into: &initial, transform)
reductions(into: initial, transform)
}
}

Expand All @@ -616,10 +613,10 @@ extension Sequence {
@available(*, deprecated, message: "Use reductions(into:_:) instead.")
@inlinable
public func scan<Result>(
into initial: inout Result,
into initial: Result,
_ transform: (inout Result, Element) throws -> Void
) rethrows -> [Result] {
try reductions(into: &initial, transform)
try reductions(into: initial, transform)
}
}

Expand Down
24 changes: 6 additions & 18 deletions Tests/SwiftAlgorithmsTests/ReductionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,11 @@ final class ReductionsTests: XCTestCase {
XCTAssertEqualCollections([1].lazy.reductions(0, +), [0, 1])
XCTAssertEqualCollections(EmptyCollection<Int>().lazy.reductions(0, +), [0])

var value = 0
XCTAssertEqual([1, 2, 3, 4].lazy.reductions(into: &value, +=), [0, 1, 3, 6, 10])
XCTAssertEqual(value, 10)
XCTAssertEqual([1, 2, 3, 4].lazy.reductions(into: 0, +=), [0, 1, 3, 6, 10])

value = 0
XCTAssertEqual([1].lazy.reductions(into: &value, +=), [0, 1])
XCTAssertEqual(value, 1)
XCTAssertEqual([1].lazy.reductions(into: 0, +=), [0, 1])

value = 0
XCTAssertEqual(EmptyCollection<Int>().lazy.reductions(into: &value, +=), [0])
XCTAssertEqual(value, 0)
XCTAssertEqual(EmptyCollection<Int>().lazy.reductions(into: 0, +=), [0])

XCTAssertLazySequence((1...).prefix(1).lazy.reductions(0, +))
XCTAssertLazySequence([1].lazy.reductions(0, +))
Expand All @@ -49,17 +43,11 @@ final class ReductionsTests: XCTestCase {
XCTAssertEqual([1].reductions(0, +), [0, 1])
XCTAssertEqual(EmptyCollection<Int>().reductions(0, +), [0])

var value = 0
XCTAssertEqual([1, 2, 3, 4].reductions(into: &value, +=), [0, 1, 3, 6, 10])
XCTAssertEqual(value, 10)
XCTAssertEqual([1, 2, 3, 4].reductions(into: 0, +=), [0, 1, 3, 6, 10])

value = 0
XCTAssertEqual([1].reductions(into: &value, +=), [0, 1])
XCTAssertEqual(value, 1)
XCTAssertEqual([1].reductions(into: 0, +=), [0, 1])

value = 0
XCTAssertEqual(EmptyCollection<Int>().reductions(into: &value, +=), [0])
XCTAssertEqual(value, 0)
XCTAssertEqual(EmptyCollection<Int>().reductions(into: 0, +=), [0])

XCTAssertNoThrow(try [].reductions(0) { _, _ in throw TestError() })
XCTAssertThrowsError(try [1].reductions(0) { _, _ in throw TestError() })
Expand Down