Skip to content

Commit

Permalink
Merge pull request #33 from thetnaingtn/master
Browse files Browse the repository at this point in the history
Add KeyBy
  • Loading branch information
samber committed Mar 12, 2022
2 parents ecde147 + 640f087 commit 0a9e574
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Supported helpers for slices:
- Reverse
- Fill
- Repeat
- ToMap
- KeyBy
- Drop
- DropRight
- DropWhile
Expand Down Expand Up @@ -405,15 +405,28 @@ initializedSlice := lo.Repeat[foo](2, foo{"a"})
// []foo{foo{"a"}, foo{"a"}}
```

### ToMap
### KeyBy

Transforms a slice or an array of structs to a map based on a pivot callback.

```go
m := lo.ToMap[int, string]([]string{"a", "aa", "aaa"}, func(str string) int {
m := lo.KeyBy[int, string]([]string{"a", "aa", "aaa"}, func(str string) int {
return len(str)
})
// map[int]string{1: "a", 2: "aa", 3: "aaa"}

type Character struct {
dir string
code int
}
characters := []Character{
{dir: "left", code: 97},
{dir: "right", code: 100},
}
result := KeyBy[Character, string](characters, func(char Character) string {
return string(rune(char.code))
})
//map[a:{dir:left code:97} d:{dir:right code:100}]
```

### Drop
Expand Down Expand Up @@ -849,20 +862,22 @@ iter, err := lo.Attempt(0, func(i int) error {
```

### Range / RangeFrom / RangeWithSteps

Creates an array of numbers (positive and/or negative) progressing from start up to, but not including end.

```go
result := Range(4)
// [0, 1, 2, 3]

result := Range(-4);
// [0, -1, -2, -3]

result := RangeFrom(1, 5);
// [1, 2, 3, 4]

result := RangeFrom[float64](1.0, 5);
// [1.0, 2.0, 3.0, 4.0]

result := RangeWithSteps(0, 20, 5);
// [0, 5, 10, 15]

Expand Down Expand Up @@ -915,7 +930,6 @@ ok github.com/samber/lo 6.657s
- `lop.Map` is slower than `lo.Map` because it implies more memory allocation and locks. `lop.Map` will be usefull for long-running callbacks, such as i/o bound processing.
- `for` beats other implementations for memory and CPU.


## 馃 Contributing

- Ping me on twitter [@samuelberthe](https://twitter.com/samuelberthe) (DMs, mentions, whatever :))
Expand Down
2 changes: 1 addition & 1 deletion map.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ func MapValues[K comparable, V any, R any](in map[K]V, iteratee func(V, K) R) ma
}

return result
}
}
4 changes: 2 additions & 2 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ func Repeat[T Clonable[T]](count int, initial T) []T {
return result
}

// ToMap transforms a slice or an array of structs to a map based on a pivot callback.
func ToMap[K comparable, V any](collection []V, iteratee func(V) K) map[K]V {
// KeyBy transforms a slice or an array of structs to a map based on a pivot callback.
func KeyBy[K comparable, V any](collection []V, iteratee func(V) K) map[K]V {
result := make(map[K]V, len(collection))

for _, v := range collection {
Expand Down
4 changes: 2 additions & 2 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ func TestRepeat(t *testing.T) {
is.Equal(result2, []foo{})
}

func TestToMap(t *testing.T) {
func TestKeyBy(t *testing.T) {
is := assert.New(t)

result1 := ToMap[int, string]([]string{"a", "aa", "aaa"}, func(str string) int {
result1 := KeyBy[int, string]([]string{"a", "aa", "aaa"}, func(str string) int {
return len(str)
})

Expand Down

0 comments on commit 0a9e574

Please sign in to comment.