lambda/map.go

34 lines
607 B
Go
Raw Normal View History

2022-03-26 19:16:31 +00:00
package lambda
func Map[T any](xs []T, fx func(T) T) []T {
for i, v := range xs {
xs[i] = fx(v)
}
return xs
}
func MapV[T, V any](xs []T, fx func(T) V) (ov []V) {
ov = make([]V, len(xs))
for i, v := range xs {
ov[i] = fx(v)
}
return
}
func MapError[T any](xs []T, fx func(T) (T, error)) (ov []T, oe []error) {
oe = make([]error, len(xs))
for i, v := range xs {
xs[i], oe[i] = fx(v)
}
return
}
func MapErrorV[T, V any](xs []T, fx func(T) (V, error)) (ov []V, oe []error) {
ov = make([]V, len(xs))
oe = make([]error, len(xs))
for i, v := range xs {
ov[i], oe[i] = fx(v)
}
return
}