array

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2025 License: MIT Imports: 1 Imported by: 0

README

Array Method Package

Go Reference

This package provides convenient methods for arrays and slices in Go, inspired by JavaScript's Array methods. It allows for easy operations such as filtering, mapping, searching, and more.

Installation

To use this package, install it with the following command:

go get github.com/hiroky1983/array-method/array

Usage

Below are examples of how to use the main features of this package.

Filter

Extract elements from a slice that meet a condition.

import (
    "github.com/hiroky1983/array-method/array"
)

nums := []int{1, 2, 3, 4, 5}
evens := array.Filter(nums, func(n int) bool {
    return n%2 == 0
})
// evens will be [2, 4]
Map

Apply a function to each element of a slice to create a new slice.

people := []Person{
    {"Alice", 25},
    {"Bob", 30},
}
names := array.Map(people, func(p Person) string {
    return p.Name
})
// names will be ["Alice", "Bob"]
Find

Find the first element in a slice that meets a condition.

person := array.Find(people, func(p Person) bool {
    return p.Name == "Alice"
})
// person will be &{Alice 25}
FindIndex

Find the index of the first element in a slice that meets a condition.

index := array.FindIndex(people, func(p Person) bool {
    return p.Name == "Bob"
})
// index will be 1
ForEach

Perform an action for each element in a slice.

array.ForEach(people, func(p Person) {
    fmt.Println(p.Name)
})
// Output: Alice Bob
Reduce

Reduce a slice to a single value.

result := array.Reduce(nums, func(prev int, current int) int {
    return prev + current
}, 0)
// result will be 15
Some

Check if at least one element in a slice meets a condition.

result := array.Some(nums, func(n int) bool {
    return n%2 == 0
})
// result will be true

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

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

Filter applies a condition function to each element of a slice and returns a new slice containing only the elements that satisfy the condition. If the input slice is nil, returns nil. If an element contains nil pointer fields, it is skipped. The original slice is not modified.

func Find

func Find[T any](slice []T, condition func(T) bool) *T

Find searches for the first element in the provided slice that satisfies the given condition. If the input slice is nil, it returns nil.

func FindIndex

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

FindIndex searches for the index of the first element in the provided slice that satisfies the given condition. If the input slice is nil, it returns -1.

func ForEach

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

ForEach executes a given callback function for each element in the provided slice. If the input slice is nil, the function does nothing.

func Map

func Map[Input any, Output any](slice []Input, transform func(Input) Output) []Output

Map applies a given transformation function to each element of the provided slice and returns a new slice containing the results. If the input slice is nil, it returns nil.

func Reduce added in v0.0.6

func Reduce[T any, U any](slice []T, reducer func(U, T) U, initial U) U

Reduce applies a reducer function to each element of the slice and returns the accumulated result. If the input slice is nil, it returns the initial value.

func Some

func Some[T any](slice []T, condition func(T) bool) bool

Types

This section is empty.

Jump to

Keyboard shortcuts

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