hashicorp / go-set

The go-set package provides generic Set implementations for Go, including HashSet for types with a Hash() function and TreeSet for orderable data
Mozilla Public License 2.0
118 stars 8 forks source link

set: add a FromFunc set constructor #6

Closed shoenig closed 2 years ago

shoenig commented 2 years ago

This PR adds FromFunc for conveniently constructing a set from a slice of objects, where we only care about some comparable aspect of the object.

e.g. - something like this

names := make([]string, 0, len(employees))
for _, employee := range employees {
    names = append(names, employee.name)
}

would become

set.FromFunc(employees, func(e employee) string {return e.Name} )