Skip to content

Commit

Permalink
Add Values function return 1.23 iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
amikai committed Aug 2, 2024
1 parent 342941b commit d49072b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
8 changes: 8 additions & 0 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
36 changes: 36 additions & 0 deletions set123_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
32 changes: 32 additions & 0 deletions set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit d49072b

Please sign in to comment.