Documentation
¶
Overview ¶
Package map provides functional abstractions over go maps.
Index ¶
- Variables
- func FilterMap[K comparable, V any](in map[K]V, fn func(K, V) bool) map[K]V
- func Invert[K comparable, V comparable](in map[K]V) map[V]K
- func Keys[K comparable, T any](in map[K]T) []K
- func MapValues[K comparable, V any, R any](in map[K]V, fn func(V) R) map[K]R
- func Merge[K comparable, V any](a, b map[K]V) map[K]V
- func MergeWith[K comparable, V any](a, b map[K]V, fn func(V, V) V) map[K]V
- func ToMap[K comparable, T any](in []T, keyFunc KeyFunc[K, T]) (map[K]T, error)
- func ToSlice[K comparable, T any](in map[K]T) []T
- type KeyFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrDuplicateKeys = errors.New("input slice has duplicate keys according to key function")
)
Functions ¶
func FilterMap ¶ added in v1.0.0
func FilterMap[K comparable, V any](in map[K]V, fn func(K, V) bool) map[K]V
FilterMap returns a new map containing only the key-value pairs for which fn returns true.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/maps"
)
func main() {
scores := map[string]int{"Alice": 90, "Bob": 45, "Carol": 75}
passing := maps.FilterMap(scores, func(_ string, v int) bool { return v >= 60 })
fmt.Printf("Alice passing: %v\n", passing["Alice"])
fmt.Printf("Bob passing: %v\n", passing["Bob"] != 0)
fmt.Printf("Carol passing: %v\n", passing["Carol"])
}
Output: Alice passing: 90 Bob passing: false Carol passing: 75
func Invert ¶ added in v1.0.0
func Invert[K comparable, V comparable](in map[K]V) map[V]K
Invert returns a new map with keys and values swapped. If multiple keys in the input map to the same value, the resulting key will map to one of the original keys non-deterministically.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/maps"
)
func main() {
codes := map[string]int{"USD": 1, "EUR": 2, "GBP": 3}
byCode := maps.Invert(codes)
fmt.Printf("%s\n", byCode[1])
fmt.Printf("%s\n", byCode[2])
}
Output: USD EUR
func Keys ¶ added in v0.4.0
func Keys[K comparable, T any](in map[K]T) []K
func MapValues ¶ added in v1.0.0
func MapValues[K comparable, V any, R any](in map[K]V, fn func(V) R) map[K]R
MapValues returns a new map with the same keys as in, with each value transformed by fn.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/maps"
)
func main() {
prices := map[string]float64{"apple": 1.00, "banana": 0.50}
discounted := maps.MapValues(prices, func(v float64) float64 { return v * 0.9 })
fmt.Printf("apple: %.2f\n", discounted["apple"])
fmt.Printf("banana: %.2f\n", discounted["banana"])
}
Output: apple: 0.90 banana: 0.45
func Merge ¶ added in v1.0.0
func Merge[K comparable, V any](a, b map[K]V) map[K]V
Merge combines two maps into a new map. When both maps contain the same key, the value from b takes precedence.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/maps"
)
func main() {
defaults := map[string]int{"timeout": 30, "retries": 3}
overrides := map[string]int{"retries": 5, "verbose": 1}
config := maps.Merge(defaults, overrides)
fmt.Printf("timeout: %d\n", config["timeout"])
fmt.Printf("retries: %d\n", config["retries"])
fmt.Printf("verbose: %d\n", config["verbose"])
}
Output: timeout: 30 retries: 5 verbose: 1
func MergeWith ¶ added in v1.0.0
func MergeWith[K comparable, V any](a, b map[K]V, fn func(V, V) V) map[K]V
MergeWith combines two maps into a new map. When both maps contain the same key, fn is called with the values from a and b to produce the merged value.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/maps"
)
func main() {
a := map[string]int{"x": 1, "y": 2}
b := map[string]int{"y": 3, "z": 4}
merged := maps.MergeWith(a, b, func(va, vb int) int { return va + vb })
fmt.Printf("x: %d\n", merged["x"])
fmt.Printf("y: %d\n", merged["y"])
fmt.Printf("z: %d\n", merged["z"])
}
Output: x: 1 y: 5 z: 4
func ToMap ¶
func ToMap[K comparable, T any](in []T, keyFunc KeyFunc[K, T]) (map[K]T, error)
ToMap converts a slice that is map indexed into a map according to the provided key function. Duplicates are not allowed, so an error is returned in the case of a duplicate.
func ToSlice ¶
func ToSlice[K comparable, T any](in map[K]T) []T
ToSlice takes in a map and returns a slice of the values from the map. Order of the slice is not guaranteed in any way.
Types ¶
type KeyFunc ¶
type KeyFunc[K comparable, T any] func(v T) K