Skip to content

Commit 9ee14ee

Browse files
author
Dave Abrahams
committed
reduce(_ initial:combine:) => reduce(_ initialResult:accumulatingResultWith nextPartialResult)
1 parent a216755 commit 9ee14ee

16 files changed

+56
-45
lines changed

benchmark/single-source/CaptureProp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func benchCaptureProp<S : Sequence
2121

2222
var it = s.makeIterator()
2323
let initial = it.next()!
24-
return IteratorSequence(it).reduce(initial, combine: f)
24+
return IteratorSequence(it).reduce(initial, accumulatingResultBy: f)
2525
}
2626

2727
public func run_CaptureProp(_ N: Int) {

benchmark/single-source/MapReduce.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public func run_MapReduce(_ N: Int) {
2020
var c = 0
2121
for _ in 1...N*100 {
2222
numbers = numbers.map({$0 &+ 5})
23-
c += numbers.reduce(0, combine: &+)
23+
c += numbers.reduce(0, accumulatingResultBy: &+)
2424
}
2525
CheckResults(c != 0, "IncorrectResults in MapReduce")
2626
}

stdlib/public/core/Arrays.swift.gyb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ if True:
9999
///
100100
/// You can call any method on the slices that you might have called on the
101101
/// `absences` array. To learn which half had more absences, use the
102-
/// `reduce(_:combine:)` method to calculate each sum.
102+
/// `reduce(_:accumulatingResultBy:)` method to calculate each sum.
103103
///
104-
/// let firstHalfSum = firstHalf.reduce(0, combine: +)
105-
/// let secondHalfSum = secondHalf.reduce(0, combine: +)
104+
/// let firstHalfSum = firstHalf.reduce(0, accumulatingResultBy: +)
105+
/// let secondHalfSum = secondHalf.reduce(0, accumulatingResultBy: +)
106106
///
107107
/// if firstHalfSum > secondHalfSum {
108108
/// print("More absences in the first half.")

stdlib/public/core/Boolean.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public prefix func !<T : Boolean>(a: T) -> Bool {
4242
/// `lhs` evaluates to `true`. For example:
4343
///
4444
/// let measurements = [7.44, 6.51, 4.74, 5.88, 6.27, 6.12, 7.76]
45-
/// let sum = measurements.reduce(0, combine: +)
45+
/// let sum = measurements.reduce(0, accumulatingResultBy: +)
4646
///
4747
/// if measurements.count > 0 && sum / Double(measurements.count) < 6.5 {
4848
/// print("Average measurement is less than 6.5")
@@ -121,7 +121,7 @@ public func || <T : Boolean, U : Boolean>(
121121
/// `lhs` evaluates to `true`. For example:
122122
///
123123
/// let measurements = [7.44, 6.51, 4.74, 5.88, 6.27, 6.12, 7.76]
124-
/// let sum = measurements.reduce(0, combine: +)
124+
/// let sum = measurements.reduce(0, accumulatingResultBy: +)
125125
///
126126
/// if measurements.count > 0 && sum / Double(measurements.count) < 6.5 {
127127
/// print("Average measurement is less than 6.5")

stdlib/public/core/CompilerProtocols.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public protocol StringLiteralConvertible
476476
/// array as a parameter:
477477
///
478478
/// func sum(values: [Int]) -> Int {
479-
/// return values.reduce(0, combine: +)
479+
/// return values.reduce(0, accumulatingResultBy: +)
480480
/// }
481481
///
482482
/// let sumOfFour = sum([5, 10, 15, 20])

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ internal struct _UnmanagedAnyObjectArray {
376376
/// }
377377
/// // Prints "We have 4 primes."
378378
///
379-
/// let primesSum = primes.reduce(0, combine: +)
379+
/// let primesSum = primes.reduce(0, accumulatingResultBy: +)
380380
/// // 'primesSum' == 17
381381
///
382382
/// let primeStrings = primes.sorted().map(String.init)

stdlib/public/core/LazySequence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
/// extension Sequence {
3838
/// /// Returns an array containing the results of
3939
/// ///
40-
/// /// p.reduce(initial, combine: combine)
40+
/// /// p.reduce(initial, accumulatingResultBy: combine)
4141
/// ///
4242
/// /// for each prefix `p` of `self`, in order from shortest to
4343
/// /// longest. For example:

stdlib/public/core/Sequence.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@
6161
/// algorithms, however, may call for direct iterator use.
6262
///
6363
/// One example is the `reduce1(_:)` method. Similar to the
64-
/// `reduce(_:combine:)` method defined in the standard library, which takes
65-
/// an initial value and a combining closure, `reduce1(_:)` uses the first
66-
/// element of the sequence as the initial value.
64+
/// `reduce(_:accumulatingResultBy:)` method defined in the standard
65+
/// library, which takes an initial value and a combining closure,
66+
/// `reduce1(_:)` uses the first element of the sequence as the
67+
/// initial value.
6768
///
6869
/// Here's an implementation of the `reduce1(_:)` method. The sequence's
6970
/// iterator is used directly to retrieve the initial value before looping

stdlib/public/core/SequenceAlgorithms.swift.gyb

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -522,40 +522,44 @@ extension Sequence {
522522
/// Returns the result of calling the given combining closure with each
523523
/// element of this sequence and an accumulating value.
524524
///
525-
/// The `combine` closure is called sequentially with an accumulating
526-
/// value initialized to `initial` and each element of the sequence. This
527-
/// example shows how to find the sum of an array of numbers.
525+
/// The `nextPartialResult` closure is called sequentially with an
526+
/// accumulating value initialized to `initialResult` and each
527+
/// element of the sequence. This example shows how to find the sum
528+
/// of an array of numbers.
528529
///
529530
/// let numbers = [1, 2, 3, 4]
530531
/// let addTwo: (Int, Int) -> Int = { x, y in x + y }
531-
/// let numberSum = numbers.reduce(0, combine: addTwo)
532+
/// let numberSum = numbers.reduce(0, accumulatingResultBy: addTwo)
532533
/// // 'numberSum' == 10
533534
///
534-
/// When `numbers.reduce(_:combine:)` is called, the following steps
535-
/// occur:
535+
/// When `numbers.reduce(_:accumulatingResultBy:)` is called, the
536+
/// following steps occur:
536537
///
537-
/// 1. The `combine` closure is called with the initial value and the
538-
/// first element of `numbers`, returning the sum: `1`.
538+
/// 1. The `nextPartialResult` closure is called with the initial
539+
/// result and the first element of `numbers`, returning the sum:
540+
/// `1`.
539541
/// 2. The closure is called again repeatedly with the previous call's
540542
/// return value and each element of the sequence.
541543
/// 3. When the sequence is exhausted, the last value returned from the
542544
/// closure is returned to the caller.
543545
///
544546
/// - Parameters:
545-
/// - initial: A value to use as the initial value for accumulation.
546-
/// - combine: A closure that combines an accumulating value and an
547-
/// element of the sequence into a new accumulating value, to be used
548-
/// in the next call of the `combine` closure or returned to the
549-
/// caller.
550-
/// - Returns: The final accumulated value.
551-
public func reduce<T>(
552-
_ initial: T, combine: @noescape (T, ${GElement}) throws -> T
553-
) rethrows -> T {
554-
var result = initial
547+
/// - initialResult: the initial accumulating value.
548+
/// - nextPartialResult: A closure that combines an accumulating
549+
/// value and an element of the sequence into a new accumulating
550+
/// value, to be used in the next call of the
551+
/// `nextPartialResult` closure or returned to the caller.
552+
/// - Returns: The final accumulated value.
553+
public func reduce<Result>(
554+
_ initialResult: Result,
555+
accumulatingResultBy nextPartialResult:
556+
@noescape (partialResult: Result, ${GElement}) throws -> Result
557+
) rethrows -> Result {
558+
var accumulator = initialResult
555559
for element in self {
556-
result = try combine(result, element)
560+
accumulator = try nextPartialResult(partialResult: accumulator, element)
557561
}
558-
return result
562+
return accumulator
559563
}
560564
}
561565

test/1_stdlib/ErrorHandling.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ ErrorHandlingTests.test("ErrorHandling/contains") {
233233
ErrorHandlingTests.test("ErrorHandling/reduce") {
234234
var loopCount = 0
235235
do {
236-
let x: Int = try [1, 2, 3, 4, 5].reduce(0, combine: {
236+
let x: Int = try [1, 2, 3, 4, 5].reduce(0) {
237237
(x: Int, y: Int) -> Int
238238
in
239239
loopCount += 1
@@ -242,7 +242,7 @@ ErrorHandlingTests.test("ErrorHandling/reduce") {
242242
throw SillyError.JazzHands
243243
}
244244
return total
245-
})
245+
}
246246
expectUnreachable()
247247
} catch {}
248248
expectEqual(loopCount, 3)

test/1_stdlib/NumericParsing.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ tests.test("${Self}/Basics") {
146146
// Check that we can round-trip the string representation of 2^100,
147147
// which requires an exponent to express...
148148
let large =
149-
repeatElement(1024 as ${Self}, count: 10).reduce(1, combine: *)
149+
repeatElement(1024 as ${Self}, count: 10).reduce(1, accumulatingResultBy: *)
150150
let largeText = String(large)
151151
expectEqual(largeText, String(${Self}(largeText)!))
152152

test/1_stdlib/Strideable.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ StrideTestSuite.test("HalfOpen") {
7171
// Work on Ints
7272
expectEqual(
7373
sum,
74-
stride(from: start, to: end, by: stepSize).reduce(0, combine: +))
74+
stride(from: start, to: end, by: stepSize).reduce(
75+
0, accumulatingResultBy: +))
7576

7677
// Work on an arbitrary RandomAccessIndex
7778
expectEqual(
@@ -96,12 +97,15 @@ StrideTestSuite.test("Closed") {
9697
// Work on Ints
9798
expectEqual(
9899
sum,
99-
stride(from: start, through: end, by: stepSize).reduce(0, combine: +))
100+
stride(from: start, through: end, by: stepSize).reduce(
101+
0, accumulatingResultBy: +))
100102

101103
// Work on an arbitrary RandomAccessIndex
102104
expectEqual(
103105
sum,
104-
stride(from: R(start), through: R(end), by: stepSize).reduce(0) { $0 + $1.x })
106+
stride(from: R(start), through: R(end), by: stepSize).reduce(
107+
0, accumulatingResultBy: { $0 + $1.x })
108+
)
105109
}
106110

107111
check(from: 1, through: 15, by: 3, sum: 35) // 1 + 4 + 7 + 10 + 13

test/IDE/complete_from_stdlib.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func testArchetypeReplacement2<BAR : Equatable>(_ a: [BAR]) {
149149
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: enumerated()[#EnumeratedSequence<[Equatable]>#]{{; name=.+}}
150150
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: min({#orderingBy: (Equatable, Equatable) throws -> Bool##(Equatable, Equatable) throws -> Bool#})[' rethrows'][#Equatable?#]{{; name=.+}}
151151
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: max({#orderingBy: (Equatable, Equatable) throws -> Bool##(Equatable, Equatable) throws -> Bool#})[' rethrows'][#Equatable?#]{{; name=.+}}
152-
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: reduce({#(initial): T#}, {#combine: (T, Equatable) throws -> T##(T, Equatable) throws -> T#})[' rethrows'][#T#]{{; name=.+}}
152+
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: reduce({#(initialResult): Result#}, {#accumulatingResultBy: (partialResult: Result, Equatable) throws -> Result##(partialResult: Result, Equatable) throws -> Result#})[' rethrows'][#Result#]{{; name=.+}}
153153
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: dropFirst({#(n): Int#})[#ArraySlice<Equatable>#]{{; name=.+}}
154154
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: flatMap({#(elementTransform): (Equatable) throws -> Sequence##(Equatable) throws -> Sequence#})[' rethrows'][#[SegmentOfResult.Iterator.Element]#]{{; name=.+}}
155155

test/NameBinding/reference-dependencies.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func lookUpManyTopLevelNames() {
139139

140140
// CHECK-DAG: !private "UInt"
141141
// CHECK-DAG: !private "+"
142-
let _: UInt = [1, 2].reduce(0, combine: +)
142+
let _: UInt = [1, 2].reduce(0, accumulatingResultBy: +)
143143

144144
// CHECK-DAG: !private "-"
145145
let _: UInt = 3 - 2 - 1

test/Parse/semicolon.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ for i in 1..<1000 {
4444
};
4545
}
4646

47-
let six = (1..<3).reduce(0, combine: +);
47+
let six = (1..<3).reduce(0, accumulatingResultBy: +);
4848

4949
func lessThanTwo(input: UInt) -> Bool {
5050
switch input {

test/Prototypes/CollectionTransformers.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,9 @@ public struct CollectionTransformerPipeline<
12741274
)
12751275
}
12761276

1277-
public func reduce<U>(_ initial: U, _ combine: (U, T) -> U) -> U {
1277+
public func reduce<U>(
1278+
_ initial: U, accumulatingResultBy combine: (U, T) -> U
1279+
) -> U {
12781280
return _runCollectionTransformer(_input, _step.reduce(initial, combine))
12791281
}
12801282

@@ -1315,7 +1317,7 @@ t.test("fusion/map+reduce") {
13151317
let result =
13161318
transform(xs)
13171319
.map { $0 * 2 }
1318-
.reduce(0, { $0 + $1 })
1320+
.reduce(0, accumulatingResultBy: { $0 + $1 })
13191321
expectEqual(12, result)
13201322
}
13211323

@@ -1324,7 +1326,7 @@ t.test("fusion/map+filter+reduce") {
13241326
let result = transform(xs)
13251327
.map { $0 * 2 }
13261328
.filter { $0 != 0 }
1327-
.reduce(0, { $0 + $1 })
1329+
.reduce(0, accumulatingResultBy: { $0 + $1 })
13281330
expectEqual(12, result)
13291331
}
13301332

0 commit comments

Comments
 (0)