Last active
April 1, 2020 16:29
-
-
Save jasdev/702d1e8fc7079ba42081b5d4f4868e20 to your computer and use it in GitHub Desktop.
An alternative, `flatMap`-based implementation of `Publisher.removeKnownDuplicates`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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