Skip to content

Commit

Permalink
from slice
Browse files Browse the repository at this point in the history
  • Loading branch information
Allyson-English committed Aug 15, 2024
1 parent 4f542a0 commit edb39a8
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions set/set.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package set

// Set is a set of elements.
type Set[A comparable] map[A]struct{}

// New creates a new set.
func New[A comparable]() Set[A] {
return make(Set[A])
}

// FromSlice creates a new set from a slice.
func FromSlice[A comparable](as ...A) Set[A] {
s := make(Set[A])
for _, a := range as {
s.Add(a)
}
return s
}

// Add adds an element to the set.
func (s Set[A]) Add(a A) {
s[a] = struct{}{}
}

// Contains checks if an element is in the set.
func (s Set[A]) Contains(a A) bool {
_, ok := s[a]
return ok
Expand Down

0 comments on commit edb39a8

Please sign in to comment.