lo.Associate: improve doc

This commit is contained in:
Samuel Berthe
2022-08-01 00:58:43 +02:00
parent 350ee65573
commit 852f0eacfe
+21 -1
View File
@@ -563,7 +563,27 @@ in := []*foo{
aMap := lo.Associate[*foo, string, int](in, func (f *foo) (string, int) {
return f.baz, f.bar
})
// map[string][int]{ "apple":1, "banana":2 }
// map[string]int{
// "apple": 1,
// "banana": 2,
// }
```
Converting a slice into a map of keys, can increase lookup time.
```go
in := []string{"a", "b", "c", "a"}
aMap := lo.Associate[*foo, string, struct{}](in, func (item string) (string, struct{}) {
return item, struct{}{}
})
// map[string]int{
// "a": struct{}{},
// "b": struct{}{},
// "c": struct{}{},
// }
_, ok := aMap["b"]
```
### SliceToMap2 -> SliceToMap9