Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active April 1, 2020 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdev/702d1e8fc7079ba42081b5d4f4868e20 to your computer and use it in GitHub Desktop.
Save jasdev/702d1e8fc7079ba42081b5d4f4868e20 to your computer and use it in GitHub Desktop.
An alternative, `flatMap`-based implementation of `Publisher.removeKnownDuplicates`
import Combine
extension Publisher where Output: Hashable {
func removeKnownDuplicates() -> Publishers.FlatMap<Publishers.Sequence<[Output], Failure>, Self> {
var seen = Set<Output>()
return flatMap { incoming in
[
seen.insert(incoming).inserted ?
incoming :
nil
]
.compactMap { $0 }
.publisher
.setFailureType(to: Failure.self)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment