feat: reducer, but doesn`t work
This commit is contained in:
45
pkg/set/set.go
Normal file
45
pkg/set/set.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package set
|
||||
|
||||
type Set[T comparable] map[T]bool
|
||||
|
||||
func (this *Set[T]) Add(items ...T) {
|
||||
for _, item := range items {
|
||||
(*this)[item] = true
|
||||
}
|
||||
}
|
||||
|
||||
func (this Set[T]) Has(item T) bool {
|
||||
return this[item] == true
|
||||
}
|
||||
|
||||
func (this *Set[T]) Remove(items ...T) {
|
||||
for _, item := range items {
|
||||
delete(*this, item)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Set[T]) Union(o Set[T]) {
|
||||
for item := range o {
|
||||
this.Add(item)
|
||||
}
|
||||
}
|
||||
|
||||
func (this Set[T]) ToList() []T {
|
||||
list := []T{}
|
||||
|
||||
for item := range this {
|
||||
list = append(list, item)
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
func New[T comparable](items ...T) Set[T] {
|
||||
result := Set[T]{}
|
||||
|
||||
for _, item := range items {
|
||||
result.Add(item)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user