-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 remove(where:) for Set and Dictionary 🎯 #2647
Conversation
@@ -0,0 +1,148 @@ | |||
# Add `remove(where:)` Method for `Set` and `Dictionary` | |||
|
|||
* **Proposal**: [SE-0456](0456-remove-where-for-set-and-dictionary.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
proposal numbers are assigned by the review manager when review starts. The text (and filename) should be 0000- or NNNN- for now to avoid confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pushed it.
The proposal seems mostly OK, but I have some high-level questions about this proposed API. First off, it really should be something like Can you give some examples of the real problems that you want to use this API to solve? I have some ideas about how I would use it, but I'd like to see some others and think about what other shapes this API might take that aren't as fundamentally dependent on the ordering of Set or Dictionary elements. |
@stephentyrone kindly give me sometime, will get back to you. |
numbers.remove(element) | ||
break | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For anyone following along, I would write this:
removedElement = numbers.first(where: { $0.isMultiple(of: 2) }).map {
numbers.remove($0)!
}
This PR introduces a powerful and highly-requested feature: the
remove(where:)
method for bothSet
andDictionary
. This enhancement allows developers to efficiently remove elements from these collections based on a condition, leveraging Swift's expressive closures.Motivation
🔍 Current implementations make it challenging to remove elements efficiently from
Set
andDictionary
based on conditions. Developers either need to iterate over snapshots (causing unnecessary copying) or collect items in a separate collection for removal. This PR addresses these inefficiencies by providing a native, performant solution.What's New?
1️⃣
Set.remove(where:)
: Removes the first element satisfying the condition, if any.2️⃣
Dictionary.remove(where:)
: Removes all key-value pairs satisfying the condition.3️⃣ Comprehensive unit tests included to ensure robustness.
Example Usage
For
Set
:For
Dictionary
:Implementation Details
remove(where:)
as mutating functions inSet
andDictionary
.test/stdlib/Set.swift
andtest/stdlib/Dictionary.swift
.Why This Matters
💡 This feature simplifies common use cases, improving Swift's ergonomics and efficiency for developers. It's an essential step toward making Swift's standard library more versatile and user-friendly.
References