slices

package
v0.0.0-...-480317c Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

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

Any returns true if at least one element in the slice satisfies the predicate and false otherwise. It returns true at the first element that satisfies the predicate, and does not check the rest of the elements

func Contains

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

Contains returns true if the item is in the slice and false otherwise

func Copy

func Copy[S Slice[T], T any](slice S) S

Copy returns a new slice with the same elements as the original

func Count

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

Count returns the number of elements in the slice that satisfy the predicate

func Filter

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

Filter returns a new slice with the elements that satisfy the predicate

func Find

func Find[T any](arr []T, predicate func(T) bool) (T, bool)

Find returns the first element that satisfies the predicate and a boolean

func FindIndex

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

FindIndex returns the index of the first element that satisfies the predicate and -1 if no element satisfies the predicate

func First

func First[S Slice[T], T any](slice S) T

First returns the first element of the slice. If the slice is empty, it panics

func For

func For[T any](i int, f func(int) T) []T

For applies the function f i times, and returns a slice with the results

func ForEach

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

ForEach applies the function f to each element of the slice

func ForEach_m

func ForEach_m[T any](slice []T, f func(*T))

ForEach_m applies the function f to each element of the slice. The function f receives a pointer to the element as an argument

func IsEmpty

func IsEmpty[S Slice[T], T any](slice S) bool

IsEmpty returns true if the slice is empty and false otherwise

func IsInBounds

func IsInBounds[T any](slice []T, index int) bool

IsInBounds returns true if the index is within the bounds of the slice and false otherwise

func Last

func Last[S Slice[T], T any](slice S) T

Last returns the last element of the slice. If the slice is empty, it panics

func Map

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

Map applies the function f to each element of the slice and returns a new slice with the results

func Map_i

func Map_i[T, U any](slice []T, f func(T, int) U) []U

Map_i applies the function f to each element of the slice and returns a new slice with the results. The function f receives the index of the element as a second argument

func Max

func Max[S Slice[T], T cmp.Ordered](slice S) T

Max returns the maximum element in the slice. If the slice is empty, it panics

func Max_i

func Max_i[S Slice[T], T cmp.Ordered](slice S) (T, int)

Max_i returns the maximum element in the slice. If the slice is empty, it panics

func Middle

func Middle[S Slice[T], T any](slice S) T

Middle returns the middle element of the slice. If the slice has an even number of elements, it returns the first element of the second half. If the slice is empty, it panics

func Min

func Min[S Slice[T], T cmp.Ordered](slice S) T

Min returns the minimum element in the slice. If the slice is empty, it panics

func Min_i

func Min_i[S Slice[T], T cmp.Ordered](slice S) (T, int)

Min_i returns the minimum element in the slice. If the slice is empty, it panics

func Product

func Product[S Slice[T], T types.Number](slice S) T

Product returns the product of all the elements in the slice

func Reduce

func Reduce[T any](slice []T, f func(T, T) T, initialValue T) T

Reduce applies the function f to the elements of the slice and returns a single value. The function f receives the accumulated value and the current element as arguments

func Reduce_i

func Reduce_i[T any](slice []T, f func(T, T, int) T, initialValue T) T

Reduce_i applies the function f to the elements of the slice and returns a single value. The function f receives the index of the element as a third argument

func RemoveNth

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

RemoveNth returns a new slice with the element at the given index removed If the index is out of bounds, it panics

func Repeat

func Repeat[T any](element T, n int) []T

Repeat returns a new slice with the element repeated n times

func Sum

func Sum[S Slice[T], T types.Summable](slice S) T

Sum returns the sum of all the elements in the slice

func Swap

func Swap[T any](slice []T, i, j int)

Swap swaps the elements at the given indices in the slice. If the indices are out of bounds, it panics

func ToMap

func ToMap[K comparable, V any](keys []K, values []V) map[K]V

ToMap converts two slices into a map. The first slice contains the keys and the second slice contains the values. If the slices have different lengths, it panics

func ZipWith

func ZipWith[T, U, V any](slice1 []T, slice2 []U, f func(T, U) V) []V

ZipWith applies the function f to each pair of elements from the two slices and returns a new slice with the results. If the slices have different lengths, the result will have the length of the shortest slice

Types

type Slice

type Slice[T any] interface {
	~[]T
}

Slice represents a generic slice type. It also includes the named types

Jump to

Keyboard shortcuts

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