Skip to content

Commit 24f21dd

Browse files
committed
Rename Values to Elements
Use the name "Element" as it is more intuitive.
1 parent aab5b00 commit 24f21dd

File tree

3 files changed

+41
-37
lines changed

3 files changed

+41
-37
lines changed

set.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ func NewThreadUnsafeSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] {
254254
return s
255255
}
256256

257-
func Values[T comparable](s Set[T]) func(func(element T) bool) {
257+
// Elements returns an iterator that yields the elements of the set. Starting
258+
// with Go 1.23, users can use a for loop to iterate over it.
259+
func Elements[T comparable](s Set[T]) func(func(element T) bool) {
258260
return func(yield func(element T) bool) {
259261
s.Each(func(t T) bool {
260262
return !yield(t)

set123_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
package mapset
55

6-
import "testing"
6+
import (
7+
"testing"
8+
)
79

8-
func TestAll123(t *testing.T) {
10+
func Test_Elements123(t *testing.T) {
911
a := NewSet[string]()
1012

1113
a.Add("Z")
@@ -14,7 +16,7 @@ func TestAll123(t *testing.T) {
1416
a.Add("W")
1517

1618
b := NewSet[string]()
17-
for elem := range Values(a) {
19+
for elem := range Elements(a) {
1820
b.Add(elem)
1921
}
2022

@@ -23,7 +25,7 @@ func TestAll123(t *testing.T) {
2325
}
2426

2527
var count int
26-
for range Values(a) {
28+
for range Elements(a) {
2729
if count == 2 {
2830
break
2931
}

set_test.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,38 @@ func Test_NewThreadUnsafeSetFromMapKey_Strings(t *testing.T) {
13461346
}
13471347
}
13481348

1349+
func Test_Elements(t *testing.T) {
1350+
a := NewSet[string]()
1351+
1352+
a.Add("Z")
1353+
a.Add("Y")
1354+
a.Add("X")
1355+
a.Add("W")
1356+
1357+
b := NewSet[string]()
1358+
Elements(a)(func(elem string) bool {
1359+
b.Add(elem)
1360+
return true
1361+
})
1362+
1363+
if !a.Equal(b) {
1364+
t.Error("The sets are not equal after iterating (Each) through the first set")
1365+
}
1366+
1367+
var count int
1368+
Elements(a)(func(elem string) bool {
1369+
if count == 2 {
1370+
return false
1371+
}
1372+
count++
1373+
return true
1374+
})
1375+
1376+
if count != 2 {
1377+
t.Error("Iteration should stop on the way")
1378+
}
1379+
}
1380+
13491381
func Test_Example(t *testing.T) {
13501382
/*
13511383
requiredClasses := NewSet()
@@ -1386,35 +1418,3 @@ func Test_Example(t *testing.T) {
13861418
fmt.Println(allClasses.ContainsAll("Welding", "Automotive", "English"))
13871419
*/
13881420
}
1389-
1390-
func TestAll(t *testing.T) {
1391-
a := NewSet[string]()
1392-
1393-
a.Add("Z")
1394-
a.Add("Y")
1395-
a.Add("X")
1396-
a.Add("W")
1397-
1398-
b := NewSet[string]()
1399-
Values(a)(func(elem string) bool {
1400-
b.Add(elem)
1401-
return true
1402-
})
1403-
1404-
if !a.Equal(b) {
1405-
t.Error("The sets are not equal after iterating (Each) through the first set")
1406-
}
1407-
1408-
var count int
1409-
Values(a)(func(elem string) bool {
1410-
if count == 2 {
1411-
return false
1412-
}
1413-
count++
1414-
return true
1415-
})
1416-
1417-
if count != 2 {
1418-
t.Error("Iteration should stop on the way")
1419-
}
1420-
}

0 commit comments

Comments
 (0)