collections

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllMatch

func AllMatch[TSlice ~[]T, T any](slice TSlice, predicate func(T) bool) bool

AllMatch returns true if all elements pass the predicate function

func AnyMatch

func AnyMatch[TSlice ~[]T, T any](slice TSlice, predicate func(T) bool) bool

AnyMatch returns true if any element passes the predicate function

func Contains

func Contains[TSlice ~[]T, T comparable](slice TSlice, find T) bool

Contains returns true if find appears in slice

func Difference

func Difference[Map ~map[K]V, K comparable, V any](left, right Map) Map

Difference returns a new map of key/value pairs that only appear in left.

func Filter

func Filter[TSlice ~[]T, T any](slice TSlice, predicate func(T) bool) TSlice

Filter returns a new slice consisting of all elements that pass the predicate function

func FirstMatch

func FirstMatch[TSlice ~[]T, T any](slice TSlice, predicate func(T) bool) T

FirstMatch returns the first element that passes the predicate function. If no element is found the zero value of the type will be returned.

func GroupBy

func GroupBy[TSlice ~[]T, T any, K comparable, V any](slice TSlice, keySelector func(T) K, valSelector func(T) V) map[K][]V

GroupBy returns a map that is keyed by keySelector and contains a slice of elements returned by valSelector

func IndexOf

func IndexOf[TSlice ~[]T, T comparable](slice TSlice, find T) int

IndexOf returns the index of find if it appears in slice. If find is not in slice, -1 will be returned.

func Intersect

func Intersect[Map ~map[K]V, K comparable, V any](left, right Map) Map

Intersect returns a new map of key/value pairs where the key exists in both left and right. The value from right will be used in the return map.

func Keys

func Keys[Map ~map[K]V, K comparable, V any](m Map) []K

Keys returns a slice of all keys in m

func LastMatch

func LastMatch[TSlice ~[]T, T any](slice TSlice, predicate func(T) bool) T

LastMatch returns the last element that passes the predicate function. If no element is found the zero value of the type will be returned.

func Map

func Map[TSlice ~[]T, T any, R any](slice TSlice, fn func(T) R) []R

Map returns a new slice where each element is the result of fn for the corresponding element in the original slice

func Reduce

func Reduce[TSlice ~[]T, T any, R any](slice TSlice, fn func(acc R, elem T) R, acc R) R

Reduce return a result that

func SliceDifference

func SliceDifference[T comparable](a, b []T) []T

SliceDifference Difference of two slices(A-B). Slices do not need to be unique. Order not guaranteed.

@param a []T
@param b []T
@return []T
@author kevineluo
@update 2023-03-01 04:41:53

func SliceEqual

func SliceEqual[T comparable](a, b []T) bool

SliceEqual judge if two slice is equal(unique element)

@param a []T
@param b []T
@return bool
@author kevineluo
@update 2023-03-01 04:46:22

func SliceIntersection

func SliceIntersection[T comparable](a, b []T) []T

SliceIntersection Intersection of two slices, The provided slices do not need to be unique. Order not guaranteed.

@param a []T
@param b []T
@return []T
@author kevineluo
@update 2023-03-01 04:41:58

func SliceUnion

func SliceUnion[T comparable](a, b []T) []T

SliceUnion Union two slices, The provided slices do not need to be unique. Order not guaranteed.

@param a []T
@param b []T
@return []T
@author kevineluo
@update 2023-03-01 04:42:00

func ToMap

func ToMap[TSlice ~[]T, T any, K comparable, V any](slice TSlice, keySelector func(T) K, valSelector func(T) V) map[K]V

ToMap return a map that is keyed keySelector and has the value of valSelector for each element in slice. If multiple elements return the same key the element that appears later in slice will be chosen.

func ToSet

func ToSet[TSlice ~[]T, T any, K comparable](slice TSlice, keySelector func(T) K) map[K]struct{}

ToSet returns a map keyed by keySelector and contains a value of an empty struct

func Union

func Union[Map ~map[K]V, K comparable, V any](left, right Map) Map

Union returns a new map of all key/value pairs in left and right. If a key exists in both left and right the value in right will appear in the resultant map.

func UnionInPlace

func UnionInPlace[Map ~map[K]V, K comparable, V any](left, right Map) Map

UnionInPlace modifies left to include key/value pairs in right. If a key exists in both left and right the value in right will be used.

func Uniq

func Uniq[TSlice ~[]T, T any, K comparable](slice TSlice, keySelector func(T) K) (uniqSlice TSlice)

Uniq returns a new slice that uniques in values

func Values

func Values[Map ~map[K]V, K comparable, V any](m Map) []V

Values returns a slice of all values in m

Types

type Set

type Set[T comparable] map[T]struct{}

Set A collection of unique comparable items. Uses a map with only true values to accomplish set functionality.

@author kevineluo
@update 2023-03-01 03:52:15

func MapSliceToSet

func MapSliceToSet[S any, T comparable](s []S, f func(s S) T) Set[T]

MapSliceToSet Map a slice to a set using a function f

@param s []S
@param f func(s S) T
@return Set
@author kevineluo
@update 2023-03-01 04:42:04

func NewSet

func NewSet[T comparable](size int) Set[T]

NewSet Create a new empty set with the specified initial size.

@param size int
@return Set
@author kevineluo
@update 2023-03-01 03:52:21

func SliceToSet

func SliceToSet[T comparable](s []T) Set[T]

SliceToSet Create a Set from a slice.

@param s []T
@return Set
@author kevineluo
@update 2023-03-01 04:42:55

func (Set[T]) Add

func (s Set[T]) Add(key T)

Add Add a new key to the set

@param s Set[T]
@return Add
@author kevineluo
@update 2023-03-01 03:52:26

func (Set[T]) Contains

func (s Set[T]) Contains(key T) bool

Contains Check if Set s contains key

@param s Set[T]
@return Contains
@author kevineluo
@update 2023-03-01 03:52:29

func (Set[T]) Difference

func (s Set[T]) Difference(b Set[T]) Set[T]

Difference A difference B NOTE: A-B != B-A

@param a Set[T]
@return Difference
@author kevineluo
@update 2023-03-01 03:52:42

func (Set[T]) Equals

func (s Set[T]) Equals(b Set[T]) bool

Equals A == B (all elements of A are in B and vice versa)

@param a Set[T]
@return Equals
@author kevineluo
@update 2023-03-01 04:43:01

func (Set[T]) Intersection

func (s Set[T]) Intersection(b Set[T]) Set[T]

Intersection A intersect B

@param s Set[T]
@return Intersection
@author kevineluo
@update 2023-03-01 03:54:59

func (Set[T]) Remove

func (s Set[T]) Remove(key T)

Remove Remove a key from the set. If the key is not in the set then noop

@param s Set[T]
@return Remove
@author kevineluo
@update 2023-03-01 03:52:28

func (Set[T]) ToSlice

func (s Set[T]) ToSlice() []T

ToSlice Turn a Set into a slice

@param s Set[T]
@return ToSlice
@author kevineluo
@update 2023-03-01 04:43:18

func (Set[T]) Union

func (s Set[T]) Union(b Set[T]) Set[T]

Union A union B

@param a Set[T]
@return Union
@author kevineluo
@update 2023-03-01 03:54:43

Jump to

Keyboard shortcuts

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