Skip to content

Commit 0d26f4c

Browse files
authored
Support 1.23 iterator for set (#140)
* ci: add 1.22 and 1.23.0-rc2 and upgrade action version * Add Values function return 1.23 iterator * ci: add go versions in ci to 1.23.x and 1.24.x * Rename Values to Elements Use the name "Element" as it is more intuitive.
1 parent e1dc81f commit 0d26f4c

File tree

4 files changed

+92
-13
lines changed

4 files changed

+92
-13
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-version: [1.20.1, 1.21.1]
7+
go-version: [1.20.x, 1.21.x, 1.22.x, 1.23.x, 1.24.x]
88
os: [ubuntu-latest, macos-latest, windows-latest]
99
runs-on: ${{ matrix.os }}
1010
steps:
11-
- name: Install Go
12-
uses: actions/setup-go@v3
13-
with:
14-
go-version: ${{ matrix.go-version }}
15-
stable: false
16-
- name: Checkout code
17-
uses: actions/checkout@v3
18-
- name: Test
19-
run: |
20-
go test -v -race ./...
21-
# go vet ./...
22-
# go test -bench=.
11+
- name: Install Go
12+
uses: actions/setup-go@v5
13+
with:
14+
go-version: ${{ matrix.go-version }}
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
- name: Test
18+
run: |
19+
go test -v -race ./...
20+
# go vet ./...
21+
# go test -bench=.

set.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,13 @@ func NewThreadUnsafeSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] {
257257

258258
return s
259259
}
260+
261+
// Elements returns an iterator that yields the elements of the set. Starting
262+
// with Go 1.23, users can use a for loop to iterate over it.
263+
func Elements[T comparable](s Set[T]) func(func(element T) bool) {
264+
return func(yield func(element T) bool) {
265+
s.Each(func(t T) bool {
266+
return !yield(t)
267+
})
268+
}
269+
}

set123_test.go

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

set_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,38 @@ func Test_NewThreadUnsafeSetFromMapKey_Strings(t *testing.T) {
13731373
}
13741374
}
13751375

1376+
func Test_Elements(t *testing.T) {
1377+
a := NewSet[string]()
1378+
1379+
a.Add("Z")
1380+
a.Add("Y")
1381+
a.Add("X")
1382+
a.Add("W")
1383+
1384+
b := NewSet[string]()
1385+
Elements(a)(func(elem string) bool {
1386+
b.Add(elem)
1387+
return true
1388+
})
1389+
1390+
if !a.Equal(b) {
1391+
t.Error("The sets are not equal after iterating (Each) through the first set")
1392+
}
1393+
1394+
var count int
1395+
Elements(a)(func(elem string) bool {
1396+
if count == 2 {
1397+
return false
1398+
}
1399+
count++
1400+
return true
1401+
})
1402+
1403+
if count != 2 {
1404+
t.Error("Iteration should stop on the way")
1405+
}
1406+
}
1407+
13761408
func Test_Example(t *testing.T) {
13771409
/*
13781410
requiredClasses := NewSet()

0 commit comments

Comments
 (0)