collection

package
v0.0.0-...-42d93e8 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chunk

func Chunk[T any](s []T, size int) [][]T

Chunk splits the slice into chunks of the given size. The last chunk may be smaller than the given size.

func Difference

func Difference[T comparable](s1 []T, s2 []T) []T

Difference returns a new slice with elements that exist in the first slice but not in the second slice.

func Filter

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

Filter returns a new slice with all elements that satisfy the given predicate.

func GroupBy

func GroupBy[T any, K comparable](s []T, keyMapper func(T) K) map[K][]T

GroupBy returns a map of slices that accumulates the input elements into a map whose keys are the result of applying the provided mapping function to the input elements, and values are the input elements.

func Intersection

func Intersection[T comparable](s1 []T, s2 []T) []T

Intersection returns a new slice with elements that exist in both slices.

func Map

func Map[T, R any](s []T, transformer func(T) R) []R

Map returns a new slice with the result of applying the given function

func MarshalJSON

func MarshalJSON[E comparable](c Collection[E]) (bz []byte, err error)

func Partition

func Partition[T any](s []T, size int) [][]T

Partition splits the slice into chunks of the given size. The last chunk may be smaller than the given size.

func Reverse

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

Reverse reverses the slice in place.

func Shuffle

func Shuffle[T any](s []T)

Shuffle shuffles the slice in place. See https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle.

func Sort

func Sort[T any](s []T, less func(i, j int) bool)

Sort sorts the slice given the provided less function.

func String

func String[E comparable](c Collection[E]) string

String returns a string representation of the collection.

func ToMap

func ToMap[T any, K comparable](s []T, keyMapper func(T) K) map[K]T

ToMap returns a map with the given key functions.

func ToMapWithDuplicateKeyHandler

func ToMapWithDuplicateKeyHandler[T any, K comparable](
	s []T,
	keyMapper func(T) K,
	duplicateKeyHandler func(duplicateKey K, existingValue T, newValue T) T,
) map[K]T

ToMapWithDuplicateKeyHandler returns a map with the given key functions. If duplicate key found, the duplicate key handler will be called.

func ToMapWithIgnoreDuplicateKey

func ToMapWithIgnoreDuplicateKey[T any, K comparable](s []T, keyMapper func(T) K) map[K]T

ToMapWithIgnoreDuplicateKey returns a map with the given key functions. If duplicate key found, the new value will be kept.

func Transform

func Transform[T, R any](s []T, transformer func(T) R) []R

Transform returns a new slice with the result of applying the given function

func Union

func Union[T comparable](s1 []T, s2 []T) []T

Union returns a new slice with elements that exist in either slice.

func UnmarshalJSON

func UnmarshalJSON[E comparable](c Collection[E], data []byte) error

Types

type Collection

type Collection[E comparable] interface {
	fmt.Stringer
	json.Marshaler
	json.Unmarshaler
	// Add adds an element to the collection.
	// Returns true if the element was added.
	Add(e E) bool
	// AddAll adds all elements from the collection to the collection.
	// Returns true if the collection was modified.
	AddAll(other Collection[E]) bool
	// Remove removes an element from the collection.
	Remove(e E) bool
	// RemoveAll removes all elements from the collection.
	RemoveAll(other Collection[E]) bool
	// RemoveIf removes all elements that satisfy the predicate.
	RemoveIf(predicate func(e E) bool)
	// RetainAll retains only the elements in the collection that are contained in the specified collection.
	RetainAll(other Collection[E])
	// Clear removes all elements from the collection.
	Clear()
	// Contains returns true if the collection contains the element.
	Contains(e E) bool
	// ContainsAll returns true if the collection contains all elements in the specified collection.
	ContainsAll(other Collection[E]) bool
	// IsEmpty returns true if the collection is empty.
	IsEmpty() bool
	// Size returns the number of elements in the collection.
	Size() int
	// ForEach iterates over all elements in the collection.
	ForEach(consumer func(e E))
	// ForEachIndexed iterates over all elements in the collection.
	// The consumer function returns true to stop iterating.
	ForEachIndexed(consumer func(index int, e E) (stop bool))
	// AsSlice returns the collection as a slice.
	AsSlice() []E
	// Stream returns a stream of the elements in the collection.
	Stream() stream.Stream
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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