Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active April 1, 2020 16:30
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/3b98310fe3ca6b32fea5bfed243f54e2 to your computer and use it in GitHub Desktop.
Save jasdev/3b98310fe3ca6b32fea5bfed243f54e2 to your computer and use it in GitHub Desktop.
An alternative, `scan`-based implementation of `Publisher.removeKnownDuplicates`
import Combine
extension Publisher where Output: Hashable {
func removeKnownDuplicates() -> Publishers.CompactMap<Publishers.Scan<Self, (Set<Output>, Output?)>, Output> {
scan((Set<Output>(), Output?.none)) { seenAndPrevious, incoming in
var seen = seenAndPrevious.0
let isIncomingUnique = seen.insert(incoming).inserted
return (seen, isIncomingUnique ? incoming : nil)
}
.compactMap(\.1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment