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

add InsertFunc method #16

Closed shoenig closed 1 year ago

shoenig commented 1 year ago

Sometimes I have a []T, but want to create (e.g.) a set from a particular field of T, type S.

Currently have to manually loop and Insert(S), would be nice if we could InsertFunc(f(T)S) instead.

shoenig commented 1 year ago

This doesn't really work as of Go 1.19 at least, because Go does not support generic methods using types not declared on the struct, e.g.

func(s *Set[T]) InsertAllFunc(items []E, f func(element E) T ) - no way to declare type E.

shoenig commented 1 year ago

Closed by #56