Skip to content

Support 1.23 iterator for set #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ jobs:
test:
strategy:
matrix:
go-version: [1.20.1, 1.21.1]
go-version: [1.20.x, 1.21.x, 1.22.x, 1.23.0-rc.2]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
stable: false
- name: Checkout code
uses: actions/checkout@v3
- name: Test
run: |
go test -v -race ./...
# go vet ./...
# go test -bench=.
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v4
- name: Test
run: |
go test -v -race ./...
# go vet ./...
# go test -bench=.
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")
}
}
Loading