arrays

package
v0.0.0-...-40f42d4 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare[T any, R any](arr1 *[]T, arr2 *[]R, compare functions.BiFunction[T, R, bool]) []int

Compare compares two slices and returns the indexes of elements that satisfy the comparison function. It has a complexity of O[m * n] where m is the length of the first slice and n is the length of the second slice.

Parameters: - arr1: the first input slice - arr2: the second input slice - compare: a function that compares elements of type T and R and returns a boolean

Returns: - []int: a slice of indexes of elements that satisfy the comparison function

func Contains

func Contains[T comparable](arr *[]T, val T) bool

Contains checks if a value exists in a slice.

It iterates over the elements of the slice and returns true if the value is found, otherwise it returns false.

Parameters: - arr: the input slice - val: the value to search for

Returns: - bool: true if the value is found in the slice, false otherwise

func ContainsAllBy

func ContainsAllBy[T any, R any](exp *[]T, res *[]R, compare functions.BiFunction[T, R, bool]) bool

ContainsAllBy checks if all elements in the expected slice (exp) are present in the result slice (res) using the provided comparison function (compare). It has a complexity of O(n*m) where n is the length of exp and m is the length of res.

Parameters: - exp: the expected slice of type T - res: the result slice of type R - compare: the comparison function that takes two elements of type T and R and returns a boolean indicating if they are equal

Returns: - bool: true if all elements in exp are present in res, false otherwise

func ContainsBy

func ContainsBy[T any, K any](arr *[]T, val K, f functions.BiFunction[T, K, bool]) bool

ContainsBy checks if a value exists in a slice using a BiFunction function.

The function iterates over the elements of the slice and applies the BiFunction function to each element. If the predicate function returns true for the current element, the function returns true. Otherwise, it returns false.

Parameters: - arr: the input slice - val: the value to search for - f: the BiFunction function that takes an element of the slice and a value of type K and returns a boolean - K: the type of the value

Returns: - bool: true if the value is found in the slice, false otherwise

func Empty

func Empty[T any](arr *[]T) bool

Empty checks if a given slice is empty. It has a complexity of O(1).

Parameters: - arr: the input slice

Returns: - bool: true if the slice is empty, false otherwise

func Equals

func Equals[T comparable](a, b *[]T) bool

Equals checks if two slices of comparable elements are equal.

Parameters: - a: the first slice - b: the second slice

Returns: - bool: true if the slices are equal, false otherwise

func EqualsBy

func EqualsBy[T any, K comparable](a, b *[]T, f functions.Function[T, K]) bool

EqualsBy checks if two slices of elements are equal based on a given function. The function f is used to compare elements of the slices. The function returns true if the slices are equal, false otherwise.

Parameters: - a: the first slice - b: the second slice - f: the function used to compare elements of the slices

Returns: - bool: true if the slices are equal, false otherwise

func Find

func Find[T any](arr *[]T, f functions.Function[T, bool]) int

Find returns the index of the first element in the slice that satisfies the condition defined by the provided function. If no elements satisfy the condition, it returns -1. It has a complexity of O(n) where n is the length of the slice.

Parameters: - arr: the input slice - f: a function that takes an element of type T and returns a boolean

Returns: - int: the index of the first element that satisfies the condition, or -1 if no elements satisfy the condition

func FindAll

func FindAll[T any](arr *[]T, f functions.Function[T, bool]) []int

FindAll returns all the indexes of elements in the slice that satisfy the condition defined by the provided function. It has a complexity of O(n) where n is the length of the slice.

Parameters: - arr: the input slice - f: a function that takes an element of type T and returns a boolean

Returns: - []int: a slice of indexes of elements that satisfy the condition

func First

func First[T any](arr *[]T) T

First returns the first element of the given slice. If the slice is empty, it returns the zero value of the element type.

func Fold

func Fold[T, R any](arr *[]T, initial R, f functions.BiFunction[R, T, R]) R

Fold applies a bi-function to each element of a slice, starting from an initial value, and returns the final result.

Parameters: - arr: the input slice - f: the bi-function to apply to each element - initial: the initial value

Returns: - R: the final result

func FoldRight

