Skip to content

Add function to initialize set from elements #1808

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 4, 2023

Conversation

joshua-kim
Copy link
Contributor

@joshua-kim joshua-kim commented Aug 3, 2023

Why this should be merged

Add a way for us to initialize a set with a pre-defined list of items. This avoids the one-liner we currently have to do of:

s := set.Set[ids.NodeID]{nodeID: struct{}{}}

and now we can just do:

s := set.Of[ids.NodeID](nodeID)

This PR adds this new function to the set package and replaces usages in avalanchego.

How this works

Calls Add for each element passed into Of

How this was tested

Added a UT

@joshua-kim joshua-kim self-assigned this Aug 3, 2023
@joshua-kim joshua-kim added enhancement New feature or request good first issue Good for newcomers labels Aug 3, 2023
Copy link

@danlaine danlaine left a comment

Choose a reason for hiding this comment

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

Looks correct to me. Only questions are:

  1. In this PR, should we use this in more places? i.e. in addition to occurrences where we do : struct{}{}
  2. Should we name this SetOf in anticipation of adding functions ShortSetOf / NodeIDSetOf?

@joshua-kim
Copy link
Contributor Author

joshua-kim commented Aug 3, 2023

In this PR, should we use this in more places? i.e. in addition to occurrences where we do : struct{}{}

Hm yeah we definitely can. I did a deeper scan and found some spots we can replace usages where we're also creating an empty set then calling Add on a predefined list of elements.

Should we name this SetOf in anticipation of adding functions ShortSetOf / NodeIDSetOf?

Hmm personally I dislike repeating package names as I think set.Of sounds better than set.SetOf. I think set[ids.ShortID].Of and set[ids.NodeID].Of should be enough for now? I'm not sure what making separate signatures for ids.ShortID and ids.NodeID would give us that aren't already provided by the generic type

@danlaine
Copy link

danlaine commented Aug 3, 2023

In this PR, should we use this in more places? i.e. in addition to occurrences where we do : struct{}{}

Hm yeah we definitely can. I did a deeper scan and found some spots we can replace usages where we're also creating an empty set then calling Add on a predefined list of elements.

Should we name this SetOf in anticipation of adding functions ShortSetOf / NodeIDSetOf?

Hmm personally I dislike repeating package names as I think set.Of sounds better than set.SetOf. I think set[ids.ShortID].Of and set[ids.NodeID].Of should be enough for now? I'm not sure what making separate signatures for ids.ShortID and ids.NodeID would give us that aren't already provided by the generic type

Ah totally forgot we no longer have the ShortSet and NodeIDSet types anymore lol nvm

@darioush
Copy link
Contributor

darioush commented Aug 3, 2023

nit: I wonder if we should call this FromList instead of Of?

@joshua-kim
Copy link
Contributor Author

joshua-kim commented Aug 3, 2023

I went with Of because I'm used to Guava (java framework)'s ImmutableSet.of(1, 2, 3, 4, 5) syntax. If people feel like FromList or From or With (or other alternatives?) is better we can do that is well but they all seem pretty similar sounding to me 👍

Copy link
Contributor

@StephenButtolph StephenButtolph left a comment

Choose a reason for hiding this comment

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

Can we remove the unnecessary type annotations?

Copy link
Contributor

@StephenButtolph StephenButtolph left a comment

Choose a reason for hiding this comment

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

small nits

joshua-kim and others added 2 commits August 4, 2023 01:54
Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>
Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>
joshua-kim and others added 3 commits August 4, 2023 02:41
Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>
@StephenButtolph StephenButtolph merged commit 1bb3a00 into ava-labs:dev Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants