array

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2023 License: MIT Imports: 1 Imported by: 0

README

JS Arrays in Go

This project is a simple go library that will help working with arrays easy in go. I started this project to learn go. I am an intermediate JS developer. I want to learn go and I thought this would be a good way to learn go. I am open to suggestions and contributions. I am also open to any feedback. I am trying to build 'arrays' like javascript in 'go'

Table of Contents

Installation

  • go mod download

Usage

  • To test
    • go test

Contribution

  • I am actively looking for participants to contribute to this project. Please feel free to fork this project and make a pull request. I will review the pull request and merge it if it is satisfactory.

License

  • This project is licensed under the MIT License - see the LICENSE.md file for details

Documentation

Overview

Package array provides a generic array implementation in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reduce added in v1.0.8

func Reduce[T interface{}, K any](a Array[T], cb func(pV K, cV T, index int) K, initial K) K

Reduce iterates over the array and calls the callback function for each element. The callback function is passed the previous value, current value and index of the element. It returns the final value.

Types

type Array

type Array[T interface{}] []T

Array is a generic data structure that can hold elements of any type.

func Map added in v1.0.8

func Map[T interface{}, K any](a Array[T], cb func(value T, index int) K) Array[K]

Map iterates over the array and calls the callback function for each element. The callback function is passed the value and index of the element. It returns a new Array containing the elements returned by the callback function.

func New

func New[T interface{}](arr ...T) Array[T]

New creates a new Array[T] from a variadic number of arguments. It accepts zero or more arguments of any type and returns an Array containing these elements.

func (Array[T]) Filter added in v1.0.8

func (a Array[T]) Filter(cb func(value T, index int) bool) Array[T]

Filter iterates over the array and calls the callback function for each element. The callback function is passed the value and index of the element. It returns a new Array containing the elements for which the callback function returned true.

func (Array[T]) Find added in v1.0.9

func (a Array[T]) Find(findFunc func(item T, index int) bool) (T, int)

func (Array[T]) FindIndex added in v1.0.9

func (a Array[T]) FindIndex(findFunc func(item T, index int) bool) int

FindIndex returns the index of the first element in the array that satisfies the provided testing function.

func (Array[T]) FindLast added in v1.0.9

func (a Array[T]) FindLast(findFunc func(item T, index int) bool) (T, int)

FindLast returns the last element along with index in the array that satisfies the provided testing function.

func (Array[T]) ForEach added in v1.0.8

func (a Array[T]) ForEach(cb func(value T, index int))

ForEach iterates over the array and calls the callback function for each element. The callback function is passed the value and index of the element.

func (Array[T]) Go added in v1.0.8

func (a Array[T]) Go() []T

Go returns the underlying slice of the array.

func (*Array[T]) Pop

func (a *Array[T]) Pop() T

Pop removes the last element from the array and returns it. If the array is empty, it panics with the message "Array is empty". It returns the removed element.

func (*Array[T]) Push

func (a *Array[T]) Push(arr ...T) int

Push adds a variadic number of arguments to the end of the array. It accepts zero or more arguments of any type and appends them to the end of the array. It returns the new length of the array.

func (*Array[T]) Sort

func (a *Array[T]) Sort(compareFn ...sortParams[T])

compareFn is an optional parameter. only one argument is needed

func (*Array[T]) Splice

func (a *Array[T]) Splice(index int, count int) Array[T]

Splice removes a number of elements from the array starting at a given index. It accepts an index and a count as arguments. It removes 'count' elements from the array starting at 'index'. It returns a new Array containing the removed elements.

Jump to

Keyboard shortcuts

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