maputil

package
v1.154.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 0 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package maputil provides generic functional-style helpers for Go maps.

It 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.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL