Skip to content

Commit d7a74ca

Browse files
author
Dave Abrahams
committed
reduce(_ initial:combine:) => reduce(_ initialResult:_ nextPartialResult)
1 parent ecd3266 commit d7a74ca

16 files changed

+69
-58
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, 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, &+)
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(_:)` 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, +)
105+
/// let secondHalfSum = secondHalf.reduce(0, +)
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, +)
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, +)
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 ExpressibleByStringLiteral
476476
/// array as a parameter:
477477
///
478478
/// func sum(values: [Int]) -> Int {
479-
/// return values.reduce(0, combine: +)
479+
/// return values.reduce(0, +)
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, +)
380380
/// // 'primesSum' == 17
381381
///
382382
/// let primeStrings = primes.sorted().map(String.init)

stdlib/public/core/LazySequence.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@
3737
/// extension Sequence {
3838
/// /// Returns an array containing the results of
3939
/// ///
40-
/// /// p.reduce(initial, combine: combine)
40+
/// /// p.reduce(initial, nextPartialResult)
4141
/// ///
4242
/// /// for each prefix `p` of `self`, in order from shortest to
4343
/// /// longest. For example:
4444
/// ///
45-
/// /// (1..<6).scan(0, combine: +) // [0, 1, 3, 6, 10, 15]
45+
/// /// (1..<6).scan(0, +) // [0, 1, 3, 6, 10, 15]
4646
/// ///
4747
/// /// - Complexity: O(N)
4848
/// func scan<ResultElement>(
4949
/// _ initial: ResultElement,
50-
/// combine: @noescape (ResultElement, Iterator.Element) -> ResultElement
50+
/// _ nextPartialResult: @noescape (ResultElement, Iterator.Element) -> ResultElement
5151
/// ) -> [ResultElement] {
5252
/// var result = [initial]
5353
/// for x in self {
54-
/// result.append(combine(result.last!, x))
54+
/// result.append(nextPartialResult(result.last!, x))
5555
/// }
5656
/// return result
5757
/// }
@@ -64,25 +64,25 @@
6464
/// : IteratorProtocol {
6565
/// mutating func next() -> ResultElement? {
6666
/// return nextElement.map { result in
67-
/// nextElement = base.next().map { combine(result, $0) }
67+
/// nextElement = base.next().map { nextPartialResult(result, $0) }
6868
/// return result
6969
/// }
7070
/// }
7171
/// private var nextElement: ResultElement? // The next result of next().
7272
/// private var base: Base // The underlying iterator.
73-
/// private let combine: (ResultElement, Base.Element) -> ResultElement
73+
/// private let nextPartialResult: (ResultElement, Base.Element) -> ResultElement
7474
/// }
7575
///
7676
/// struct LazyScanSequence<Base: Sequence, ResultElement>
7777
/// : LazySequenceProtocol // Chained operations on self are lazy, too
7878
/// {
7979
/// func makeIterator() -> LazyScanIterator<Base.Iterator, ResultElement> {
8080
/// return LazyScanIterator(
81-
/// nextElement: initial, base: base.makeIterator(), combine: combine)
81+
/// nextElement: initial, base: base.makeIterator(), nextPartialResult)
8282
/// }
8383
/// private let initial: ResultElement
8484
/// private let base: Base
85-
/// private let combine:
85+
/// private let nextPartialResult:
8686
/// (ResultElement, Base.Iterator.Element) -> ResultElement
8787
/// }
8888
///
@@ -91,20 +91,20 @@
9191
/// extension LazySequenceProtocol {
9292
/// /// Returns a sequence containing the results of
9393
/// ///
94-
/// /// p.reduce(initial, combine: combine)
94+
/// /// p.reduce(initial, nextPartialResult)
9595
/// ///
9696
/// /// for each prefix `p` of `self`, in order from shortest to
9797
/// /// longest. For example:
9898
/// ///
99-
/// /// Array((1..<6).lazy.scan(0, combine: +)) // [0, 1, 3, 6, 10, 15]
99+
/// /// Array((1..<6).lazy.scan(0, +)) // [0, 1, 3, 6, 10, 15]
100100
/// ///
101101
/// /// - Complexity: O(1)
102102
/// func scan<ResultElement>(
103103
/// _ initial: ResultElement,
104-
/// combine: (ResultElement, Iterator.Element) -> ResultElement
104+
/// _ nextPartialResult: (ResultElement, Iterator.Element) -> ResultElement
105105
/// ) -> LazyScanSequence<Self, ResultElement> {
106106
/// return LazyScanSequence(
107-
/// initial: initial, base: self, combine: combine)
107+
/// initial: initial, base: self, nextPartialResult)
108108
/// }
109109
/// }
110110
///

stdlib/public/core/Sequence.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,18 @@
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(_:)` 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
7071
/// over the rest of the sequence.
7172
///
7273
/// extension Sequence {
7374
/// func reduce1(
74-
/// combine: (Iterator.Element, Iterator.Element) -> Iterator.Element
75+
/// _ nextPartialResult: (Iterator.Element, Iterator.Element) -> Iterator.Element
7576
/// ) -> Iterator.Element?
7677
/// {
7778
/// var i = makeIterator()
@@ -80,7 +81,7 @@
8081
/// }
8182
///
8283
/// while let element = i.next() {
83-
/// accumulated = combine(accumulated, element)
84+
/// accumulated = nextPartialResult(accumulated, element)
8485
/// }
8586
/// return accumulated
8687
/// }

stdlib/public/core/SequenceAlgorithms.swift.gyb

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

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, *)
150150
let largeText = String(large)
151151
expectEqual(largeText, String(${Self}(largeText)!))
152152

test/1_stdlib/Strideable.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ StrideTestSuite.test("Double") {
6666
// Work on Doubles
6767
expectEqual(
6868
sum,
69-
stride(from: start, to: end, by: stepSize).reduce(0.0, combine: +))
69+
stride(from: start, to: end, by: stepSize).reduce(0.0, +))
7070
}
7171

7272
func checkClosed(from start: Double, through end: Double, by stepSize: Double, sum: Double) {
7373
// Work on Doubles
7474
expectEqual(
7575
sum,
76-
stride(from: start, through: end, by: stepSize).reduce(0.0, combine: +))
76+
stride(from: start, through: end, by: stepSize).reduce(0.0, +))
7777
}
7878

7979
checkOpen(from: 1.0, to: 15.0, by: 3.0, sum: 35.0)
@@ -104,7 +104,8 @@ StrideTestSuite.test("HalfOpen") {
104104
// Work on Ints
105105
expectEqual(
106106
sum,
107-
stride(from: start, to: end, by: stepSize).reduce(0, combine: +))
107+
stride(from: start, to: end, by: stepSize).reduce(
108+
0, +))
108109

109110
// Work on an arbitrary RandomAccessIndex
110111
expectEqual(
@@ -129,12 +130,15 @@ StrideTestSuite.test("Closed") {
129130
// Work on Ints
130131
expectEqual(
131132
sum,
132-
stride(from: start, through: end, by: stepSize).reduce(0, combine: +))
133+
stride(from: start, through: end, by: stepSize).reduce(
134+
0, +))
133135

134136
// Work on an arbitrary RandomAccessIndex
135137
expectEqual(
136138
sum,
137-
stride(from: R(start), through: R(end), by: stepSize).reduce(0) { $0 + $1.x })
139+
stride(from: R(start), through: R(end), by: stepSize).reduce(
140+
0, { $0 + $1.x })
141+
)
138142
}
139143

140144
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({#by: (Equatable, Equatable) throws -> Bool##(Equatable, Equatable) throws -> Bool#})[' rethrows'][#Equatable?#]{{; name=.+}}
151151
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: max({#by: (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#}, {#(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({#(transform): (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, +)
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, +);
4848

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

test/Prototypes/CollectionTransformers.swift

Lines changed: 3 additions & 1 deletion
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, _ combine: (U, T) -> U
1279+
) -> U {
12781280
return _runCollectionTransformer(_input, _step.reduce(initial, combine))
12791281
}
12801282

0 commit comments

Comments
 (0)