Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fn: Added HasDuplicates function to slice #8750

Merged
merged 1 commit into from
May 21, 2024

Conversation

Chinwendu20
Copy link
Contributor

@Chinwendu20 Chinwendu20 commented May 14, 2024

Change Description

This PR adds a new function to check if the values in a slice have duplicates.

Steps to Test

Steps for reviewers to follow to test the change.

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

馃摑 Please see our Contribution Guidelines for further guidance.

Copy link

coderabbitai bot commented May 14, 2024

Important

Auto Review Skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

fn/slice.go Outdated
@@ -198,3 +198,7 @@ func Sum[B Number](items []B) B {
return a + b
}, 0, items)
}

func IsUniqueVals[A comparable](items []A) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rename to HasDups and invert the equality check.

"IsUniqueVals" is a bit awkward in English.

Otherwise this looks great

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also add a godoc for this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. I will add the godoc, HasDups also sounds to me like awkward English. I guess there is really no standard for this ? Just subjectiveness?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ultimately all naming decisions are subjective. However, just because there is an element of taste involved doesn't mean that there aren't better and worse choices.

There is a standard around the prefixes isX and hasX when a function returns a bool. The question you are attempting to answer in full english is any of the following:

  • "Does this slice have any duplicate values?"
  • "Are all of the values in this slice unique?"

Since the argument to the function is a singular slice, the slice is the subject of the sentence. The answer to the question then takes the following form.

  • "The slice has only unique values"
  • "The slice has duplicate values"

Both of these are equivalent statements and, from an awkwardness perspective, are fine. Translating this to function names we might get something like:

  • hasOnlyUniqueVals
  • hasDuplicateVals
  • hasDuplicates since "duplicate" is also a noun in English ("unique" is not)

From there we can use the very common abbreviation of "duplicate" as "dup" to arrive at a quite short and also intuitive name of hasDups.

If you think that "dup" is confusing, we can expand it to hasDuplicates instead. I'm flexible on how aggressively we abbreviate names but insist we use as precise of language as we can in naming things, specifically because these functions become canonical very very quickly. Most FP utility libraries don't actually have this function (I checked) so we can't copy their naming. In fact when doing some research to come up with the name I suggested I stumbled on people discovering that this is not a typical functional name.

Here are some of the findings of that research:

  • There are things in the Haskell ecosystem that are close.
  • Ramda doesn't have something that checks for uniqueness but it does have a function that computes it.
  • Lodash is the same story.
  • IBM's fp-go is the same story as well
  • Scala doesn't have this operation either but instead of uniq it's called distinct

So you're right that there is not a standard name for this specific function... mostly because people don't need it very often. I haven't seen how you plan to use it so I can't comment on whether it is needed, however, if we are going to put that function in this package we should name it as best as possible because utility package functions get more use than average and the fact that fn gets version pinned means that changing the names is nontrivial. The standardness that does exist is around both isX and hasX being normal for bool returning functions.

These explanations take me a considerable amount of time to put together. I am giving you this one as a courtesy because this is a subject I both know and care deeply about. In the future, I would really suggest you be more cooperative in code review and put at least as much effort into trying to understand your reviewer's comments as you do contesting them. I do not ask you to rename things just to be difficult. I would appreciate it if you were less stubborn in accepting feedback going forward.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for sharing this with me.

I would appreciate it if you were less stubborn in accepting feedback going forward.

Do you mean not asking for clarification, when one does not genuinely follow or agree with the reviewer? I do not think anyone would be growing if they did that. Not everyone is as smart and experienced as some though, we do not all think alike, contributors all come from different background ( is open source any way). What might be obvious to one might not be to the other. So I think reviewers ( and authors) should be open to explaining when need be. It is a different case when the author of a code is being unreasonable stubborn which so far from thread and others, it is interesting to me that, that is anyone's conclusion.

I think writing things, like "awkward", "unreadable" etc. might need to come with justifying explanations from the writer's POV so that the reader can understand their perspective. I think those are vague words and interpretation varies from person to person.

Hence, I think the below should not be and should be a norm in code review sessions?

I am giving you this one as a courtesy because this is a subject I both know and care deeply about.

Interesting take I have seen you, other contributors , reviewers etc. write paragraphs in PRs and issues explaining their POV. I have not seen anyone come to this conclusion that because one disagrees with the other they are not being cooperative.

