Skip to content

Commit d49072b

Browse files
committed
Add Values function return 1.23 iterator
1 parent 342941b commit d49072b

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

set.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,11 @@ func NewThreadUnsafeSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] {
253253

254254
return s
255255
}
256+
257+
func Values[T comparable](s Set[T]) func(func(element T) bool) {
258+
return func(yield func(element T) bool) {
259+
s.Each(func(t T) bool {
260+
return !yield(t)
261+
})
262+
}
263+
}

set123_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//go:build go1.23
2+
// +build go1.23
3+
4+
package mapset
5+
6+
import "testing"
7+
8+
func TestAll123(t *testing.T) {
9+
a := NewSet[string]()
10+
11+
a.Add("Z")
12+
a.Add("Y")
13+
a.Add("X")
14+
a.Add("W")
15+
16+
b := NewSet[string]()
17+
for elem := range Values(a) {
18+
b.Add(elem)
19+
}
20+
21+
if !a.Equal(b) {
22+
t.Error("The sets are not equal after iterating (Each) through the first set")
23+
}
24+
25+
var count int
26+
for range Values(a) {
27+
if count == 2 {
28+
break
29+
}
30+
count++
31+
}
32+
33+
if count != 2 {
34+
t.Error("Iteration should stop on the way")
35+
}
36+
}

set_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,3 +1386,35 @@ func Test_Example(t *testing.T) {
13861386
fmt.Println(allClasses.ContainsAll("Welding", "Automotive", "English"))
13871387
*/
13881388
}
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)