northwang-lucky / sliceutil

Implementing array manipulation functions in JavaScript in Golang

Home Page:https://pkg.go.dev/github.com/wyb199877/sliceutil

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Slice Util

Implementing array manipulation functions in JavaScript in Golang

Usage

English | 简体中文

Import

import "github.com/wyb199877/sliceutil"

Operation Functions

Foreach

Iterate over the elements in the slice

func Foreach[T any](
  slice []T, 
  action func(item T, index int, slice []T)
)
sl := []int{1, 2, 3}
sliceutil.Foreach(sl, func(it int, idx int, s []int) {
  // ... do something
})

Map

Conversion of elements in slices

func Map[T, R any](
  slice []T,
  action func(item T, index int, slice []T) R,
) []R
sl := []int{1, 2, 3}
strs := sliceutil.Map(sl, func(it int, idx int, s []int) string {
  return strconv.Itoa(it)
})
// strs = ["1" "2" "3"]

Filter

Slice element filter

func Filter[T any](
  slice []T,
  action func(value T, index int, slice []T) bool,
) []T 
sl := []int{1, 2, 3}
odds := sliceutil.Filter(sl, func(it int, idx int, s []int) bool {
  return it%2 != 0
})
// odds = [1 3]

Reduce

Slice element accumulator (no initial value implemented)

func Reduce[T any](
  slice []T,
  action func(result T, item T, index int, slice []T) T,
) T
sl := []int{1, 2, 3}
sum := sliceutil.Reduce(sl, func(ret int, it int, idx int, s []int) int {
  return ret + it
})
// sum = 6

Search Functions

Find

Find the first element that satisfies the condition in the slice

func Find[T any](
  slice []T,
  action func(item T, index int, slice []T) bool,
) (t T, hasFound bool)
sl := []int{1, 2, 3}
target, hasFound := sliceutil.Find(sl, func(it int, idx int, s []int) bool {
  return it%2 != 0
})
// target = 1, hasFound = true

target, hasFound = sliceutil.Find(sl, func(it int, idx int, s []int) bool {
  return it > 3
})
// target = 0, hasFound = false

FindIndex

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

func FindIndex[T any](
  slice []T,
  action func(item T, index int, slice []T) bool,
) int
sl := []int{1, 2, 3}
index := sliceutil.FindIndex(sl, func(it int, idx int, s []int) bool {
  return it%2 != 0
})
// index = 0

index = sliceutil.FindIndex(sl, func(it int, idx int, s []int) bool {
  return it > 3
})
// index = -1

IndexOf

Finds the index of the first element that equal to the target value in the slice

func IndexOf[T comparable](slice []T, target T) int
sl := []int{1, 2, 3}
index := sliceutil.IndexOf(sl, 1)
// index = 0

index = sliceutil.IndexOf(sl, 4)
// index = -1

LastIndexOf

Finds the index of the last element that equal to the target value in the slice

func LastIndexOf[T comparable](slice []T, target T) int
sl := []int{1, 2, 3, 1}
index := sliceutil.LastIndexOf(sl, 1)
// index = 3

index = sliceutil.LastIndexOf(sl, 4)
// index = -1

Includes

Determine whether there is an element equal to the target value in the slice

func Includes[T comparable](slice []T, target T) bool
sl := []int{1, 2, 3}
isExist := sliceutil.Includes(sl, 1)
// isExist = true

isExist = sliceutil.Includes(sl, 4)
// isExist = false

Some

Determine if there is an element in the slice that satisfies the condition

func Some[T any](
  slice []T,
  action func(item T, index int, slice []T) bool,
) bool
sl := []int{1, 3, 5}
isExist := sliceutil.Some(sl, func(it int, idx int, s []int) bool {
  return it%2 != 0
})
// isExist = true

isExist = sliceutil.Some(sl, func(it int, idx int, s []int) bool {
  return it%2 == 0
})
// isExist = false

Every

Determines whether all elements in the slice satisfy the condition

