Skip to content

Add method distinct to TraverseFilter#3603

Merged
djspiewak merged 1 commit into
typelevel:masterfrom
takayahilton:add-distinct-traversefilter
Sep 13, 2020
Merged

Add method distinct to TraverseFilter#3603
djspiewak merged 1 commit into
typelevel:masterfrom
takayahilton:add-distinct-traversefilter

Conversation

@takayahilton

Copy link
Copy Markdown
Contributor

It comes from Data.Witherable's hashNub and ordNub.

It comes from Data.Witherable's hashNub and ordNub.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

Merging #3603 into master will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            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              

@djspiewak
djspiewak merged commit d43d2f3 into typelevel:master Sep 13, 2020
@takayahilton
takayahilton deleted the add-distinct-traversefilter branch September 14, 2020 14:08

@22majda22 22majda22 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ggg

Comment on lines +110 to +116
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(linking to discussions in #4147 and #4185 just for reference)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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" ...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should fix this. This "law" precludes many Hash implementations on opaque types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants