Documentation
¶
Index ¶
- func Chunk[T any](s []T, size int) [][]T
- func Difference[T comparable](s1 []T, s2 []T) []T
- func Filter[T any](s []T, predicate func(T) bool) []T
- func GroupBy[T any, K comparable](s []T, keyMapper func(T) K) map[K][]T
- func Intersection[T comparable](s1 []T, s2 []T) []T
- func Map[T, R any](s []T, transformer func(T) R) []R
- func MarshalJSON[E comparable](c Collection[E]) (bz []byte, err error)
- func Partition[T any](s []T, size int) [][]T
- func Reverse[T any](s []T)
- func Shuffle[T any](s []T)
- func Sort[T any](s []T, less func(i, j int) bool)
- func String[E comparable](c Collection[E]) string
- func ToMap[T any, K comparable](s []T, keyMapper func(T) K) map[K]T
- func ToMapWithDuplicateKeyHandler[T any, K comparable](s []T, keyMapper func(T) K, ...) map[K]T
- func ToMapWithIgnoreDuplicateKey[T any, K comparable](s []T, keyMapper func(T) K) map[K]T
- func Transform[T, R any](s []T, transformer func(T) R) []R
- func Union[T comparable](s1 []T, s2 []T) []T
- func UnmarshalJSON[E comparable](c Collection[E], data []byte) error
- type Collection
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chunk ¶
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 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 ¶
Partition splits the slice into chunks of the given size. The last chunk may be smaller than the given size.
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 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 }