Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inout variant of groupedReduce #221

Merged
merged 1 commit into from
Oct 2, 2020
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
39 changes: 36 additions & 3 deletions Sources/Extensions/Foundation/Sequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ public extension Sequence {
/// - combine: a closure that combines the accumulating value of a group and produces a new accumulating value.
/// - groupBy: a closure that produces a key for each element in the sequence.
/// - Returns: a dictionary containing the final accumulated values for each produced key.
func groupedReduce<K: Hashable, U>(initial: U,
combine: (U, Iterator.Element) throws -> U,
groupBy: (Iterator.Element) throws -> K) rethrows -> [K : U] {
func groupedReduce<K: Hashable, U>(
initial: U,
combine: (U, Iterator.Element) throws -> U,
groupBy: (Iterator.Element) throws -> K
) rethrows -> [K : U] {

var result: [K : U] = [:]

for element in self {
Expand All @@ -26,4 +29,34 @@ public extension Sequence {

return result
}

/// Returns the result of combining the elements of the sequence using the given combining closure, grouped by keys
/// generated using the a grouping closure. The result is a dictionary of type `[K : U]`. An initial value should
/// be given to be used as initial accumulating value in each group.
///
/// This method is preferred over `groupedReduce(initial:combine:groupBy:)` for efficiency when the group `U` is a
/// copy-on-write type, for example an Array or a Dictionary.
///
/// - Parameters:
/// - initial: a value to be used as the initial accumulating value in each group.
/// - combine: a closure that combines the accumulating value of a group and produces a new accumulating value.
/// - groupBy: a closure that produces a key for each element in the sequence.
/// - Returns: a dictionary containing the final accumulated values for each produced key.
func groupedReduce<K: Hashable, U>(
into initial: U,
combine: (inout U, Iterator.Element) throws -> Void,
groupBy: (Iterator.Element) throws -> K
) rethrows -> [K : U] {

var result: [K : U] = [:]

for element in self {
let key = try groupBy(element)
var group = result[key] ?? initial
try combine(&group, element)
result[key] = group
}

return result
}
}
36 changes: 36 additions & 0 deletions Tests/AlicerceTests/Extensions/Foundation/SequenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,40 @@ class SequenceTests: XCTestCase {

XCTAssertEqual(groupedSeq, ["a".utf8.first! : 3, "b".utf8.first! : 2, "c".utf8.first! : 1, "d".utf8.first! : 1])
}

func testGroupedReduceInto_WithNonEmptySequenceAndSameKeyType_ShouldReturnGroupedDictionary() {

let seq = ["a", "a", "a", "b", "b", "c", "d"]

let sumCombine: (inout Int, String) -> Void = { acc, element in acc += 1 }
let stringKey: (String) -> String = { $0 }

let groupedSeq: [String : Int] = seq.groupedReduce(into: 0, combine: sumCombine, groupBy: stringKey)

XCTAssertEqual(groupedSeq, ["a" : 3, "b" : 2, "c" : 1, "d" : 1])
}

func testGroupedReduceInto_WithEmptySequence_ShouldReturnEmptyDictionary() {

let seq: [String] = []

let sumCombine: (inout Int, String) -> Void = { acc, element in acc += 1 }
let stringKey: (String) -> String = { $0 }

let groupedSeq: [String : Int] = seq.groupedReduce(into: 0, combine: sumCombine, groupBy: stringKey)

XCTAssertEqual(groupedSeq, [:])
}

func testGroupedReduceInto_WithNonEmptySequenceAndDifferentKeyType_ShouldReturnGroupedDictionary() {

let seq = ["a", "a", "a", "b", "b", "c", "d"]

let sumCombine: (inout Int, String) -> Void = { acc, element in acc += 1 }
let utf8Key: (String) -> UTF8Char = { $0.utf8.first! }

let groupedSeq: [UTF8Char : Int] = seq.groupedReduce(into: 0, combine: sumCombine, groupBy: utf8Key)

XCTAssertEqual(groupedSeq, ["a".utf8.first! : 3, "b".utf8.first! : 2, "c".utf8.first! : 1, "d".utf8.first! : 1])
}
}