1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add EachWithBreak for set

This commit is contained in:
dudaodong
2023-03-20 10:49:14 +08:00
parent 0b80074bb7
commit 70d0adde42
4 changed files with 97 additions and 0 deletions

View File

@@ -171,3 +171,13 @@ func (s Set[T]) Minus(comparedSet Set[T]) Set[T] {
return set
}
// EachWithBreak iterates over elements of a set and invokes function for each element,
// when iteratee return false, will break the for each loop.
func (s Set[T]) EachWithBreak(iteratee func(item T) bool) {
for v := range s {
if !iteratee(v) {
break
}
}
}

View File

@@ -192,3 +192,20 @@ func TestSet_Minus(t *testing.T) {
assert.Equal(NewSet(1), set1.Minus(set2))
assert.Equal(NewSet(4, 5), set2.Minus(set3))
}
func TestEachWithBreak(t *testing.T) {
s := NewSet(1, 2, 3, 4, 5)
var sum int
s.EachWithBreak(func(n int) bool {
if n > 3 {
return false
}
sum += n
return true
})
assert := internal.NewAssert(t, "TestEachWithBreak")
assert.Equal(6, sum)
}