mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 18:22:27 +08:00
feat: add ForEach func
This commit is contained in:
@@ -38,3 +38,10 @@ func Merge[K comparable, V any](maps ...map[K]V) map[K]V {
|
|||||||
|
|
||||||
return res
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -62,3 +62,22 @@ func TestMerge(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(expected, acturl)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user