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

Already on GitHub? Sign in to your account

Non-mutating helpers to add/remove #128

Open
Frassle opened this issue Sep 16, 2023 · 2 comments
Open

Non-mutating helpers to add/remove #128

Frassle opened this issue Sep 16, 2023 · 2 comments

Comments

@Frassle
Copy link
Contributor

Frassle commented Sep 16, 2023

I'm finding quite a bit of code needs to do things like take a set and make a new set but with one extra element, or with one element removed. This quite often needs repeating with small variations. For example we're looking at using a Set[string] for the languages to run tests against, but some tests need to remove one or more languages. So what I'm writing helpers so that I can do

{ test: "1", languages: Remove(allLanguages, "go", "dotnet") },
{ test: "2", languages: Remove(allLanguages, "python") },

Feels like it might make sense to put something like this on set itself so I could do allLanguages.TBD("go").
Technically I don't think these methods are hard to write, but I'm a bit torn on two designs regarding names.

  1. Is to just add something like UnionWith(vals ...T) Set[T], but then it feels like maybe we should also do Intersect and SymmetricDifference, and then maybe the IsSub/Superset methods as well, and all of that feels maybe too busy. Although has some nice symmetry in that python allows any iterable for these methods.
  2. Is to name it something like Add/Remove to keep it limited to just a non-mutating adding and removing. But I'm not sure what's a good name for this, maybe With and Without.
@deckarep
Copy link
Owner

deckarep commented Dec 10, 2023

Hi @Frassle,

The issue you proposed has crossed my mind before because I think the Python implementation of the set also has the ability to do either mutating or non-mutating transforms. I'm still thinking about this however because I don't know if the demand for such an API is worth it to increase the surface complexity of the API.

Since the Python version has operator overloading it makes doing this pretty straightforward but for Go it just means many more methods which might start getting unwieldy.

I'll leave this ticket open if others want to chime in.

@Frassle
Copy link
Contributor Author

Frassle commented Dec 10, 2023

Since the Python version has operator overloading it makes doing this pretty straightforward but for Go it just means many more methods which might start getting unwieldy.

Yeh was thinking it might make sense as top-level methods but they don't chain nicely, and not sure what's a good name for instance methods given there's already Add, Append, Remove, RemoveAll. But it might be worth having:

func Add[T comparable](s Set[T], values ...T) Set[T] {
	n := s.Clone()
	n.Append(values...)
	return n
}

func Remove[T comparable](s Set[T], values ...T) Set[T] {
	n := s.Clone()
	n.RemoveAll(values...)
	return n
}

Given that it's simple and then that boilerplate is just handled for anyone using this but it's really not as nice to use as python operators.

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

No branches or pull requests

2 participants