apple / apple/swift-algorithms
`uniqued(on:)` is missing a `uniquingWith` overload.
- Dominant language
- Swift
- Stars
- 6.3k
- Forks
- 483
- PR merge metrics
- No merged PRs in 30d
Description
With dictionaries, we have a [`uniquingKeysWith`](https://developer.apple.com/documentation/swift/dictionary/3127161-init) parameter. It would be helpful to have similar for [`uniqued`](https://github.com/apple/swift-algorithms/blob/main/Guides/Unique.md).
This came up in a [Stack Overflow Q/A](https://stackoverflow.com/questions/72339425/how-to-filter-an-array-of-objects-with-unique-properties/72339742#72339742). The following works but relies on arrays—we should have something better in `Algorithms`.
```swift
import struct OrderedCollections.OrderedDictionary
public extension Sequence {
@inlinable func uniqued(
on projection: (Element) throws -> Subject,
uniquingWith combine: (Element, Element) throws -> Element
) rethrows -> [Element] {
try OrderedDictionary(keyed(by: projection), uniquingKeysWith: combine)
.values
.elements
}
}
```
```swift
public extension Sequence {
@inlinable func keyed(
by key: (Element) throws -> Key
) rethrows -> [KeyValuePairs.Element] {
try map { (try key($0), $0) }
}
}
```
Contributor guide
Research direction
Start with the `uniqued` API described in `Guides/Unique.md` and inspect its existing overloads and behavior. Compare the requested `uniquingWith` semantics with Swift’s `Dictionary` initializer example, then confirm that the new overload preserves the intended element order and handles duplicate elements consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- api
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100