-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from votdev/issue_4282_map_slice
Add several map and slice functions
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package gocommon | ||
|
||
// MapFilterFunc iterates over elements of map, returning a map of all elements | ||
// the function f returns truthy for. The function f is invoked with three | ||
// arguments: (value, key). | ||
func MapFilterFunc[M ~map[K]V, K comparable, V any](m M, f func(V, K) bool) map[K]V { | ||
r := make(map[K]V, len(m)) | ||
for k, v := range m { | ||
if f(v, k) { | ||
r[k] = v | ||
} | ||
} | ||
return r | ||
} | ||
|
||
// MapKeys returns the keys of the map m. The keys will be in an indeterminate | ||
// order. | ||
func MapKeys[M ~map[K]V, K comparable, V any](m M) []K { | ||
keys := make([]K, 0, len(m)) | ||
for k := range m { | ||
keys = append(keys, k) | ||
} | ||
return keys | ||
} | ||
|
||
// MapValues returns the values of the map m. The values will be in an | ||
// indeterminate order. | ||
func MapValues[M ~map[K]V, K comparable, V any](m M) []V { | ||
values := make([]V, 0, len(m)) | ||
for _, v := range m { | ||
values = append(values, v) | ||
} | ||
return values | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package gocommon | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_MapFilterFunc_1(t *testing.T) { | ||
result := MapFilterFunc(map[int]bool{1: true, 2: false, 3: true}, func(v bool, _ int) bool { | ||
return v == true | ||
}) | ||
assert.Equal(t, map[int]bool{1: true, 3: true}, result) | ||
} | ||
|
||
func Test_MapFilterFunc_2(t *testing.T) { | ||
result := MapFilterFunc(map[string]int{"a": 1, "b": 2, "c": 3, "d": 4}, func(v int, _ string) bool { | ||
return v <= 3 | ||
}) | ||
assert.Equal(t, map[string]int{"a": 1, "b": 2, "c": 3}, result) | ||
} | ||
|
||
func Test_MapKeys_1(t *testing.T) { | ||
result := MapKeys(map[string]string{"a": "1", "b": "2"}) | ||
assert.ElementsMatch(t, []string{"a", "b"}, result) | ||
} | ||
|
||
func Test_MapKeys_2(t *testing.T) { | ||
result := MapKeys(map[int]bool{1: true, 2: false}) | ||
assert.ElementsMatch(t, []int{1, 2}, result) | ||
} | ||
|
||
func Test_MapValues_1(t *testing.T) { | ||
result := MapValues(map[string]string{"a": "1", "b": "2"}) | ||
assert.ElementsMatch(t, []string{"1", "2"}, result) | ||
} | ||
|
||
func Test_MapValues_2(t *testing.T) { | ||
result := MapValues(map[int]bool{1: true, 2: false}) | ||
assert.ElementsMatch(t, []bool{true, false}, result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters