underscore/flatmap.go
Ruidy c87f3f88b7 add flatmap
Signed-off-by: Ruidy <rnemausat@newstore.com>
2022-08-10 16:57:00 +02:00

11 lines
375 B
Go

package underscore
// Flatmap flatten the input slice element into the new slice. FlatMap maps every element with the help of a mapper function, then flattens the input slice element into the new slice.
func Flatmap[T any](values []T, mapper func(n T) []T) []T {
res := make([]T, 0)
for _, v := range values {
vs := mapper(v)
res = append(res, vs...)
}
return res
}