1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 15:12:26 +08:00

feat: add FlatMap

This commit is contained in:
dudaodong
2023-02-24 11:14:59 +08:00
parent 5767aad303
commit f3801bcd8f
3 changed files with 39 additions and 0 deletions

View File

@@ -438,6 +438,18 @@ func FilterMap[T any, U any](slice []T, iteratee func(index int, item T) (U, boo
return result
}
// FlatMap manipulates a slice and transforms and flattens it to a slice of another type.
// Play: todo
func FlatMap[T any, U any](slice []T, iteratee func(index int, item T) []U) []U {
result := make([]U, 0, len(slice))
for i, v := range slice {
result = append(result, iteratee(i, v)...)
}
return result
}
// Reduce creates an slice of values by running each element of slice thru iteratee function.
// Play: https://go.dev/play/p/_RfXJJWIsIm
func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T {