1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-07 06:02:27 +08:00

feat: add ForEach func

This commit is contained in:
dudaodong
2022-03-30 19:41:55 +08:00
parent 1a436aeb41
commit 3ad142d5d7
2 changed files with 26 additions and 0 deletions

View File

@@ -38,3 +38,10 @@ func Merge[K comparable, V any](maps ...map[K]V) map[K]V {
return res
}
// ForEach executes iteratee funcation for every key and value pair in map
func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V)) {
for k, v := range m {
iteratee(k, v)
}
}

View File

@@ -62,3 +62,22 @@ func TestMerge(t *testing.T) {
assert.Equal(expected, acturl)
}
func TestForEach(t *testing.T) {
assert := internal.NewAssert(t, "TestForEach")
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
}
var sum int
ForEach(m, func(_ string, value int) {
sum += value
})
assert.Equal(10, sum)
}