blob: 943261f786a8c665e1e2ff0cda24a9b81cd8c855 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package slices
// Map applies given function to every value of slice
func Map[S ~[]T, T, M any](s S, fn func(T) M) []M {
if s == nil {
return []M(nil)
}
if len(s) == 0 {
return make([]M, 0)
}
res := make([]M, len(s))
for i, v := range s {
res[i] = fn(v)
}
return res
}
// Mutate is like Map, but it prohibits type changes and modifies original slice.
func Mutate[S ~[]T, T any](s S, fn func(T) T) S {
if len(s) == 0 {
return s
}
for i, v := range s {
s[i] = fn(v)
}
return s
}
|