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

Dictionary.MergeStrategy: simplify implementation #2587

Merged
merged 1 commit into from
Jun 6, 2023
Merged
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
8 changes: 4 additions & 4 deletions Sources/FoundationExtensions/Dictionary+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ extension Dictionary {
extension Dictionary {

/// Merge strategy to use for any duplicate keys.
enum MergeStrategy<NewValue> {
enum MergeStrategy {

/// Keep the original value.
case keepOriginalValue
/// Overwrite the original value.
case overwriteValue

var combine: (NewValue, NewValue) -> NewValue {
var combine: (Value, Value) -> Value {
switch self {
case .keepOriginalValue:
return { original, _ in original }
Expand All @@ -59,7 +59,7 @@ extension Dictionary {
/// closure that returns the desired value for the final dictionary. The default is `overwriteValue`.
/// - Returns: A new dictionary with the combined keys and values of this
/// dictionary and `other`.
func merging(_ other: [Key: Value], strategy: MergeStrategy<Value> = .overwriteValue) -> [Key: Value] {
func merging(_ other: [Key: Value], strategy: MergeStrategy = .overwriteValue) -> [Key: Value] {
return self.merging(other, uniquingKeysWith: strategy.combine)
}

Expand All @@ -70,7 +70,7 @@ extension Dictionary {
/// - other: A dictionary to merge.
/// - strategy: The merge strategy to use for any duplicate keys. The strategy provides a
/// closure that returns the desired value for the final dictionary. The default is `overwriteValue`.
mutating func merge(_ other: [Key: Value], strategy: MergeStrategy<Value> = .overwriteValue) {
mutating func merge(_ other: [Key: Value], strategy: MergeStrategy = .overwriteValue) {
self.merge(other, uniquingKeysWith: strategy.combine)
}

Expand Down