Documentation
¶
Overview ¶
Package maputil provides generic functional-style helpers for Go maps.
Problem ¶
Go's built-in map type is efficient and ergonomic, but common transformation patterns (filtering entries, remapping keys/values, reducing to an aggregate, or inverting key-value direction) are often rewritten ad hoc across projects. That leads to repetitive loops, inconsistent behavior, and subtle bugs around map iteration order.
Solution ¶
This package offers a small set of generic, allocation-conscious helpers:
- Filter keeps entries matching a predicate.
- Map transforms key/value pairs into a new map type.
- Reduce folds all entries into a single accumulator value.
- Invert swaps keys and values.
All functions are pure map-to-map/map-to-value transforms: they return new results and never mutate the input map directly.
Important Semantics ¶
Go map iteration order is intentionally randomized. Therefore:
- Reduce results are deterministic only when the reducing function is order-independent (for example, commutative/associative operations).
- Map and Invert follow "last write wins" semantics when multiple input entries map to the same output key.
Benefits ¶
These utilities remove repetitive boilerplate while preserving type safety, making map-heavy code easier to read, review, and test.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Filter ¶
func Filter[M ~map[K]V, K comparable, V any](m M, f func(K, V) bool) M
Filter returns new map containing only entries where predicate f returns true; input map is not modified.
Example ¶
package main
import (
"fmt"
"github.com/tecnickcom/nurago/pkg/maputil"
)
func main() {
m := map[int]string{0: "Hello", 1: "World"}
filterFn := func(_ int, v string) bool { return v == "World" }
s2 := maputil.Filter(m, filterFn)
fmt.Println(s2)
}
Output: map[1:World]
func Invert ¶
func Invert[M ~map[K]V, K, V comparable](m M) map[V]K
Invert returns new map with swapped keys/values, with last-write-wins semantics for duplicate input values.
Example ¶
package main
import (
"fmt"
"github.com/tecnickcom/nurago/pkg/maputil"
)
func main() {
m := map[int]int{1: 10, 2: 20}
s2 := maputil.Invert(m)
fmt.Println(s2)
}
Output: map[10:1 20:2]
func Map ¶
func Map[M ~map[K]V, K, J comparable, V, U any](m M, f func(K, V) (J, U)) map[J]U
Map transforms each entry of m using f, with last-write-wins semantics for duplicate output keys.
Example ¶
package main
import (
"fmt"
"github.com/tecnickcom/nurago/pkg/maputil"
)
func main() {
m := map[int]string{0: "Hello", 1: "World"}
mapFn := func(k int, v string) (string, int) { return "_" + v, k + 1 }
s2 := maputil.Map(m, mapFn)
fmt.Println(s2)
}
Output: map[_Hello:1 _World:2]
func Reduce ¶
func Reduce[M ~map[K]V, K comparable, V, U any](m M, init U, f func(K, V, U) U) U
Reduce folds m into single value by repeatedly applying f to each entry and accumulator; f should be order-independent for deterministic output.
Example ¶
package main
import (
"fmt"
"github.com/tecnickcom/nurago/pkg/maputil"
)
func main() {
m := map[int]int{0: 2, 1: 3, 2: 5, 3: 7, 4: 11}
init := 97
reduceFn := func(k, v, r int) int { return k + v + r }
r := maputil.Reduce(m, init, reduceFn)
fmt.Println(r)
}
Output: 135
Types ¶
This section is empty.