I would really suggest you be more cooperative in code review and put at least as much effort into trying to understand your reviewer's comments as you do contesting them. I do not ask you to rename things just to be difficult. I would appreciate it if you were less stubborn in accepting feedback going forward.


I see why you think it might be awkward English, according to you the the slice is the subject and it would be awkward to be like, slice is unique values. I was just looking at it from the perspective of the values not really the structure that holds them, so something like, "are unique values", I was not sure I had seen a function that starts with, "are" so I just settled for using "is" at the beginning.

The name can be improved upon, out of your well curated and researched list, I think I would go with hasDuplicates That way I would not have to change the logic as well?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see why you think it might be awkward English, according to you the the slice is the subject and it would be awkward to be like, slice is unique values. I was just looking at it from the perspective of the values not really the structure that holds them, so something like, "are unique values", I was not sure I had seen a function that starts with, "are" so I just settled for using "is" at the beginning.

This is a fine intuition, and ultimately a good attempt. I just wasn't satisfied with it as is so I provided an alternative that I believe to be better so that I didn't burden you with having to figure it out on your own.

The name can be improved upon, out of your well curated and researched list, I think I would go with hasDuplicates That way I would not have to change the logic as well?

HasDuplicates is a fine option and I would support its addition. However, you still would need to invert the equality check since the presence of duplicates means that the values are not all unique.


Regarding Cooperation and Review

Do you mean not asking for clarification, when one does not genuinely follow or agree with the reviewer?

You may always ask for clarification, but you must consider the cost of giving that clarification. Wanting clarification is free. Giving clarification is at least to some degree costly. Choose wisely what you ask about. If your choices of how and when to dig in on an issue result in being a net cost to the project, people will deprioritize engaging with you.

So I think reviewers ( and authors) should be open to explaining when need be.

I am open to explaining any and all of my positions as I have done many times with you before. I'm not fundamentally against explaining things. I like doing so. I find myself frustrated by the fact that every single thing I ask for in your reviews that isn't part of a "codified standard" is challenged without an attempt to foster a cooperative spirit.

I have been asked many times in my career to do things that I either didn't completely understand or agree with. In those cases I stated my case, but ultimately communicated to those I was working with/for that I would defer to their judgement and that I would appreciate an explanation if they were willing to do so. We aren't entitled to explanations, so it's best to act in a way that makes people want to give them.

Hence, I think the below should not be and should be a norm in code review sessions?

It's not the norm. It also isn't the norm for someone to DOS the maintainer's time by demanding explanations for small requested changes when the maintainer has already done the work of giving the solution.

I have not seen anyone come to this conclusion that because one disagrees with the other they are not being cooperative.

Cooperative disagreement is nuanced. It is a give and take between hearing points and making them. Saying "I don't agree", "I don't follow", or "Can you explain why?" without demonstrating your own thoughtfulness or reasoning creates asymmetry in the effort put in by the parties involved. I don't mind that you disagree. The fact that you resist changes without offering concessions, demonstrating a genuine desire to understand, or providing mutually satisfying alternatives, makes it very difficult to make progress on these reviews.

I know that maybe it might feel like I'm attacking you with this feedback but I'm actually genuinely trying to help you with it. Whatever you choose to work on in the future be it OSS or a company environment will require being able to navigate these "softer" aspects of software development. The more you act in a way that makes people want to help you, the faster you will grow and the more opportunities you will have.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just wasn't satisfied with it as is so I provided an alternative that I believe to be better so that I didn't burden you

You just weren't satisfied with it and this is a valid reason to request changes?

I disagree with everything you wrote regarding cooperation and review and I think it is problematic that anyone thinks it is okay to order anyone based off their personal whims, caprices and personal philosophies especially in such an environment made to be collaborative such as open source.

I have pushed changes for this PR, maybe there is a better place to discuss this.

@Chinwendu20 Chinwendu20 changed the title fn: Added IsUnique function to slice fn: Added HasDuplicates function to slice May 20, 2024
Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
Copy link
Collaborator

@bhandras bhandras left a comment

Choose a reason for hiding this comment

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

LGTM 馃帀

},
{
name: "No items",
items: []int{},
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: there's no need to write out []int{} as nil evaluates to an empty slice.

@guggero guggero merged commit 68ea8d5 into lightningnetwork:master May 21, 2024
32 of 34 checks passed
@guggero
Copy link
Collaborator

guggero commented May 21, 2024

Pushed a new tag fn/v1.0.7.

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.

None yet

4 participants