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

Feature Request: Support creating a set from a generic slice with a key selector function #147

Open
sp71 opened this issue Oct 2, 2024 · 0 comments

Comments

@sp71
Copy link

sp71 commented Oct 2, 2024

Request a new feature allowing creation a set from a generic slice, where a key selector function can be provided. This would be useful in cases where we have complex structures and want to generate sets based on specific fields or derived values.

Proposed Function Signature

The function could look something like this:

func NewFromSlice[E any, T comparable](vals []E, f func(e E) T) Set[T] {
    s := NewSet[T]()
    for _, v := range vals {
        s.Add(f(v))
    }
    return s
}

Usage

type Person struct {
    Name string
    Age  int
}

people := []Person{
    {Name: "Alice", Age: 30},
    {Name: "Bob", Age: 25},
}

namesSet := NewFromSlice(people, func(p Person) string {
    return p.Name
})
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

1 participant