github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/x/mapx/set.go (about)

     1  package mapx
     2  
     3  import "github.com/pkg/errors"
     4  
     5  type Set[K comparable] map[K]bool
     6  
     7  func ToSet[K comparable](vals []K, conv func(K) K) (s Set[K], err error) {
     8  	s = make(Set[K])
     9  	for _, v := range vals {
    10  		if _, ok := s[v]; ok && err == nil {
    11  			err = ErrConflict
    12  		}
    13  		if conv != nil {
    14  			s[conv(v)] = true
    15  		} else {
    16  			s[v] = true
    17  		}
    18  	}
    19  	return
    20  }
    21  
    22  var ErrConflict = errors.Errorf("slice elements exsit conflict(s)")