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

feat: add Minus func for set

This commit is contained in:
dudaodong
2022-04-01 16:44:25 +08:00
parent 79867e8a63
commit f28b5b2f92
2 changed files with 24 additions and 0 deletions

View File

@@ -103,3 +103,16 @@ func (s Set[T]) Intersection(other Set[T]) Set[T] {
return set
}
// Minus creates an set of whose element in origin set but not in compared set
func (s Set[T]) Minus(comparedSet Set[T]) Set[T] {
set := NewSet[T]()
s.Iterate(func(value T) {
if !comparedSet.Contain(value) {
set.Add(value)
}
})
return set
}

View File

@@ -127,3 +127,14 @@ func TestSet_Intersection(t *testing.T) {
assert.Equal(expected, intersectionSet)
}
func TestSet_Minus(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_Intersection")
set1 := NewSet(1, 2, 3)
set2 := NewSet(2, 3, 4, 5)
set3 := NewSet(2, 3)
assert.Equal(NewSet(1), set1.Minus(set2))
assert.Equal(NewSet(4, 5), set2.Minus(set3))
}