From edb39a849be21dcf89c4a03bdd2cad1b89839419 Mon Sep 17 00:00:00 2001 From: Allyson-English Date: Thu, 15 Aug 2024 11:14:29 -0400 Subject: [PATCH] from slice --- set/set.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/set/set.go b/set/set.go index b3e7c32..c250d42 100644 --- a/set/set.go +++ b/set/set.go @@ -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