mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
feat: add EachWithBreak for set
This commit is contained in:
@@ -171,3 +171,13 @@ func (s Set[T]) Minus(comparedSet Set[T]) Set[T] {
|
|||||||
|
|
||||||
return set
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -192,3 +192,20 @@ func TestSet_Minus(t *testing.T) {
|
|||||||
assert.Equal(NewSet(1), set1.Minus(set2))
|
assert.Equal(NewSet(1), set1.Minus(set2))
|
||||||
assert.Equal(NewSet(4, 5), set2.Minus(set3))
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import (
|
|||||||
- [Size](#Size)
|
- [Size](#Size)
|
||||||
- [Equal](#Equal)
|
- [Equal](#Equal)
|
||||||
- [Iterate](#Iterate)
|
- [Iterate](#Iterate)
|
||||||
|
- [EachWithBreak](#EachWithBreak)
|
||||||
- [IsEmpty](#IsEmpty)
|
- [IsEmpty](#IsEmpty)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
- [Intersection](#Intersection)
|
- [Intersection](#Intersection)
|
||||||
@@ -428,6 +429,40 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EachWithBreak">EachWithBreak</span>
|
||||||
|
<p>Iterates over elements of a set and invokes function for each element, when iteratee return false, will break the for each loop.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s Set[T]) EachWithBreak(iteratee func(item T) bool)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
set "github.com/duke-git/lancet/v2/datastructure/set"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s := set.NewSet(1, 2, 3, 4, 5)
|
||||||
|
|
||||||
|
var sum int
|
||||||
|
|
||||||
|
s.EachWithBreak(func(n int) bool {
|
||||||
|
if n > 3 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
sum += n
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(sum) //6
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="IsEmpty">IsEmpty</span>
|
### <span id="IsEmpty">IsEmpty</span>
|
||||||
<p>Check if the set is empty or not</p>
|
<p>Check if the set is empty or not</p>
|
||||||
|
|||||||
@@ -430,6 +430,41 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EachWithBreak">EachWithBreak</span>
|
||||||
|
<p>遍历集合的元素并为每个元素调用iteratee函数,当iteratee函数返回false时,终止遍历。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s Set[T]) EachWithBreak(iteratee func(item T) bool)
|
||||||
|
```
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
set "github.com/duke-git/lancet/v2/datastructure/set"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s := set.NewSet(1, 2, 3, 4, 5)
|
||||||
|
|
||||||
|
var sum int
|
||||||
|
|
||||||
|
s.EachWithBreak(func(n int) bool {
|
||||||
|
if n > 3 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
sum += n
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(sum) //6
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsEmpty">IsEmpty</span>
|
### <span id="IsEmpty">IsEmpty</span>
|
||||||
<p>判断集合是否为空</p>
|
<p>判断集合是否为空</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user