moby/internal/sliceutil/sliceutil.go
Sebastiaan van Stijn c7b3cb101b
Merge pull request #47213 from thaJeztah/more_gocompat
add more //go:build directives to prevent downgrading to go1.16 language
2024-01-25 14:37:29 +01:00

34 lines
729 B
Go

// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.19
package sliceutil
func Dedup[T comparable](slice []T) []T {
keys := make(map[T]struct{})
out := make([]T, 0, len(slice))
for _, s := range slice {
if _, ok := keys[s]; !ok {
out = append(out, s)
keys[s] = struct{}{}
}
}
return out
}
func Map[S ~[]In, In, Out any](s S, fn func(In) Out) []Out {
res := make([]Out, len(s))
for i, v := range s {
res[i] = fn(v)
}
return res
}
func Mapper[In, Out any](fn func(In) Out) func([]In) []Out {
return func(s []In) []Out {
res := make([]Out, len(s))
for i, v := range s {
res[i] = fn(v)
}
return res
}
}