diff --git a/set.go b/set.go index 292089d..dadadb8 100644 --- a/set.go +++ b/set.go @@ -253,3 +253,11 @@ func NewThreadUnsafeSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] { return s } + +func Values[T comparable](s Set[T]) func(func(element T) bool) { + return func(yield func(element T) bool) { + s.Each(func(t T) bool { + return !yield(t) + }) + } +} diff --git a/set123_test.go b/set123_test.go new file mode 100644 index 0000000..1102399 --- /dev/null +++ b/set123_test.go @@ -0,0 +1,36 @@ +//go:build go1.23 +// +build go1.23 + +package mapset + +import "testing" + +func TestAll123(t *testing.T) { + a := NewSet[string]() + + a.Add("Z") + a.Add("Y") + a.Add("X") + a.Add("W") + + b := NewSet[string]() + for elem := range Values(a) { + b.Add(elem) + } + + if !a.Equal(b) { + t.Error("The sets are not equal after iterating (Each) through the first set") + } + + var count int + for range Values(a) { + if count == 2 { + break + } + count++ + } + + if count != 2 { + t.Error("Iteration should stop on the way") + } +} diff --git a/set_test.go b/set_test.go index a21153d..9b41116 100644 --- a/set_test.go +++ b/set_test.go @@ -1386,3 +1386,35 @@ func Test_Example(t *testing.T) { fmt.Println(allClasses.ContainsAll("Welding", "Automotive", "English")) */ } + +func TestAll(t *testing.T) { + a := NewSet[string]() + + a.Add("Z") + a.Add("Y") + a.Add("X") + a.Add("W") + + b := NewSet[string]() + Values(a)(func(elem string) bool { + b.Add(elem) + return true + }) + + if !a.Equal(b) { + t.Error("The sets are not equal after iterating (Each) through the first set") + } + + var count int + Values(a)(func(elem string) bool { + if count == 2 { + return false + } + count++ + return true + }) + + if count != 2 { + t.Error("Iteration should stop on the way") + } +}