array

package module
v0.0.0-...-f4dff82 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

README

array

Generic golang array utils

Documentation

Overview

Package array provides generic functions for working with go slices

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Average

func Average[T Number](data []T) float64

Average returns the average for an array of Numbers

func Contains

func Contains[T comparable](data []T, v T) bool

Contains returns whether an element is contained in an array

func Equal

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

Equal compares 2 arrays to ensure they are equal

func Filter

func Filter[T any](data []T, fn func(int, T) bool) []T

Filter returns an array containing the filtered results of an array

func First

func First[T any](data []*T, fn func(*T) bool) *T

First returns the first element matching a condition

func InsertAt

func InsertAt[T any](data []T, i int, v T) []T

InsertAt inserts v into data at index i and returns a new slice Adapted from: https://stackoverflow.com/questions/42746972/golang-insert-to-a-sorted-slice

func InsertSorted

func InsertSorted[T any](data []T, v T, fn func(a, b T) bool) []T

InsertSorted inserts v into data sorted by function fn

func Intersection

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

Intersection returns the intersection of two arrays

func Map

func Map[V, T any](data []V, fn func(int, V) (T, error)) ([]T, error)

Map returns an array containing the results of a array

func Max

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

Max returns the max Number in an array

func Min

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

Min returns the min Number in an array

func Reduce

func Reduce[V, T any](initial V, data []T, fn func(V, T) V) V

Reduce returns the result of combining the elements of an array using a closure

func Sum

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

Sum sums all Numbers in an array

Types

type Number

type Number interface {
	constraints.Float | constraints.Integer
}

Jump to

Keyboard shortcuts

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