Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maps.ToKeys method #30

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@ package maps

// ToValues returns an array of just the values of the input Map.
func ToValues[In any, Key comparable](arr map[Key]In) []In {
out := make([]In, 0, len(arr))
for _, elem := range arr {
out = append(out, elem)
i := 0
out := make([]In, len(arr))
for _, value := range arr {
out[i] = value
i++
}
return out
}

// ToSlice applies the function to each element of the input map, and returns an
// array of the results.
func ToSlice[M ~map[K]V, K comparable, V, R any](items M, f func(K, V) R) []R {
out := make([]R, 0, len(items))
i := 0
out := make([]R, len(items))
for k, v := range items {
out = append(out, f(k, v))
out[i] = f(k, v)
i++
}
return out
}

func ToKeys[In any, Key comparable](arr map[Key]In) []Key {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add the docs here, so our Go Packages page looks nice.

BTW the deprecated golint used to check for that, but the modern staticcheck only has checks for the wording of docs of exported functions, methods, variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man I totally didn't think to update the docs or the examples. Good call, thank you!

i := 0
out := make([]Key, len(arr))
for key := range arr {
out[i] = key
i++
}
return out
}
29 changes: 27 additions & 2 deletions maps/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@ const benchmarkArrayLength = 10_000

var benchResult []int

func TestToKeys(t *testing.T) {
t.Run(
"String Keys", func(t *testing.T) {
test := map[string]int{
"one": 1,
"two": 2,
"three": 3,
}
keys := ToKeys(test)
require.ElementsMatch(t, []string{"one", "two", "three"}, keys)
},
)
t.Run(
"Int Keys", func(t *testing.T) {
test := map[int]string{
1: "one",
2: "two",
3: "three",
}
keys := ToKeys(test)
require.ElementsMatch(t, []int{1, 2, 3}, keys)
},
)
}

func TestToValues(t *testing.T) {
nums := map[string]int{
"one": 1,
Expand Down Expand Up @@ -44,7 +69,7 @@ func TestToSlice(t *testing.T) {
func BenchmarkToValues(b *testing.B) {
arr := make(map[int]int, benchmarkArrayLength)
for i := range arr {
arr[i] = rand.Int()
arr[i] = rand.Int() //nolint:gosec
}

var r []int
Expand All @@ -57,7 +82,7 @@ func BenchmarkToValues(b *testing.B) {
func BenchmarkToSlice(b *testing.B) {
arr := make(map[int]int, benchmarkArrayLength)
for i := range arr {
arr[i] = rand.Int()
arr[i] = rand.Int() //nolint:gosec
}
var r []int
for i := 0; i < b.N; i++ {
Expand Down