Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Combine framework: remove duplicates by key paths.
import Combine
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher {
func removeDuplicates<Property>(by keyPath: KeyPath<Output, Property>)
-> Publishers.RemoveDuplicates<Self> where Property: Equatable
{
return self.removeDuplicates {
$0[keyPath: keyPath] == $1[keyPath: keyPath]
}
}
func removeDuplicates<Property0, Property1>(
by keyPath0: KeyPath<Output, Property0>,
_ keyPath1: KeyPath<Output, Property1>
) -> Publishers.RemoveDuplicates<Self> where
Property0: Equatable, Property1: Equatable
{
return self.removeDuplicates {
$0[keyPath: keyPath0] == $1[keyPath: keyPath0] &&
$0[keyPath: keyPath1] == $1[keyPath: keyPath1]
}
}
func removeDuplicates<Property0, Property1, Property2>(
by keyPath0: KeyPath<Output, Property0>,
_ keyPath1: KeyPath<Output, Property1>,
_ keyPath2: KeyPath<Output, Property2>
) -> Publishers.RemoveDuplicates<Self> where
Property0: Equatable, Property1: Equatable, Property2: Equatable
{
return self.removeDuplicates {
$0[keyPath: keyPath0] == $1[keyPath: keyPath0] &&
$0[keyPath: keyPath1] == $1[keyPath: keyPath1] &&
$0[keyPath: keyPath2] == $1[keyPath: keyPath2]
}
}
func removeDuplicates<Property0, Property1, Property2, Property3>(
by keyPath0: KeyPath<Output, Property0>,
_ keyPath1: KeyPath<Output, Property1>,
_ keyPath2: KeyPath<Output, Property2>,
_ keyPath3: KeyPath<Output, Property3>
) -> Publishers.RemoveDuplicates<Self> where
Property0: Equatable, Property1: Equatable, Property2: Equatable, Property3: Equatable
{
return self.removeDuplicates {
$0[keyPath: keyPath0] == $1[keyPath: keyPath0] &&
$0[keyPath: keyPath1] == $1[keyPath: keyPath1] &&
$0[keyPath: keyPath2] == $1[keyPath: keyPath2] &&
$0[keyPath: keyPath3] == $1[keyPath: keyPath3]
}
}
}
// etc, maybe repeat for more properties.
// until Swift have variadic generic parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.