collections

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2022 License: Apache-2.0 Imports: 2 Imported by: 3

README

Collections (Golang library)

go get github.com/joshuatcasey/collections

Contains several functions to help with common array processing. These functions are stateless and generic.

Also contains a set class which is backed by a standard map.

Consuming

go get github.com/joshuatcasey/collections

Testing

./scripts/unit.sh

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeriveFunc added in v0.2.0

func DeriveFunc[T any](a []T, f func(current, derived T) T) T

DeriveFunc will scan the array to gain an insight (a derivation) about the array. For example, given an array of ints it can calculate the max or min.

func FilterFunc

func FilterFunc[T any](a []T, f func(T) bool) []T

FilterFunc returns an array containing only those elements of a for which f returns true

func Max added in v0.2.0

func Max[T constraints.Ordered](a []T) T

Max will return the max of the ordered elements

func MaxParams added in v0.2.0

func MaxParams[T constraints.Ordered](a ...T) T

MaxParams will return the max of the ordered elements

func Min added in v0.2.0

func Min[T constraints.Ordered](a []T) T

Min will return the min of the ordered elements

func MinParams added in v0.2.0

func MinParams[T constraints.Ordered](a ...T) T

MinParams will return the min of the ordered elements

func Sum added in v0.2.0

func Sum[T constraints.Ordered](a []T) T

Sum will return the sum of the ordered elements

func SumParams added in v0.2.0

func SumParams[T constraints.Ordered](a ...T) T

SumParams will return the sum of the ordered elements

func TransformFunc

func TransformFunc[T, U any](a []T, f func(T) U) []U

TransformFunc returns an array containing the result of f for all elements of a

func TransformFuncWithError

func TransformFuncWithError[T, U any](a []T, f func(T) (U, error)) ([]U, error)

TransformFuncWithError returns an array containing the result of f for all elements of a and allows f to return an error. If f returns an error at any point, an empty array and the error are returned.

Types

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set emulates a mathematical set. It is backed by a map but intent is clearer when using named functions instead of map operations. Presence in the map indicates existence in the set. Not thread-safe

func NewSet

func NewSet(elements ...string) *Set[string]

NewSet assumes a set of strings

func NewSetOf

func NewSetOf[T comparable](elements ...T) *Set[T]

NewSetOf permits a parameterized type

func (*Set[T]) Add

func (s *Set[T]) Add(elements ...T)

Add an element to the set

func (*Set[T]) AddAll

func (s *Set[T]) AddAll(other *Set[T])

AddAll performs a set union by modifying s

func (*Set[T]) Contains

func (s *Set[T]) Contains(elements ...T) bool

Contains returns true if and only if all elements are contained in the set

func (*Set[T]) Elements

func (s *Set[T]) Elements() []T

Elements returns all the elements of the set as an array. Order within the array is not guaranteed.

func (*Set[T]) ForEach added in v0.5.0

func (s *Set[T]) ForEach(f func(T))

ForEach calls function f with each element of the set. The order in which the elements will be called is not guaranteed.

func (*Set[T]) IsEmpty

func (s *Set[T]) IsEmpty() bool

IsEmpty returns true if and only if no elements are in the set

func (*Set[T]) KeepOnly

func (s *Set[T]) KeepOnly(other *Set[T])

KeepOnly performs a set intersection by modifying s

func (*Set[T]) Remove

func (s *Set[T]) Remove(elements ...T)

Remove all elements from the set

func (*Set[T]) Size

func (s *Set[T]) Size() int

Size returns the number of elements in the set

Jump to

Keyboard shortcuts

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