swift-async-algorithms
swift-async-algorithms copied to clipboard
removeDuplicates naming is provisional and needs review
The name for removeDuplicates is fairly straightforward and has precedent in other frameworks, but should have greater consideration for naming and how it fits into the swift ecosystem. This type needs a guide and some research/discussion on what a good name for this type should be.
The same functionality in the swift-algorithms package is called uniqued()
uniqued() strips all duplicate elements from the entire collection. removeDuplicates strips only sequential duplicates. Given they aren't equivalent they shouldn't be named the same. removeDuplicates replicates the Rx operator of the same name, so keeping that name makes sense. At most I think the name might be made even more specific, removeSequentialDuplicates, but only if that clarity is better than keeping the term of art. I don't think it is.
The verb + direct object form is out of place in Swift—it implies mutation and no return value.
deduplicated is idiomatic.
sequentiallyDeduplicated is more accurate but won't be necessary if uniqued gets carried over here.
If that does happen,sequentiallyUniqued might be better than deduplicated.
Personally, I think the name deduplicated is a better name for what uniqued already does—"uniqued" suggests you might be getting the values of a sequence which appear only once (a concept not applicable to an async sequence), which is not what it does.
Wouldn't a name as straightforward as changes be to the point for this purpose? Immediately conveys the idea of comparing successive values only.
It's not accurate either but it's so close that I think might be good enough.
I.e. an element itself is not a "change"; "changes" is the first derivative of a sequence. A sequence of ComparisonResults could be considered "changes".
The other problem just has to do with the first element. But if we agree that the first element is considered to have "changed", okay. So this interpretation wouldn't do:
extension Sequence where Element: Equatable {
var changes: some Sequence<Element> {
adjacentPairs().lazy.filter(!=).map(\.1)
}
}
Array([1, 2].changes) // [2]
Array([1, 1].changes) // []
distinctUntilChanged
Yeah, I didn't think this was called "removeDuplicates" in Rx, it's called "distinctUntilChanged", which is somewhere very high up on the top list of worst function names ever conceived.
ReactiveSwift calls this "skipRepeats" which I think is very descriptive: "skip" feels more appropriate than "remove", which to me more implies an action on a mutable collection, and "repeats" is less ambiguous - with "removeDuplicates", it's unclear if 1, 2, 2, 3 turns into 1, 2, 3, or 1, 3. "skipRepeats" to me much more clearly implies the former, as the first 2 is not a "repeat", even though it might be a "duplicate".
If "skip" is too out of place, then I would say that this function is most closely related to "filter", so maybe "filterOutRepeats"? I still think "skipRepeats" is more pleasing though.
@phausler Should we reconsider the naming here?
I think at this point the right move would be to deprecate and replace the name if a new name applies - so not blocking.