arr

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package arr provides utility functions for array/slice manipulation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chunk

func Chunk[T any](array []T, size int) [][]T

Chunk splits an array into groups of the specified size. Example: Chunk([]int{1, 2, 3, 4}, 2) -> [][]int{{1, 2}, {3, 4}}

func Compact

func Compact[T comparable](array []T) []T

Compact removes falsey values from an array. In Go, we consider nil, zero values, and empty collections as falsey. Example: Compact([]int{0, 1, 2, 0, 3}) -> []int{1, 2, 3}

func Concat

func Concat[T any](arrays ...[]T) []T

Concat concatenates arrays together. Example: Concat([]int{1, 2}, []int{3, 4}) -> []int{1, 2, 3, 4}

func Difference

func Difference[T comparable](array []T, others ...[]T) []T

Difference returns an array of elements that are in the first array but not in the others. Example: Difference([]int{1, 2, 3}, []int{2, 3, 4}) -> []int{1}

func Drop

func Drop[T any](array []T, n int) []T

Drop creates a slice with n elements dropped from the beginning. Example: Drop([]int{1, 2, 3, 4}, 2) -> []int{3, 4}

func DropRight

func DropRight[T any](array []T, n int) []T

DropRight creates a slice with n elements dropped from the end. Example: DropRight([]int{1, 2, 3, 4}, 2) -> []int{1, 2}

func Fill

func Fill[T any](array []T, value T, start, end int) []T

Fill fills elements of array with value from start up to, but not including, end. Example: Fill([]int{1, 2, 3, 4}, 0, 1, 3) -> []int{1, 0, 0, 4}

func FindIndex

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

FindIndex returns the index of the first element that satisfies the predicate function. Example: FindIndex([]int{1, 2, 3, 4}, func(n int) bool { return n > 2 }) -> 2

func FindLastIndex

func FindLastIndex[T any](array []T, predicate func(T) bool) int

FindLastIndex returns the index of the last element that satisfies the predicate function. Example: FindLastIndex([]int{1, 2, 3, 4}, func(n int) bool { return n > 2 }) -> 3

func First

func First[T any](array []T) (T, bool)

First returns the first element of an array. Example: First([]int{1, 2, 3}) -> 1

func Flatten

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

Flatten flattens an array a single level deep. Example: Flatten([][]int{{1, 2}, {3, 4}}) -> []int{1, 2, 3, 4}

func Includes

func Includes[T comparable](array []T, value T) bool

Includes checks if a value is in the array. Example: Includes([]int{1, 2, 3}, 2) -> true

func IndexOf

func IndexOf[T comparable](array []T, value T) int

IndexOf returns the index of the first occurrence of value in array. Example: IndexOf([]int{1, 2, 3, 2}, 2) -> 1

func Initial

func Initial[T any](array []T) []T

Initial returns all but the last element of an array. Example: Initial([]int{1, 2, 3}) -> []int{1, 2}

func Intersection

func Intersection[T comparable](arrays ...[]T) []T

Intersection returns an array of unique values that are included in all given arrays. Example: Intersection([]int{1, 2, 3}, []int{2, 3, 4}) -> []int{2, 3}

func Join

func Join[T any](array []T, separator string) string

Join joins all elements of an array into a string. Example: Join([]int{1, 2, 3}, ",") -> "1,2,3"

func Last

func Last[T any](array []T) (T, bool)

Last returns the last element of an array. Example: Last([]int{1, 2, 3}) -> 3

func LastIndexOf

func LastIndexOf[T comparable](array []T, value T) int

LastIndexOf returns the index of the last occurrence of value in array. Example: LastIndexOf([]int{1, 2, 3, 2}, 2) -> 3

func Nth

func Nth[T any](array []T, n int) (T, bool)

Nth returns the element at index n of array. If n is negative, the nth element from the end is returned. Example: Nth([]int{1, 2, 3}, 1) -> 2

func Pull

func Pull[T comparable](array []T, values ...T) []T

Pull removes all given values from array. Example: Pull([]int{1, 2, 3, 1, 2, 3}, 2, 3) -> []int{1, 1}

func Random

func Random[T any](slice []T, n int) []T

Random returns n random elements from the given slice without replacement. Example: Random([]int{1, 2, 3, 4, 5}, 3) -> [2, 4, 1]

func RandomChoice

func RandomChoice[T any](choices []T) (T, bool)

RandomChoice returns a random element from the given slice. Example: RandomChoice([]string{"a", "b", "c"}) -> "b"

func Reverse

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

Reverse reverses the order of elements in array. Example: Reverse([]int{1, 2, 3}) -> []int{3, 2, 1}

func Shuffle

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

Shuffle returns a new slice with elements in random order. Example: Shuffle([]int{1, 2, 3, 4, 5}) -> [3, 1, 5, 2, 4]

func Slice

func Slice[T any](array []T, start, end int) []T

Slice returns a slice of array from start up to, but not including, end. Example: Slice([]int{1, 2, 3, 4}, 1, 3) -> []int{2, 3}

func SortBy

func SortBy[T any, U int | int8 | int16 | int32 | int64 | float32 | float64 | string](array []T, iteratee func(T) U) []T

SortBy sorts an array by the results of running each element through iteratee. Example: SortBy([]int{1, 3, 2}, func(n int) int { return n })

func SortedIndex

func SortedIndex[T int | int8 | int16 | int32 | int64 | float32 | float64](array []T, value T) int

SortedIndex returns the index at which value should be inserted into array to maintain its sort order. Example: SortedIndex([]int{1, 3, 5, 7}, 4) -> 2

func Tail

func Tail[T any](array []T) []T

Tail returns all but the first element of array. Example: Tail([]int{1, 2, 3}) -> []int{2, 3}

func Take

func Take[T any](array []T, n int) []T

Take creates a slice of array with n elements taken from the beginning. Example: Take([]int{1, 2, 3, 4}, 2) -> []int{1, 2}

func TakeRight

func TakeRight[T any](array []T, n int) []T

TakeRight creates a slice of array with n elements taken from the end. Example: TakeRight([]int{1, 2, 3, 4}, 2) -> []int{3, 4}

func Union

func Union[T comparable](arrays ...[]T) []T

Union creates an array of unique values from all given arrays. Example: Union([]int{1, 2}, []int{2, 3}) -> []int{1, 2, 3}

func Uniq

func Uniq[T comparable](array []T) []T

Uniq creates an array of unique values. Example: Uniq([]int{1, 2, 1, 3}) -> []int{1, 2, 3}

func Without

func Without[T comparable](array []T, values ...T) []T

Without creates an array excluding all given values. Example: Without([]int{1, 2, 3, 4}, 2, 4) -> []int{1, 3}

func Zip

func Zip[T any](arrays ...[]T) [][]T

Zip creates an array of grouped elements. Example: Zip([]int{1, 2}, []string{"a", "b"}) -> [][]interface{}{{1, "a"}, {2, "b"}}

Types

This section is empty.

Jump to

Keyboard shortcuts

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