lambda/map.go

40 lines
690 B
Go
Raw Permalink Normal View History

2022-03-26 19:16:31 +00:00
package lambda
2022-09-03 04:08:36 +00:00
func MapNil[T any](xs []T, fx func(T)) {
for _, v := range xs {
fx(v)
}
}
2022-03-26 19:16:31 +00:00
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
}