github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/isc/flat.go (about) 1 package isc 2 3 func ListFlatMap[T any, R any](list []T, f func(T) []R) []R { 4 var r []R 5 for _, e := range list { 6 rlist := f(e) 7 for _, rl := range rlist { 8 r = append(r, rl) 9 } 10 } 11 return r 12 } 13 14 func ListFlatMapIndexed[T any, R any](list []T, f func(int, T) []R) []R { 15 var r []R 16 for i, e := range list { 17 rlist := f(i, e) 18 for _, rl := range rlist { 19 r = append(r, rl) 20 } 21 } 22 return r 23 } 24 25 func ListFlattern[T any](list [][]T) []T { 26 var r []T 27 for _, e := range list { 28 rlist := e 29 for _, rl := range rlist { 30 r = append(r, rl) 31 } 32 } 33 return r 34 } 35 36 func ListFlatMapTo[T any, R any](list []T, dest *[]R, f func(T) []R) []R { 37 var r []R 38 for _, e := range list { 39 rlist := f(e) 40 for _, rl := range rlist { 41 *dest = append(*dest, rl) 42 r = append(r, rl) 43 } 44 } 45 return r 46 } 47 48 func ListFlatMapIndexedTo[T any, R any](list []T, dest *[]R, f func(int, T) []R) []R { 49 var r []R 50 for i, e := range list { 51 rlist := f(i, e) 52 for _, rl := range rlist { 53 *dest = append(*dest, rl) 54 r = append(r, rl) 55 } 56 } 57 return r 58 } 59 60 func MapFlatMap[K comparable, V any, R any](m map[K]V, f func(K, V) []R) []R { 61 var r []R 62 for k, v := range m { 63 rlist := f(k, v) 64 for _, rl := range rlist { 65 r = append(r, rl) 66 } 67 } 68 return r 69 } 70 71 func MapFlatMapTo[K comparable, V any, R any](m map[K]V, dest *[]R, f func(K, V) []R) []R { 72 var r []R 73 for k, v := range m { 74 rlist := f(k, v) 75 for _, rl := range rlist { 76 *dest = append(*dest, rl) 77 r = append(r, rl) 78 } 79 } 80 return r 81 } 82