map_utils

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

Map Utils

map_utils is a Go library providing a collection of utility functions for working with maps. It leverages Go generics and Go 1.23+ iterators (iter package) to offer a fluent and type-safe experience for common map operations like filtering, mapping, reducing, and converting.

Installation

go get github.com/zauberhaus/map_utils

Features

  • Filtering & Selection: Select, Delete, CountFunc, ExistsFunc
  • Existence Checks: ContainsKey, Contains
  • Transformation: Remap, Convert
  • Aggregation: Summarize
  • Access: First, Last, At (access by index based on sorted keys)
  • Conversion: Slice (to slice), Join (to string)
  • Iterators: RemapFuncSeq, WeightFuncSeq, SliceFuncSeq

Usage

Filtering
import "github.com/zauberhaus/map_utils"

m := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}
// Select even values
result := map_utils.Select(m, func(key int, val int) bool {
    return val%2 == 0
})
// result: {2: 2, 4: 4}
Remapping

Transform keys and values.

m := map[int]int{1: 10, 2: 20}
result := map_utils.Remap(m, func(k, v int) (string, string, error) {
    return fmt.Sprintf("k%d", k), fmt.Sprintf("v%d", v), nil
})
// result: {"k1": "v10", "k2": "v20"}
Converting to Slice

Convert a map to a slice, optionally filtering or transforming elements.

m := map[string]int{"a": 1, "b": 2}
s := map_utils.Slice(m, func(k string, v int) (*int, error) {
    return &v, nil
})

for _, v := range s {
    fmt.Println(v)
}
Ordered Access

Access map elements by index (keys are sorted implicitly).

m := map[string]int{"a": 1, "b": 2, "c": 3}
val, err := map_utils.At(m, 1) // Access element at index 1 (sorted by key)
// val: 2

License

Copyright 2026 Zauberhaus

Licensed to Zauberhaus under one or more agreements. Zauberhaus licenses this file to you under the Apache 2.0 License. See the LICENSE file in the project root for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func At

func At[K cmp.Ordered, V any](m map[K]V, index int) (V, error)

func Contains

func Contains[K comparable, V any](m map[K]V, f func(key K, val V) bool) bool

func ContainsKey

func ContainsKey[K comparable, V any](m map[K]V, keys ...K) bool

func Convert

func Convert[K comparable, V1 any, V2 any](m map[K]V1, f func(key K, val V1) (V2, error)) map[K]V2

func CountFunc

func CountFunc[K comparable, V any](m map[K]V, f func(key K, val V) bool) int

func Delete

func Delete[K comparable, V any](m map[K]V, f func(k K, v V) bool) int

func ExistsFunc

func ExistsFunc[K comparable, V any](m map[K]V, f func(key K, val V) bool) bool

func First

func First[K cmp.Ordered, V any](m map[K]V) (V, error)

func Flatten added in v1.1.0

func Flatten[K cmp.Ordered, V any](m map[K]V) []any

func FlattenSeq added in v1.1.0

func FlattenSeq[K comparable, V any](m iter.Seq2[K, V]) iter.Seq[any]

func Join

func Join[K cmp.Ordered, V any](m map[K]V, sep string) string

func Last

func Last[K cmp.Ordered, V any](m map[K]V) (V, error)

func Remap

func Remap[K1 comparable, V1 any, K2 comparable, V2 any](m map[K1]V1, f func(key K1, val V1) (K2, V2, error)) map[K2]V2

func RemapFuncSeq

func RemapFuncSeq[K1 comparable, V1 any, K2 comparable, V2 any](m iter.Seq2[K1, V1], f func(key K1, val V1) (K2, V2, error)) iter.Seq2[K2, V2]

func Select

func Select[K comparable, V any](m map[K]V, f func(key K, val V) bool) map[K]V

func Slice

func Slice[Map ~map[K]V, K comparable, V any, S any](m Map, f func(key K, val V) (*S, error)) []S

func SliceFuncSeq

func SliceFuncSeq[K comparable, V any, R any](m iter.Seq2[K, V], f func(key K, val V) (*R, error)) iter.Seq[R]

func Summarize

func Summarize[K cmp.Ordered, V any, M ~map[K]V, S cmp.Ordered](m M, f func(key K, val V) S) S

func WeightFuncSeq

func WeightFuncSeq[K comparable, V any, S cmp.Ordered](m iter.Seq2[K, V], f func(key K, val V) S) iter.Seq[S]

Types

This section is empty.

Jump to

Keyboard shortcuts

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