Add method distinct to TraverseFilter#3603
Conversation
It comes from Data.Witherable's hashNub and ordNub.
Codecov Report
@@ Coverage Diff @@
## master #3603 +/- ##
==========================================
+ Coverage 91.31% 91.33% +0.01%
==========================================
Files 386 386
Lines 8617 8628 +11
Branches 243 240 -3
==========================================
+ Hits 7869 7880 +11
Misses 748 748 |
| def hashDistinct[A](fa: F[A])(implicit H: Hash[A]): F[A] = | ||
| traverseFilter[State[HashSet[A], *], A, A](fa)(a => | ||
| State(alreadyIn => if (alreadyIn(a)) (alreadyIn, None) else (alreadyIn + a, Some(a))) | ||
| ) | ||
| .run(HashSet.empty) | ||
| .value | ||
| ._2 |
There was a problem hiding this comment.
@takayahilton unfortunately it is not going to work the way it is supposed to.
The passed Hash typeclass is not used here since HashSet from Scala library relies on the Java's hashCode instead.
There was a problem hiding this comment.
@satorg note that the law I mentioned in #4147 (comment) effectively makes it okay, I think. At least at the time the PR was merged.
There was a problem hiding this comment.
Yeah, I see... Although I'm not getting it – if it is strictly required by the law for Hash to produce the same value as the universal hash does, then why would Hash be useful for at all?
There was a problem hiding this comment.
You're right, it doesn't really make sense 😕 see discussion on the topic in #4118 (comment).
The problem is, it's difficult to fix this backwards-compatibly. Because there may be code (such as this PR) that relies on this "law" ...
There was a problem hiding this comment.
I think we should fix this. This "law" precludes many Hash implementations on opaque types.
It comes from Data.Witherable's hashNub and ordNub.