-
Notifications
You must be signed in to change notification settings - Fork 3
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 #11 from lxzan/dev
simplify vector
- Loading branch information
Showing
5 changed files
with
281 additions
and
266 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,17 @@ | ||
package maps | ||
|
||
func Keys[K comparable, V any](m map[K]V) []K { | ||
var keys = make([]K, 0, len(m)) | ||
for k, _ := range m { | ||
keys = append(keys, k) | ||
} | ||
return keys | ||
} | ||
|
||
func Values[K comparable, V any](m map[K]V) []V { | ||
var 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,38 @@ | ||
package maps | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestKeys(t *testing.T) { | ||
t.Run("", func(t *testing.T) { | ||
var m = map[string]any{ | ||
"a": 1, | ||
"b": 1, | ||
"c": 1, | ||
} | ||
assert.ElementsMatch(t, Keys(m), []string{"a", "b", "c"}) | ||
}) | ||
|
||
t.Run("", func(t *testing.T) { | ||
type Map map[string]any | ||
var m = Map{ | ||
"a": 1, | ||
"b": 1, | ||
"c": 1, | ||
} | ||
assert.ElementsMatch(t, Keys(m), []string{"a", "b", "c"}) | ||
}) | ||
} | ||
|
||
func TestValues(t *testing.T) { | ||
t.Run("", func(t *testing.T) { | ||
var m = map[string]any{ | ||
"a": 1, | ||
"b": 2, | ||
"c": 3, | ||
} | ||
assert.ElementsMatch(t, Values(m), []int{1, 2, 3}) | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.