lambda/map.go

34 lines
611 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
}
2022-03-26 22:29:43 +00:00
func MapV[T, V any](xs []T, fx func(T) V) []V {
ov := make([]V, len(xs))
2022-03-26 19:16:31 +00:00
for i, v := range xs {
ov[i] = fx(v)
}
2022-03-26 22:29:43 +00:00
return ov
2022-03-26 19:16:31 +00:00
}
2022-03-26 22:29:43 +00:00
func MapError[T any](xs []T, fx func(T) (T, error)) ([]T, []error) {
oe := make([]error, len(xs))
2022-03-26 19:16:31 +00:00
for i, v := range xs {
xs[i], oe[i] = fx(v)
}
2022-03-26 22:29:43 +00:00
return xs, oe
2022-03-26 19:16:31 +00:00
}
2022-03-26 22:29:43 +00:00
func MapErrorV[T, V any](xs []T, fx func(T) (V, error)) ([]V, []error) {
ov := make([]V, len(xs))
oe := make([]error, len(xs))
2022-03-26 19:16:31 +00:00
for i, v := range xs {
ov[i], oe[i] = fx(v)
}
2022-03-26 22:29:43 +00:00
return ov, oe
2022-03-26 19:16:31 +00:00
}