more generics

This commit is contained in:
Jesse Duffield
2022-03-19 16:34:46 +11:00
parent eda8f4a5d4
commit bf4f06ab4e
21 changed files with 303 additions and 198 deletions

View File

@@ -33,3 +33,21 @@ func TransformKeys[Key comparable, Value any, NewKey comparable](m map[Key]Value
}
return output
}
func MapToSlice[Key comparable, Value any, Mapped any](m map[Key]Value, f func(Key, Value) Mapped) []Mapped {
output := make([]Mapped, 0, len(m))
for key, value := range m {
output = append(output, f(key, value))
}
return output
}
func Filter[Key comparable, Value any](m map[Key]Value, f func(Key, Value) bool) map[Key]Value {
output := map[Key]Value{}
for key, value := range m {
if f(key, value) {
output[key] = value
}
}
return output
}

View File

@@ -19,13 +19,9 @@ func NewFromSlice[T comparable](slice []T) *Set[T] {
return &Set[T]{hashMap: hashMap}
}
func (s *Set[T]) Add(value T) {
s.hashMap[value] = true
}
func (s *Set[T]) AddSlice(slice []T) {
for _, value := range slice {
s.Add(value)
func (s *Set[T]) Add(values ...T) {
for _, value := range values {
s.hashMap[value] = true
}
}

2
vendor/modules.txt vendored
View File

@@ -120,7 +120,7 @@ github.com/integrii/flaggy
# github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99
## explicit
github.com/jbenet/go-context/io
# github.com/jesseduffield/generics v0.0.0-20220319042131-63614a800d5f
# github.com/jesseduffield/generics v0.0.0-20220319062156-fa5cb8bde518
## explicit; go 1.18
github.com/jesseduffield/generics/maps
github.com/jesseduffield/generics/set