Skip to content

Commit

Permalink
adding alias lo.SliceToMap -> lo.Associate
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Jul 29, 2022
1 parent bccfe8c commit a4816ee
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Breaking:
Added:

- lo.ChunkString
- lo.SliceToMap (alias to lo.Associate)

## 1.26.0 (2022-07-24)

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Supported helpers for slices:
- [Repeat](#repeat)
- [RepeatBy](#repeatby)
- [KeyBy](#keyby)
- [Associate](#associate)
- [Associate / SliceToMap](#associate-alias-slicetomap)
- [Drop](#drop)
- [DropRight](#dropright)
- [DropWhile](#dropwhile)
Expand Down Expand Up @@ -545,11 +545,12 @@ result := lo.KeyBy[string, Character](characters, func(char Character) string {
//map[a:{dir:left code:97} d:{dir:right code:100}]
```

### Associate
### Associate (alias: SliceToMap)

Returns a map containing key-value pairs provided by transform function applied to elements of the given slice.
If any of two pairs would have the same key the last one gets added to the map.
The returned map preserves the entry iteration order of the original array.

The order of keys in returned map is not specified and is not guaranteed to be the same from the original array.

```go
in := []*foo{{baz: "apple", bar: 1}, {baz: "banana", bar: 2}},
Expand Down
10 changes: 9 additions & 1 deletion slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func KeyBy[K comparable, V any](collection []V, iteratee func(V) K) map[K]V {

// Associate returns a map containing key-value pairs provided by transform function applied to elements of the given slice.
// If any of two pairs would have the same key the last one gets added to the map.
// The returned map preserves the entry iteration order of the original array.
// The order of keys in returned map is not specified and is not guaranteed to be the same from the original array.
func Associate[T any, K comparable, V any](collection []T, transform func(T) (K, V)) map[K]V {
result := make(map[K]V)

Expand All @@ -286,6 +286,14 @@ func Associate[T any, K comparable, V any](collection []T, transform func(T) (K,
return result
}

// Associate returns a map containing key-value pairs provided by transform function applied to elements of the given slice.
// If any of two pairs would have the same key the last one gets added to the map.
// The order of keys in returned map is not specified and is not guaranteed to be the same from the original array.
// Alias of Associate().
func SliceToMap[T any, K comparable, V any](collection []T, transform func(T) (K, V)) map[K]V {
return Associate(collection, transform)
}

// Drop drops n elements from the beginning of a slice or array.
func Drop[T any](collection []T, n int) []T {
if len(collection) <= n {
Expand Down

0 comments on commit a4816ee

Please sign in to comment.