This commit is contained in:
Andrey Parhomenko 2022-11-25 19:12:25 +05:00
parent df52a9102d
commit 0a23c4a5a4
2 changed files with 6 additions and 6 deletions

View file

@ -30,8 +30,8 @@ func main() {
fmt.Println(m)
fmt.Println(slicex.MakeMap(
s,
func(v Struct) string {
return v.Name
func(s []Struct, i int) string {
return s[i].Name
},
))

View file

@ -2,14 +2,14 @@ package slicex
func MakeMap[K comparable, V any](
values []V,
fn func(V) (K),
fn func([]V, int) (K),
) map[K] V {
var k K
r := make(map[K] V)
for _, v := range values {
k = fn(v)
r[k] = v
for i, _ := range values {
k = fn(values, i)
r[k] = values[i]
}
return r