array

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package array provides functions for manipulating arrays. This comprehensive library provides an array of helper functions specifically designed to empower developers in working efficiently with array slices. It encompasses popular methods like map, filter, reduce, forEach, find, some, and many more, offering streamlined functionalities to enhance your golang coding experience. For the javascript methods https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func At

func At[T comparable](s []T, i int) (*T, error)

At retrieves the element at the specified index in a slice.

The function takes a slice 's' of any comparable type 'T' and an integer 'i' representing the index of the element to retrieve. It returns a pointer to the element and an error. If the index is out of range, it returns a nil pointer and an error indicating the out-of-range condition.

Parameters:

s []T:      The slice from which to retrieve the element.
i int:       The index of the element to retrieve.

Returns:

*T:         A pointer to the element at the specified index.
error:      An error indicating any issues encountered, such as an out-of-range index.

Example:

intSlice := []int{1, 2, 3, 4, 5}
index := 2
element, err := At(intSlice, index)
if err != nil {
    fmt.Println("Error:", err)
} else {
    fmt.Println("Element at index", index, ":", *element)
}
// Output: Element at index 2 : 3

stringSlice := []string{"apple", "banana", "orange"}
index = -1
element, err = At(stringSlice, index)
if err != nil {
    fmt.Println("Error:", err)
} else {
    fmt.Println("Element at index", index, ":", *element)
}
// Output: Element at index -1 : banana

func Concat

func Concat[T comparable](s1 []T, s2 ...[]T) []T

func ConcatMut

func ConcatMut[T comparable](s1 *[]T, s2 ...[]T)

func Every

func Every[T comparable](s []T, f Predicate[T]) bool

func Fill

func Fill[T comparable](s []T, el T, startPos, endPos uint) ([]T, error)

func Filter

func Filter[T comparable](s []T, f Predicate[T]) []T

func Find

func Find[T comparable](s []T, f Predicate[T]) (*T, error)

func FindIndex

func FindIndex[T comparable](s []T, f Predicate[T]) int

func FindLast

func FindLast[T comparable](s []T, f Predicate[T]) (*T, error)

func FindLastIndex

func FindLastIndex[T comparable](s []T, f Predicate[T]) int

func Flat

func Flat[T comparable](s [][]T) []T

func ForEach

func ForEach[T comparable](s []T, f ForEachFunc[T])

ForEach applies a given function to each element of a comparable slice.

The function takes three arguments: the current element, its index, and the original slice. The function is applied in order for each element in the slice.

This function does not return a value.

Example:

ForEach([]int{1, 2, 3, 4}, func(n int, i int, nums []int) {
    fmt.Println(n, "is at index", i, "in", nums)
})

This will print: 1 is at index 0 in [1 2 3 4] 2 is at index 1 in [1 2 3 4] 3 is at index 2 in [1 2 3 4] 4 is at index 3 in [1 2 3 4]

Parameters: s - The slice to iterate over. f - The function to apply to each element.

func Includes

func Includes[T comparable](s []T, e T) bool

Includes checks if a given element is present in the array.

func IndexOf

func IndexOf[T comparable](s []T, e T) int

func Map

func Map[T comparable](s []T, f MapFunc[T]) []any

func MapMut

func MapMut()

func Pop

func Pop[T comparable](s []T, defaultValue T) ([]T, T, error)

func PopMut

func PopMut[T comparable](s *[]T, defaultValue T) (T, error)

func Push

func Push[T comparable](s []T, e T) []T

func PushMut

func PushMut[T comparable](s *[]T, e T)

func Reduce

func Reduce[T comparable](s []T, f ReduceFunc[T], defaultValue any) any

func ReduceStrict

func ReduceStrict[T comparable, K comparable](s []T, f ReduceStrictFunc[T, K], defaultValue K) K

func Reverse

func Reverse[T comparable](s []T) []T

func Shift

func Shift[T comparable](s []T, defaultValue T) ([]T, T, error)

func ShiftMut

func ShiftMut[T comparable](s *[]T, defaultValue T) (T, error)

func Slice

func Slice[T comparable](s []T, begin, end int) []T

func Some

func Some[T comparable](s []T, f Predicate[T]) bool

func UnShift

func UnShift[T comparable](s []T, e T) []T

func UnShiftMut

func UnShiftMut[T comparable](s *[]T, e T)

Types

type Entry

type Entry[T comparable] struct {
	Index int
	Value T
}

func Entries

func Entries[T comparable](s []T) []Entry[T]

type ForEachFunc

type ForEachFunc[T comparable] func(e T, i int, s []T)

type MapFunc

type MapFunc[T comparable] func(e T, i int, s []T) any

type Predicate

type Predicate[T comparable] func(e T, i int, s []T) bool

type ReduceFunc

type ReduceFunc[T comparable] func(acc any, e T, i int, s []T) any

type ReduceStrictFunc

type ReduceStrictFunc[T comparable, K comparable] func(acc K, e T, i int, s []T) K

Jump to

Keyboard shortcuts

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