Skip to content

Instantly share code, notes, and snippets.

Last active April 1, 2020 16:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment