algorithm

package module
v0.0.0-...-515fb7a Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

README

algorithm

Generic algorithms and utility functions

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T comparable](slice []T, f func(T) bool) bool

"All" returns true if the given function returns true for all elements of the slice. Supports all comparable types for slice values.

func Any

func Any[T comparable](slice []T, f func(T) bool) bool

"Any" returns true if the given function returns true for any element of the slice. Supports all comparable types as slice values.

func Coalesce

func Coalesce(input []any) []any

Removes nil values from an array.

 Example:
	var arr = []interface{}{1, 2, 3, nil, 4, 5}
	result := Compact(arr)  // [1, 2, 3, 4, 5]

func Constrain

func Constrain[T comparable](e T, a []T) bool

Whether the element is within the supplied slice's values Actually, it's the same as above ... only with reversed params

func Contains

func Contains[T comparable](a []T, e T) bool

Whether the supplied slice contains element 'e' or not

func Diff

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

Diff returns the difference slice from two slices (values in s1 that are not in s2). Returns empty if given slice s1 is nil or empty. naïve implementation: O(n2) complexity

func Filter

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

Filter filters the values of slice by the given function. Returns empty if given slice is nil or empty.

func Intersect

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

Intersect returns the intersection of two slices. Returns empty if any of the given slices are nil or empty. naïve implementation: O(n2) complexity

func Map

func Map[T, U any](slice []T, fn func(T) U) []U

fn can't be nil

func Max

func Max[T Number](slice []T) T

Max get the max value of slice. Returns 0 if slice is nil or empty.

func MergeDict

func MergeDict[T any](dst map[string]T, src map[string]T)

MergeDict: merges two dictionaries; destination values will get overwritten by those on src

func Min

func Min[T Number](slice []T) T

Min get the min value of slice. Returns 0 if slice is nil or empty.

func Reduce

func Reduce[T comparable, U any](slice []T, initial U, reducer func(U, T) U) U

func Reject

func Reject[T, U comparable](slice []T, fn func(T) (bool, U)) []U

func Select

func Select[T, U comparable](slice []T, fn func(T) (bool, U)) []U

Select / Reject -> filter Supports all comparable types as slice values.

func Sum

func Sum[T Number](slice []T) T

Sum sums the values of slice.

func Unique

func Unique[T comparable](slice []T) []T

Types

type Enumerable

type Enumerable interface {
	Integer | Float | ~string
}

type Int

type Int interface {
	Signed | Unsigned
}

type Number

type Number interface {
	Signed | Unsigned | Float //| Complex
}

type String

type String interface {
	~string // | ~[]byte | ~[]rune
}

Jump to

Keyboard shortcuts

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