From 70d0adde42302f3cafeba63110dac943e1c500f6 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 20 Mar 2023 10:49:14 +0800 Subject: [PATCH] feat: add EachWithBreak for set --- datastructure/set/set.go | 10 ++++++++++ datastructure/set/set_test.go | 17 ++++++++++++++++ docs/datastructure/set.md | 35 +++++++++++++++++++++++++++++++++ docs/datastructure/set_zh-CN.md | 35 +++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) diff --git a/datastructure/set/set.go b/datastructure/set/set.go index 771a032..8319144 100644 --- a/datastructure/set/set.go +++ b/datastructure/set/set.go @@ -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 + } + } +} diff --git a/datastructure/set/set_test.go b/datastructure/set/set_test.go index d16e354..be1de88 100644 --- a/datastructure/set/set_test.go +++ b/datastructure/set/set_test.go @@ -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) +} diff --git a/docs/datastructure/set.md b/docs/datastructure/set.md index 22ffe4b..c4f445f 100644 --- a/docs/datastructure/set.md +++ b/docs/datastructure/set.md @@ -34,6 +34,7 @@ import ( - [Size](#Size) - [Equal](#Equal) - [Iterate](#Iterate) +- [EachWithBreak](#EachWithBreak) - [IsEmpty](#IsEmpty) - [Union](#Union) - [Intersection](#Intersection) @@ -428,6 +429,40 @@ func main() { ``` +### EachWithBreak +

Iterates over elements of a set and invokes function for each element, when iteratee return false, will break the for each loop.

+ +Signature: + +```go +func (s Set[T]) EachWithBreak(iteratee func(item T) bool) +``` +Example: + +```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 +} +``` ### IsEmpty

Check if the set is empty or not

diff --git a/docs/datastructure/set_zh-CN.md b/docs/datastructure/set_zh-CN.md index d0af2c1..ac39619 100644 --- a/docs/datastructure/set_zh-CN.md +++ b/docs/datastructure/set_zh-CN.md @@ -430,6 +430,41 @@ func main() { ``` +### EachWithBreak +

遍历集合的元素并为每个元素调用iteratee函数,当iteratee函数返回false时,终止遍历。

+ +函数签名: + +```go +func (s Set[T]) EachWithBreak(iteratee func(item T) bool) +``` +示例: + +```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 +} +``` + ### IsEmpty

判断集合是否为空