slicesextended

package
v1.28.1 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package slicesextended provides extra functionality on top of the slices package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

func Count[T any](s []T, f func(T) bool) int

Count returns the number of elements in s where f returns true.

func CountError

func CountError[T any](s []T, f func(T) (bool, error)) (int, error)

CountError returns the number of elements in s where f returns true.

Returns error the first time f returns error.

func ElementsContained

func ElementsContained(superset []string, subset []string) bool

ElementsContained returns true if superset contains subset.

Nil and empty slices are treated as equals.

func ElementsEqual

func ElementsEqual[T comparable](one []T, two []T) bool

ElementsEqual returns true if the two slices have equal elements.

Nil and empty slices are treated as equals.

func Filter

func Filter[T any](s []T, f func(T) bool) []T

Filter filters the slice to only the values where f returns true.

func FilterError

func FilterError[T any](s []T, f func(T) (bool, error)) ([]T, error)

FilterError filters the slice to only the values where f returns true.

Returns error the first time f returns error.

func Map

func Map[T1, T2 any](s []T1, f func(T1) T2) []T2

Map maps the slice.

func MapError

func MapError[T1, T2 any](s []T1, f func(T1) (T2, error)) ([]T2, error)

MapError maps the slice.

Returns error the first time f returns error.

func MapToSlice

func MapToSlice[T comparable](m map[T]struct{}) []T

MapToSlice converts the map to a slice.

func MapToSortedSlice

func MapToSortedSlice[M ~map[T]struct{}, T Ordered](m M) []T

MapToSortedSlice converts the map to a sorted slice.

func Reduce

func Reduce[T1, T2 any](s []T1, f func(T2, T1) T2, initialValue T2) T2

Reduce reduces the slice.

func ReduceError

func ReduceError[T1, T2 any](s []T1, f func(T2, T1) (T2, error), initialValue T2) (T2, error)

Reduce reduces the slice.

Returns error the first time f returns error.

func ToChunks

func ToChunks[T any](s []T, chunkSize int) [][]T

ToChunks splits s into chunks of the given chunk size.

If s is nil or empty, returns empty. If chunkSize is <=0, returns [][]T{s}.

func ToMap

func ToMap[T comparable](s []T) map[T]struct{}

ToMap converts the slice to a map.

func ToUniqueSorted

func ToUniqueSorted[S ~[]T, T Ordered](s S) S

ToUniqueSorted returns a sorted copy of s with no duplicates.

Types

type Ordered

type Ordered interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64 |
		~string
}

Ordered matches cmp.Ordered until we only support Go versions >= 1.21.

TODO: remove and replace with cmp.Ordered when we only support Go versions >= 1.21.

Jump to

Keyboard shortcuts

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