Skip to content

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

Description

@sp71

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
})

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions