arr

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2025 License: AGPL-3.0 Imports: 0 Imported by: 0

Documentation

Overview

Package arr contains functions to interact with arrays.

The package is imported like this:

import "github.com/gouniverse/base/arr"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FilterEmpty added in v0.1.0

func FilterEmpty(slice []string) []string

FilterEmpty takes a slice of strings and returns a new slice with all empty strings removed.

Example:

arr.FilterEmpty([]string{"", "hello", "", "world"}) // returns []string{"hello", "world"}

Parameters: - slice: the slice of strings to filter.

Returns: - []string: a new slice with all empty strings removed.

func IndexMoveDown

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

indexMoveDown moves the element at the given index down

Business logic: - if the index is last index, will be ignored - if the index out of bounds, will be ignored

Parameters: - slice: the slice to move the element from - index: the index of the element to move

Returns: - []T: the new slice

Example:

arr := []int{1, 2, 3, 4}
result := IndexMoveDown(arr, 1)
// result is now [1, 4, 2, 3]

func IndexMoveUp

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

indexMoveUp moves the element at the given index up

Business logic: - if the index is first index, will be ignored - if the index out of bounds, will be ignored

Parameters: - slice: the slice to move the element from - index: the index of the element to move

Returns: - []T: the new slice

Example:

indices := []int{1, 2, 3, 4, 5}
IndexMoveUp(indices, 2)
fmt.Println(indices) // [1, 3, 2, 4, 5]

func IndexRemove

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

IndexRemove removes the element at the given index from the slice.

Parameters: - slice: the slice to remove the element from. - index: the index of the element to remove.

Returns: - []T: a new slice with the element at the given index removed.

Business Logic: - If the index is out of bounds, the original slice is returned unchanged. - This function does not panic on an out-of-bounds index.

Example:

arr := []int{1, 2, 3, 4}
result := IndexRemove(arr, 2)
// result is now [1, 2, 4]

Types

This section is empty.

Jump to

Keyboard shortcuts

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