transform

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: LGPL-3.0 Imports: 1 Imported by: 39

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Map added in v1.0.4

func Map[K comparable, V any, Kn comparable, Vn any](
	from map[K]V,
	transform func(K, V) (Kn, Vn),
) map[Kn]Vn

Map transforms a map of one type to a map of another, by applying the input transformation function to each key value pair.

func MapToSlice added in v1.0.3

func MapToSlice[K comparable, V, T any](from map[K]V, transform func(K, V) []T) []T

MapToSlice is responsible for flattening a map of key values into a contiguous slice of key value pares from the map.

Example
package main

import (
	"fmt"

	"golang.org/x/exp/slices"
	. "gopkg.in/check.v1"

	"github.com/juju/collections/transform"
)

type mapSuite struct{}

var _ = Suite(mapSuite{})

func main() {
	peopleStatus := map[string]string{
		"wallyworld": "peachy",
		"bob":        "happy",
	}

	flat := transform.MapToSlice(peopleStatus, func(k, v string) []string {
		return []string{k, v}
	})
	fmt.Println(flat)

}

func (mapSuite) TestEmptyMapToSlice(c *C) {
	m := map[string]string{}
	to := transform.MapToSlice(m, func(k, v string) []any { return []any{k, v} })
	c.Assert(len(to), Equals, 0)
}

func (mapSuite) TestMapToSlice(c *C) {
	m := map[string]string{
		"a": "b",
		"c": "d",
	}
	to := transform.MapToSlice(m, func(k, v string) []string { return []string{k, v} })
	slices.Sort(to)
	c.Assert(to, DeepEquals, []string{"a", "b", "c", "d"})
}

func (mapSuite) TestEmptyMapTransformEmpty(c *C) {
	m := map[string]string{}

	to := transform.Map(m, func(k, v string) (string, any) {
		return k, v
	})

	c.Assert(len(to), Equals, 0)
}

func (mapSuite) TestEmptyMapTransform(c *C) {
	m := map[string]string{
		"one":   "two",
		"three": "four",
	}

	to := transform.Map(m, func(k, v string) (string, int) {
		if v == "two" {
			return k, 2
		}
		return k, 4
	})

	c.Assert(to, DeepEquals, map[string]int{
		"one":   2,
		"three": 4,
	})
}
Output:

[wallyworld peachy bob happy]

func Slice

func Slice[F any, T any](from []F, transform func(F) T) []T

Slice transforms a slice of one type to an equal length slice of another, by applying the input transformation function to each member.

func SliceOrErr added in v1.0.3

func SliceOrErr[F any, T any](from []F, transform func(F) (T, error)) ([]T, error)

SliceOrErr transforms a slice from one type to an equal length slice of another by mapping the input transformation function to each member. This differs from Slice in that the transform function can returns an error. If an error is encountered, the mapping will be cancelled and the error returned

func SliceToMap added in v1.0.2

func SliceToMap[F any, K comparable, V any](from []F, transform func(F) (K, V)) map[K]V

SliceToMap transforms a slice of one type to an equal length map with values from the slice, keyed by values indicated by the input transformation function.

Types

This section is empty.

Jump to

Keyboard shortcuts

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