Swift Algorithms Reductions APIs
TIL swift-algorithms has Reductions APIs.
Two primary ones:
// https://swiftpackageindex.com/apple/swift-algorithms/1.2.1/documentation/algorithms/swift/sequence/reductions(_:)
func reductions(
_ transform: (Self.Element, Self.Element) throws -> Self.Element
) rethrows -> [Self.Element]
// https://swiftpackageindex.com/apple/swift-algorithms/1.2.1/documentation/algorithms/swift/sequence/reductions(_:_:)
func reductions<Result>(
_ initial: Result,
_ transform: (Result, Self.Element) throws -> Result
) rethrows -> [Result]
Examples:
let exclusiveRunningTotal = (1...5).reductions(0, +)
print(exclusiveRunningTotal)
// prints [0, 1, 3, 6, 10, 15]
let inclusiveRunningTotal = (1...5).reductions(+)
print(inclusiveRunningTotal)
// prints [1, 3, 6, 10, 15]
These are useful when creating a prefix sum array for solving some algorithm problems.