collection

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: BSD-3-Clause Imports: 1 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable](items []T, item T) bool

func Every

func Every[T interface{}](items []T, fn func(T) bool) bool

func Filter

func Filter[T interface{}](items []T, fn func(T) bool) []T

func Find

func Find[T interface{}](items []T, fn func(T) bool) (T, bool)

func GroupBy

func GroupBy[T interface{}, K comparable](items []T, fn func(T) K) map[K][]T

func IndexOf

func IndexOf[T comparable](items []T, search T) int

func LastIndexOf

func LastIndexOf[T comparable](items []T, search T) int

func Map

func Map[T interface{}, R interface{}](items []T, fn func(T) R) []R

func Pluck added in v0.2.0

func Pluck[T any, V any](items []T, fn func(T) V) []V

func Reduce

func Reduce[T interface{}, R interface{}](items []T, fn func(R, T) R, first R) R

func Reverse added in v0.2.0

func Reverse[T any](items []T)

Reverse reverses the order of the collection.

func Some added in v0.2.0

func Some[T interface{}](items []T, fn func(T) bool) bool

func SortBy

func SortBy[T interface{}](items []T, fn func(a, b T) bool) []T

func Unique added in v0.1.7

func Unique[T interface{}, K comparable](items []T, fn func(T) K) []T

Types

type Sortable

type Sortable[T interface{}] struct {
	// contains filtered or unexported fields
}

func NewSortable

func NewSortable[T interface{}](items []T) *Sortable[T]

func (*Sortable[T]) Len

func (s *Sortable[T]) Len() int

Len is the number of elements in the collection.

func (*Sortable[T]) Less

func (s *Sortable[T]) Less(i int, j int) bool

Less reports whether the element with index i must sort before the element with index j.

If both Less(i, j) and Less(j, i) are false, then the elements at index i and j are considered equal. Sort may place equal elements in any order in the final result, while Stable preserves the original input order of equal elements.

Less must describe a transitive ordering:

  • if both Less(i, j) and Less(j, k) are true, then Less(i, k) must be true as well.
  • if both Less(i, j) and Less(j, k) are false, then Less(i, k) must be false as well.

Note that floating-point comparison (the < operator on float32 or float64 values) is not a transitive ordering when not-a-number (NaN) values are involved. See Float64Slice.Less for a correct implementation for floating-point values.

func (*Sortable[T]) SortBy

func (s *Sortable[T]) SortBy(sortFn func(a, b T) bool) []T

SortBy sorts the collection in-place using the given sort function.

func (*Sortable[T]) Swap

func (s *Sortable[T]) Swap(i int, j int)

Swap swaps the elements with indexes i and j.

Jump to

Keyboard shortcuts

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