ziggornif / array-utils

Provides useful Go array operations functions such as can be found in the javascript and java languages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Go array utils

GitHub go.mod Go version of a Go module GoDoc reference example

CodeFactor Go Report Card test


Description

This package provides useful array operations functions such as can be found in the javascript and java languages.

The native array is encapsulated in a Array struct for chaining methods and doing "functional" programming.

Requirements

Go >= 1.18 version.

Installation

To install it, run:

go get github.com/drouian-m/array-utils

Usage

Create a new Array

package main
import (
	"github.com/drouian-m/array-utils"
)

func main() {
	arr := array.NewArray([]int{1, 2, 3})
}

Available operations

  • Find : Find a value in the current array
  • FindIndex : Find value index in the current array
  • Filter : Filter array values (chainable)
  • Map : Create a new array populated with the result of calling the input func on each array element (chainable)
  • ForEach : Execute the input function on each array element

Retrieve native Go array

package main
import (
	"github.com/drouian-m/array-utils"
)

func main() {
	arr := array.NewArray([]int{1, 2, 3})
	arr.ToNative() // => return []int array
}

Chaining example

package main
import (
	"fmt"
	"github.com/drouian-m/array-utils"
)

func main() {
	myarr := array.NewArray([]int{1, 2, 3, 4, 5})
	result := myarr.Map(func(val int) int {
		return val * 3
	}).Filter(func(val int) bool {
		return val % 2 == 0
	})
	fmt.Println(result.ToNative()) // => [6 12]
}

About

Provides useful Go array operations functions such as can be found in the javascript and java languages

License:MIT License


Languages

Language:Go 96.9%Language:Makefile 3.1%