Skip to content

Commit

Permalink
Add MapIter function (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiendc authored Oct 25, 2024
1 parent 68af6f2 commit 0e3972f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ go get github.com/tiendc/gofn
- [MapEqual / MapEqualBy](#mapequal--mapequalby)
- [MapContainKeys](#mapcontainkeys)
- [MapContainValues](#mapcontainvalues)
- [MapIter](#mapiter)
- [MapKeys](#mapkeys)
- [MapValues](#mapvalues)
- [MapEntries](#mapentries)
Expand Down Expand Up @@ -873,6 +874,14 @@ MapContainValues(map[int]int{1: 11, 2: 22}, 11, 22) // true
MapContainValues(map[int]int{1: 11, 2: 22}, 11, 33) // false
```

#### MapIter

Iterates over the entries of one or multiple maps.

```go
MapIter(func(k int, v string) { ... }, map[int]string{1: "11", 2: "22"}, map[int]string{3: "33", 4: "44"})
```

#### MapKeys

Gets all keys of a map.
Expand Down
9 changes: 9 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ func MapContainValues[K comparable, V comparable, M ~map[K]V](m M, values ...V)
return true
}

// MapIter iterates over entries of multiple maps
func MapIter[K comparable, V any, M ~map[K]V](iterFunc func(K, V), mapSlice ...M) {
for _, m := range mapSlice {
for k, v := range m {
iterFunc(k, v)
}
}
}

// MapKeys gets map keys as slice
func MapKeys[K comparable, V any, M ~map[K]V](m M) []K {
keys := make([]K, len(m))
Expand Down
23 changes: 23 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,29 @@ func Test_MapContainValues(t *testing.T) {
assert.False(t, MapContainValues(map[int]st{1: {1, "1"}, 2: {2, "2"}}, st{1, "1"}, st{2, "2"}, st{}))
}

func Test_MapIter(t *testing.T) {
// No input map
copy1 := map[int]int{}
MapIter[int, int, map[int]int](func(k int, v int) { copy1[k] = v })
assert.Equal(t, map[int]int{}, copy1)

// Empty input map
copy2 := map[int]int{}
MapIter(func(k int, v int) { copy2[k] = v }, map[int]int{})
assert.Equal(t, map[int]int{}, copy2)

// Single map input
copy3 := map[int]int{}
MapIter(func(k int, v int) { copy3[k] = v }, map[int]int{1: 11, 2: 22, 3: 33})
assert.Equal(t, map[int]int{1: 11, 2: 22, 3: 33}, copy3)

// Multiple maps input
copy4 := map[int]int{}
MapIter(func(k int, v int) { copy4[k] = v },
map[int]int{1: 11, 2: 22, 3: 33}, map[int]int{4: 44, 5: 55})
assert.Equal(t, map[int]int{1: 11, 2: 22, 3: 33, 4: 44, 5: 55}, copy4)
}

func Test_MapKeys(t *testing.T) {
assert.Equal(t, []int{}, MapKeys[int, bool, map[int]bool](nil))
assert.Equal(t, []int{}, MapKeys(map[int]bool{}))
Expand Down
2 changes: 1 addition & 1 deletion slice_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Iter[T any, S ~[]T](iterFunc func(index int, v T) bool, slices ...S) {
}

// IterPtr iterates over pointers to items from multiple slices with ability to stop.
// When the `iterFunc` function returns false, the iteration stops.
// When the `iterFunc` function returns false, the iterating stops.
func IterPtr[T any, S ~[]T](iterFunc func(index int, v *T) bool, slices ...S) {
global := 0
for _, s := range slices {
Expand Down

0 comments on commit 0e3972f

Please sign in to comment.