func FoldRight[T, R any](arr *[]T, initial R, f functions.BiFunction[T, R, R]) R

FoldRight applies a bi-function to each element of a slice in reverse order, starting from an initial value, and returns the final result.

Parameters: - arr: the input slice - f: the bi-function to apply to each element - initial: the initial value

Returns: - R: the final result

func Higher

func Higher[T constraints.Ordered](arr *[]T) T

Higher returns the highest value from a slice of ordered elements.

Parameters: - arr: a slice of elements that are ordered.

Returns: - T: the highest value from the slice.

func IndexOf

func IndexOf[T comparable](arr *[]T, val T) int

IndexOf returns the index of the first occurrence of a given value in a slice. If the value is not found, it returns -1. It has a complexity of O(n) where n is the length of the slice.

Parameters: - arr: the input slice - val: the value to search for

Returns: - int: the index of the first occurrence of the value, or -1 if not found

func IsReversed

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

IsReversed checks if two slices are the same, but in a reversed order.

Parameters: - a: the first slice - b: the second slice

Returns: - bool: true if the slices are the same, but in a reversed order, false otherwise

func Last

func Last[T any](arr *[]T) T

Last returns the last element of a given slice. It panics if the slice is empty.

func LastIndexOf

func LastIndexOf[T comparable](arr *[]T, val T) int

LastIndexOf returns the index of the last occurrence of a given value in a slice. If the value is not found, it returns -1. It has a complexity of O(n) where n is the length of the slice.

Parameters: - arr: the input slice - val: the value to search for

Returns: - int: the index of the last occurrence of the value, or -1 if not found

func Lower

func Lower[T constraints.Ordered](arr *[]T) T

Lower returns the lowest value from a slice of ordered elements.

Parameters: - arr: a slice of elements that are ordered.

Returns: - T: the lowest value from the slice.

func OutOfBounds

func OutOfBounds[T any](arr *[]T, i int) bool

OutOfBounds checks if the index is out of bounds of the given slice.

Parameters: - arr: the slice to check - i: the index to check

Returns: - bool: true if the index is out of bounds, false otherwise

func Reverse

func Reverse[T any](arr []T) []T

Reverse reverses the order of elements in a slice.

Parameters: - arr: The slice to be reversed.

Returns: - The reversed slice.

func SortedEqualsBy

func SortedEqualsBy[T any](a, b *[]T, eq functions.BiFunction[T, T, bool]) bool

SortedEqualsBy checks if two sorted slices of elements are equal based on a given equality function. The function eq is used to compare elements of the slices. The function returns true if the slices are equal, false otherwise.

Parameters: - a: the first slice - b: the second slice - eq: the function used to compare elements of the slices

Returns: - bool: true if the slices are equal, false otherwise

func Unique

func Unique[T comparable](arr *[]T) []T

Unique returns a new slice containing only the unique elements from the input slice. The elements in the input slice must be comparable.

Parameters: - arr: the input slice

Returns: - []T: a new slice containing only the unique elements from the input slice

func UniqueBy

func UniqueBy[T any, K comparable](arr *[]T, f functions.Function[T, K]) []T

UniqueBy returns a new slice containing only the unique elements from the input slice, based on the provided function. The elements in the input slice must be comparable.

Parameters: - arr: the input slice - f: a function that takes an element of type T and returns a key of type K

Returns: - []T: a new slice containing only the unique elements from the input slice

Types

type SortedSlice

type SortedSlice[T constraints.Ordered] []T

func Sorted

func Sorted[T constraints.Ordered](arr []T) SortedSlice[T]

Sorted sorts a slice of ordered elements in ascending order. It uses the sort.Slice function from the standard library to perform the sorting. The function returns the sorted slice.

func SortedBy

func SortedBy[T constraints.Ordered, K constraints.Ordered](arr []T, f functions.Function[T, K]) SortedSlice[T]

func (SortedSlice[T]) Len

func (a SortedSlice[T]) Len() int

func (SortedSlice[T]) Less

func (a SortedSlice[T]) Less(i, j int) bool

func (SortedSlice[T]) Swap

func (a SortedSlice[T]) Swap(i, j int)

Jump to

Keyboard shortcuts

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