mapkit

package
v0.183.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2024 License: Apache-2.0 Imports: 0 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Map

func Map[
	K comparable, V any,
	IK comparable, IV any,
	FN mapperFunc[K, V, IK, IV],
](s map[IK]IV, fn FN) (map[K]V, error)

Map will do a mapping from an input type into an output type.

Example
package main

import (
	"fmt"
	"go.llib.dev/frameless/pkg/mapkit"
	"strings"
)

func main() {
	var x = map[string]string{"a": "x", "b": "y", "c": "z"}

	x = mapkit.Must(mapkit.Map[string, string](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"}
}
Example (WithError)
package main

import (
	"fmt"
	"go.llib.dev/frameless/pkg/mapkit"
	"strconv"
)

func main() {
	var x = map[string]string{"a": "1", "b": "2", "c": "3"}

	mx, err := mapkit.Map[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 Must

func Must[T any](v T, err error) T
Example
package main

import (
	"fmt"
	"go.llib.dev/frameless/pkg/mapkit"
)

func main() {
	var x = map[string]int{"A": 1, "B": 2, "C": 3}

	x = mapkit.Must(mapkit.Map[string, int](x,
		func(k string, v int) (string, int) { return k, v * 2 }))

	v := mapkit.Must(mapkit.Reduce[int](x, 42,
		func(output int, k string, v int) int { return output + v }))

	fmt.Println("result:", v)
}

func Reduce

func Reduce[
	O any,
	K comparable, V any,
	FN reducerFunc[O, K, V],
](s map[K]V, initial O, fn FN) (O, error)

Reduce 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.Reduce[int](x, 0, func(o int, k string, v int) int {
		return o + v
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(got) // "|abc"
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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