gollections

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2022 License: MIT Imports: 2 Imported by: 3

README

gollections

Go Reference

Simple easy to use utility functions for Go slices and maps

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](slice []T, predicate func(T) bool) bool

Returns true if all elements satisfy the given predicate

func Any

func Any[T any](slice []T, predicate func(T) bool) bool

Returns true if any one of elements satisfy the given predicate

func Associate

func Associate[T any, K comparable, V any](slice []T, transform func(T) (K, V)) map[K]V

Returns a map generated from the given slice, using the given transform

func Contains

func Contains[T comparable](slice []T, target T) bool

Returns whether the given slice contains the given target value

func ContainsKey

func ContainsKey[K comparable, V any](hashMap map[K]V, target K) bool

Returns whether the map contains the key

func Drop

func Drop[T any](slice []T, index int) []T

Drops the value from the given slice at index and returns. The original slice remains unchanged.

func Filter

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

Filters the slice based on the given predicate

func FilterIndexed

func FilterIndexed[T any](slice []T, predicate func(int, T) bool) []T

Filters the slice based on the given predicate The predicate also considers index of each element

func FilterKeys

func FilterKeys[K comparable, V any](hashMap map[K]V, predicate func(K) bool) map[K]V

Filter keys based on the given predicate

func First

func First[T any](slice []T, predicate func(T) bool) (T, error)

Returns the first value the satisfies the given predicate. Raises error if there is no such element

func FirstOrDefault

func FirstOrDefault[T any](slice []T, defaultValue T, predicate func(T) bool) T

Returns the first value the satisfies the given predicate. If no such element is there then returns defaultValue

func FlatMap

func FlatMap[K comparable, V, R any](hashMap map[K]V, transform func(K, V) R) []R

Maps the map into a slice based on the given transform

func Flatten

func Flatten[T any](slices [][]T) []T

Flattens a 2d slice into a 1d slice

func Fold

func Fold[T any, R any](slice []T, initial R, operation func(T, R) R) R

Accumulates value starting with the given initial by performing operation on the slice from left to right

func FoldIndexed

func FoldIndexed[T any, R any](slice []T, initial R, operation func(int, T, R) R) R

Accumulates value starting with the given initial by performing operation on the slice from left to right The operation also considers index of each element

func FoldRight

func FoldRight[T any, R any](slice []T, initial R, operation func(T, R) R) R

Accumulates value starting with the given initial by performing operation on the slice from right to left

func FoldRightIndexed

func FoldRightIndexed[T any, R any](slice []T, initial R, operation func(int, T, R) R) R

Accumulates value starting with the given initial by performing operation on the slice from right to left The operation also considers index of each element

func ForEach

func ForEach[T any](slice []T, operation func(T))

Performs the given operation for each of element in the slice

func ForEachEntry

func ForEachEntry[K comparable, V any](hashMap map[K]V, operation func(K, V))

Runs operation on each entry of the map

func ForEachIndexed

func ForEachIndexed[T any](slice []T, operation func(int, T))

Performs the given operation for each of element in the slice The operation also considers index of each element

func GetOrDefault

func GetOrDefault[K comparable, V any](hashMap map[K]V, key K, defaultVal V) V

Get the value corresponding to the key, returns default value if the key is not there.

func GroupBy

func GroupBy[T any, K comparable](slice []T, selector func(T) K) map[K][]T

func IndexOf

func IndexOf[T comparable](slice []T, target T) int

Returns the index of the target element inside the slice. Returns -1 if that element does not exist.

func Keys

func Keys[K comparable, V any](hashMap map[K]V) []K

Returns the slice of all keys

func Map

func Map[T any, R any](slice []T, transform func(T) R) []R

Applies the transform function on each element of the slice and returns a slice of the transformed value

func MapIndexed

func MapIndexed[T any, R any](slice []T, transform func(int, T) R) []R

Applies the transform function on each element of the slice and returns a slice of the transformed value The transform also considers index of each element

func MaxOf

func MaxOf[T constraints.Ordered](slice []T) (T, error)

Returns maximum of the slice, error if empty

func MaxOfBy

func MaxOfBy[T any](slice []T, comparer func(T, T) int) (T, error)

Returns maximum of slice using the comparer function. If a > b then comparer(a, b) > 0

func MinOf

func MinOf[T constraints.Ordered](slice []T) (T, error)

Returns minimum of the slice, error if empty

func MinOfBy

func MinOfBy[T any](slice []T, comparer func(T, T) int) (T, error)

Returns minimum of slice using the comparer function. If a > b then comparer(a, b) > 0

func Partition

func Partition[T any](slice []T, predicate func(T) bool) ([]T, []T)

Partitions the slice based on the predicate. The left slice contains the elements that satisfies predicate and the right one contains the elements that does not.

func Reversed

func Reversed[T any](slice []T) []T

Returns the elements in reversed oreder

func SubList

func SubList[T any](slice []T, from, to int) []T

Returns subarray of the slice from `from` upto `to` indecies. Returns an empty slice if the indecies are invalid.

func Values

func Values[K comparable, V any](hashMap map[K]V) []V

Returns the slice of all values

Types

type Pair

type Pair[T, R any] struct {
	First  T
	Second R
}

Pair that contains two generic elements

func Entries

func Entries[K comparable, V any](hashMap map[K]V) []*Pair[K, V]

Returns a slice key, value pair

func Zip

func Zip[T, R any](a []T, b []R) []*Pair[T, R]

Returns a slice of pointers of Pair zipping given slices.

Jump to

Keyboard shortcuts

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