func Every[T any](
  slice []T,
  action func(item T, index int, slice []T) bool,
) bool
sl := []int{1, 3, 5}
isEvery := sliceutil.Every(sl, func(it int, idx int, s []int) bool {
  return it%2 != 0
})
// isEvery = true

isEvery = sliceutil.Every(sl, func(it int, idx int, s []int) bool {
  return it%2 == 0
})
// isEvery = false

Modification Functions

Functions under this heading operate directly on the memory space to which the slice address points

Insert

Insert elements at the position of index in the slice

func Insert[T any](sp *[]T, index int, items ...T)
sl := []int{1, 3, 4}
sliceutil.Insert(&sl, 1, 2)
// sl = [1 2 3 4]

Remove

Removes an element from a slice at the position of index (make sure there is an element at that position)

func Remove[T any](sp *[]T, index int)
sl := []int{1, 3, 4}
sliceutil.Remove(&sl, 1)
// sl = [1 4]

Push

Insert elements at the end of slice

func Push[T any](sp *[]T, items ...T)
sl := []int{1, 2, 3}
sliceutil.Push(&sl, 4)
// sl = [1 2 3 4]

Pop

Pops up and returns the last element of a slice (do not operate on empty slices)

func Pop[T any](sp *[]T) T
sl := []int{1, 2, 3}
last := sliceutil.Pop(&sl)
// last = 3, sl = [1 2]

Unshift

Insert elements at the start of slice

func Unshift[T any](sp *[]T, items ...T)
sl := []int{2, 3, 4}
sliceutil.Unshift(&sl, 1)
// sl = [1 2 3 4]

Shift

Pops up and returns the first element of a slice (do not operate on empty slices)

func Shift[T any](sp *[]T) T
sl := []int{1, 2, 3}
first := sliceutil.Shift(&sl)
// first = 1, sl = [2 3]

Reversal functions

Reverse

Reverse the order of the elements in the slice (only copy, no memory space operation)

func Reverse[T any](slice []T) []T
sl := []int{1, 2, 3}
sl = sliceutil.Reverse(sl)
// sl = [3 2 1]

ReverseSelf

Reverse the order of the elements in the slice (operate the memory space)

func ReverseSelf[T any](slice []T)
sl := []int{1, 2, 3}
sliceutil.ReverseSelf(sl)
// sl = [3 2 1]

Conversion functions

ToMap

Convert slice to map

func ToMap[T any, K comparable, V any](
  slice []T,
  key func(item T) K,
  value func(item T) V,
) map[K]V 
sl := []string{"a", "b", "c"}
mp := sliceutil.ToMap(sl, func(it string) string {
  return strings.ToUpper(it)
}, func(it string) string {
  return it
})
// mp = map[string]string{"A":"a", "B":"b", "C":"c"}

Copy Functions

Copy

Copy slice

func Copy[T any](slice []T) []T
sl := []int{1, 2, 3, 4}
copied := sliceutil.Copy(sl)
copied[0] = 99
// sl = [1 2 3 4] copied = [99 2 3 4]

Cut

Cut, return a copy

func Cut[T any](slice []T, start int, end int) []T
sl := []int{1, 2, 3, 4}
cut := sliceutil.Cut(sl, 1, 3)
cut[0] = 99
// sl = [1, 2, 3, 4] cut = [99, 3]

CutFromFirst

Cut from first element, return a copy

func CutFromFirst[T any](slice []T, end int) []T
sl := []int{1, 2, 3, 4}
cut := sliceutil.CutFromFirst(sl, 3)
cut[0] = 99
// sl = [1 2 3 4] cut = [99 2 3]

CutToLast

Cut to last element, return a copy

func CutToLast[T any](slice []T, start int) []T
sl := []int{1, 2, 3, 4}
cut := sliceutil.CutToLast(sl, 1)
cut[0] = 99
// sl = [1 2 3 4] cut = [99 3 4]

About

Implementing array manipulation functions in JavaScript in Golang

https://pkg.go.dev/github.com/wyb199877/sliceutil

License:MIT License


Languages

Language:Go 100.0%