 Documentation
      ¶
      Documentation
      ¶
    
    
  
    
  
    Index ¶
- func Clone[K comparable, V any](m map[K]V) map[K]V
- func Filter[K comparable, V any](m map[K]V, filter func(k K, v V) bool) map[K]V
- func FilterErr[K comparable, V any](m map[K]V, filter func(k K, v V) (bool, error)) (map[K]V, error)
- func Keys[K comparable, V any](m map[K]V, sort ...func([]K)) []K
- func Map[OK comparable, OV any, IK comparable, IV any](m map[IK]IV, mapper func(IK, IV) (OK, OV)) map[OK]OV
- func MapErr[OK comparable, OV any, IK comparable, IV any](m map[IK]IV, mapper func(IK, IV) (OK, OV, error)) (map[OK]OV, error)
- func Merge[K comparable, V any](maps ...map[K]V) map[K]V
- func Reduce[O any, K comparable, V any](m map[K]V, initial O, reducer func(O, K, V) O) O
- func ReduceErr[O any, K comparable, V any](m map[K]V, initial O, reducer func(O, K, V) (O, error)) (O, error)
- func Values[K comparable, V any](m map[K]V, sort ...func([]V)) []V
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clone ¶ added in v0.198.0
func Clone[K comparable, V any](m map[K]V) map[K]V
Clone creates a clone from a passed source map.
Example ¶
package main
import (
	"go.llib.dev/frameless/pkg/mapkit"
)
func main() {
	var (
		src = map[string]int{"a": 1, "b": 2, "c": 3}
		dst = mapkit.Clone(src)
	)
	_ = dst // dst == map[string]int{"a": 1, "b": 2, "c": 3}
}
func Filter ¶ added in v0.200.0
func Filter[K comparable, V any](m map[K]V, filter func(k K, v V) bool) map[K]V
Example ¶
package main
import (
	"go.llib.dev/frameless/pkg/mapkit"
)
func main() {
	var (
		src = map[int]string{1: "a", 2: "b", 3: "c"}
		dst = mapkit.Filter[int, string](src, func(k int, v string) bool {
			return k != 2
		})
	)
	_ = dst // map[int]string{1: "a", 3: "c"}, nil
}
func FilterErr ¶ added in v0.226.0
func FilterErr[K comparable, V any](m map[K]V, filter func(k K, v V) (bool, error)) (map[K]V, error)
Example ¶
package main
import (
	"go.llib.dev/frameless/pkg/mapkit"
)
func main() {
	var (
		src      = map[int]string{1: "a", 2: "b", 3: "c"}
		dst, err = mapkit.FilterErr[int, string](src, func(k int, v string) (bool, error) {
			return k != 2, nil
		})
	)
	_, _ = dst, err // map[int]string{1: "a", 3: "c"}, nil
}
func Keys ¶ added in v0.188.0
func Keys[K comparable, V any](m map[K]V, sort ...func([]K)) []K
Example ¶
package main
import (
	"go.llib.dev/frameless/pkg/mapkit"
)
func main() {
	_ = mapkit.Keys(map[string]string{"a": "1", "b": "2", "c": "3"})
	// -> []string{"a", "b", "c"}
}
func Map ¶
func Map[ OK comparable, OV any, IK comparable, IV any, ](m map[IK]IV, mapper func(IK, IV) (OK, OV)) map[OK]OV
Example ¶
package main
import (
	"fmt"
	"strings"
	"go.llib.dev/frameless/pkg/mapkit"
)
func main() {
	var x = map[string]string{"a": "x", "b": "y", "c": "z"}
	x = mapkit.Map(x, func(k string, v string) (string, string) { return k, strings.ToUpper(v) })
	fmt.Printf("%#v\n", x) // map[string]string{"a": "X", "b": "Y", "c": "Z"}
}
func MapErr ¶ added in v0.226.0
func MapErr[ OK comparable, OV any, IK comparable, IV any, ](m map[IK]IV, mapper func(IK, IV) (OK, OV, error)) (map[OK]OV, error)
MapErr will do a mapping from an input type into an output type.
Example ¶
package main
import (
	"fmt"
	"strings"
	"go.llib.dev/frameless/pkg/mapkit"
	"go.llib.dev/frameless/pkg/must"
)
func main() {
	var x = map[string]string{"a": "x", "b": "y", "c": "z"}
	x = must.Must(mapkit.MapErr[string, string](x,
		func(k string, v string) (string, string, error) { return k, strings.ToUpper(v), nil }))
	fmt.Printf("%#v\n", x) // map[string]string{"a": "X", "b": "Y", "c": "Z"}
}
Example (WithError) ¶
package main
import (
	"fmt"
	"strconv"
	"go.llib.dev/frameless/pkg/mapkit"
)
func main() {
	var x = map[string]string{"a": "1", "b": "2", "c": "3"}
	mx, err := mapkit.MapErr[string, int](x, func(k, v string) (string, int, error) {
		cv, err := strconv.Atoi(v)
		return k, cv, err
	})
	if err != nil {
		panic("handle error")
	}
	fmt.Printf("%#v\n", mx) // map[string]int{"a": 1, "b": 2, "c": 3}
}
func Merge ¶ added in v0.188.0
func Merge[K comparable, V any](maps ...map[K]V) map[K]V
Merge will merge all passed map[K]V into a single map[K]V. Merging is intentionally order dependent by how the map argument values are passed to Merge.
Example ¶
package main
import (
	"go.llib.dev/frameless/pkg/mapkit"
)
func main() {
	var (
		a = map[string]int{"a": 1, "b": 2, "c": 3}
		b = map[string]int{"g": 7, "h": 8, "i": 9}
		c = map[string]int{"d": 4, "e": 5, "f": 6}
		d = map[string]int{"a": 42}
	)
	got := mapkit.Merge(a, b, c, d)
	_ = got
	//
	//	map[string]int{
	//		"a": 42, "b": 2, "c": 3,
	//		"g": 7, "h": 8, "i": 9,
	//		"d": 4, "e": 5, "f": 6,
	//	}
}
func Reduce ¶
func Reduce[O any, K comparable, V any](m map[K]V, initial O, reducer func(O, K, V) O) O
Example ¶
package main
import (
	"fmt"
	"go.llib.dev/frameless/pkg/mapkit"
)
func main() {
	var x = map[string]int{"a": 1, "b": 2, "c": 3}
	got := mapkit.Reduce[int](x, 0, func(o int, k string, v int) int {
		return o + v
	})
	fmt.Println(got) // "|abc"
}
func ReduceErr ¶ added in v0.226.0
func ReduceErr[O any, K comparable, V any](m map[K]V, initial O, reducer func(O, K, V) (O, error)) (O, error)
ReduceErr goes through the map value, combining elements using the reducer function.
Example ¶
package main
import (
	"fmt"
	"go.llib.dev/frameless/pkg/mapkit"
)
func main() {
	var x = map[string]int{"a": 1, "b": 2, "c": 3}
	got, err := mapkit.ReduceErr[int](x, 0, func(o int, k string, v int) (int, error) {
		return o + v, nil
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(got) // "|abc"
}
func Values ¶ added in v0.226.0
func Values[K comparable, V any](m map[K]V, sort ...func([]V)) []V
Example ¶
package main
import (
	"go.llib.dev/frameless/pkg/mapkit"
)
func main() {
	m := map[int]string{1: "a", 2: "b", 3: "c"}
	vs := mapkit.Values(m)
	_ = vs // []string{"a", "b", "c"} // order not guaranteed
}
Example (Sorted) ¶
package main
import (
	"sort"
	"go.llib.dev/frameless/pkg/mapkit"
)
func main() {
	m := map[int]string{1: "a", 2: "b", 3: "c"}
	mapkit.Values(m, sort.Strings)
}
Types ¶
This section is empty.
 Click to show internal directories. 
   Click to hide internal directories.