Skip to content

Commit a821470

Browse files
authored
Merge pull request #12 from RougeWare/feature/reduce
Added `.reduce` which assumes reducing into 0 when elements are additive
2 parents ff4ca8f + d0e7773 commit a821470

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,28 @@ print( 0.clamping(within: 2...7)) // Prints 2
8282
print( 5.clamping(within: 2...7)) // Prints 5
8383
print(99.clamping(within: 2...7)) // Prints 7
8484
```
85+
86+
87+
88+
# Additive Reduction #
89+
90+
This introduces a `.reduce(_:)` function to sequences whose elements conform to `AdditiveArithmetic` which assumes you're reducing into `.zero`:
91+
92+
```swift
93+
// Before
94+
bunchaNumbers.reduce(into: 0) { $0 = max($0, $1) }
95+
96+
// After
97+
bunchaNumbers.reduce { $0 = max($0, $1) }
98+
```
99+
100+
101+
This also adds a convenience function `.sum()`, build on this new reducer:
102+
103+
```swift
104+
// Before
105+
bunchaNumbers.reduce(into: 0, +=)
106+
107+
// After
108+
bunchaNumbers.sum()
109+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// additive reductions.swift
3+
//
4+
//
5+
// Created by The Northstar✨ System on 2023-11-30.
6+
//
7+
8+
import Foundation
9+
10+
11+
12+
public extension Sequence where Element: AdditiveArithmetic {
13+
14+
/// Reduces this collection to a single value, assuming the result should be of the same type as each element, and that the starting value for reduction is `.zero`
15+
///
16+
/// - Parameters:
17+
/// - reducer: Processes each element and reduces this collection to a single value
18+
/// - result: The running result of reduction (starts with `.zero`)
19+
/// - each: Each element of the sequence
20+
///
21+
/// - Returns: The whole collection, reduced down to a single value
22+
func reduce(_ reducer: (_ result: inout Element, _ each: Element) -> Void) -> Element {
23+
reduce(into: .zero, reducer)
24+
}
25+
26+
27+
/// Returns the sum of all elements in this sequence
28+
func sum() -> Element {
29+
reduce(+=)
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// additive reductions tests.swift
3+
//
4+
//
5+
// Created by The Northstar✨ System on 2023-11-30.
6+
//
7+
8+
import XCTest
9+
import BasicMathTools
10+
11+
12+
13+
final class additive_reductions_tests: XCTestCase {
14+
func testSum() {
15+
XCTAssertEqual(29, [1, 2, 3, 5, 7, 11].sum())
16+
XCTAssertEqual(0, [1, -1, 1, -1, 1, -1].sum())
17+
}
18+
19+
20+
func testReduce() {
21+
XCTAssertEqual(-58, [1, 2, 3, 5, 7, 11].reduce { result, each in
22+
result = (result - (each * 2))
23+
})
24+
25+
XCTAssertEqual(-29, [1, 2, 3, 5, 7, 11].reduce(-=))
26+
27+
XCTAssertEqual(0, [1, 2, 3, 5, 7, 11].reduce(*=))
28+
29+
XCTAssertEqual(56, [8, -31, 11, -13, 56, 27, -2].reduce { $0 = max($0, $1) })
30+
}
31+
}

0 commit comments

Comments
 (0)