17 KiB
Slice
Package slice implements some functions to manipulate slice.
Source:
Usage:
import (
"github.com/duke-git/lancet/v2/slice"
)
Index
Documentation
Contain
Check if the value is in the slice or not.
Signature:
func Contain[T any](slice []T, value T) bool
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
res := slice.Contain([]string{"a", "b", "c"}, "a")
fmt.Println(res) //true
}
ContainSubSlice
Check if the slice contain subslice or not.
Signature:
func ContainSubSlice[T any](slice, subslice []T) bool
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
res := slice.ContainSubSlice([]string{"a", "b", "c"}, []string{"a", "b"})
fmt.Println(res) //true
}
Chunk
Creates an slice of elements split into groups the length of `size`.
Signature:
func Chunk[T any](slice []T, size int) [][]T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
arr := []string{"a", "b", "c", "d", "e"}
res := slice.Chunk(InterfaceSlice(arr), 3)
fmt.Println(res) //[][]any{{"a", "b", "c"}, {"d", "e"}}
}
Compact
Creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey.
Signature:
func Compact[T any](slice []T) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
res := slice.Compact([]int{0, 1, 2, 3})
fmt.Println(res) //[]int{1, 2, 3}
}
Concat
Creates a new slice concatenating slice with any additional slices and/or values.
Signature:
func Concat[T any](slice []T, values ...[]T) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
res1 := slice.Concat([]int{1, 2, 3}, 4, 5)
fmt.Println(res1) //[]int{1, 2, 3, 4, 5}
res2 := slice.Concat([]int{1, 2, 3}, []int{4, 5})
fmt.Println(res2) //[]int{1, 2, 3, 4, 5}
}
Count
Count iterates over elements of slice, returns a count of all matched elements.
Signature:
func Count[T any](slice []T, predicate func(index int, t T) bool) int
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4, 5, 6}
evenFunc := func(i, num int) bool {
return (num % 2) == 0
}
res := slice.Count(nums, evenFunc)
fmt.Println(res) //3
}
Difference
Creates an slice of whose element not included in the other given slice.
Signature:
func Difference[T comparable](slice, comparedSlice []T) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
s1 := []int{1, 2, 3, 4, 5}
s2 := []int{4, 5, 6}
res := slice.Difference(s1, s2)
fmt.Println(res) //[]int{1, 2, 3}
}
DifferenceBy
DifferenceBy accepts iteratee func which is invoked for each element of slice and values to generate the criterion by which they're compared.
Signature:
func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
s1 := []int{1, 2, 3, 4, 5}
s2 := []int{4, 5, 6}
addOne := func(i int, v int) int {
return v + 1
}
res := slice.DifferenceBy(s1, s2, addOne)
fmt.Println(res) //[]int{1, 2}
}
DifferenceWith
DifferenceWith accepts comparator which is invoked to compare elements of slice to values. The order and references of result values are determined by the first slice.
Signature:
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
s1 := []int{1, 2, 3, 4, 5}
s2 := []int{4, 5, 6, 7, 8}
isDouble := func(v1, v2 int) bool {
return v2 == 2*v1
}
res := slice.DifferenceWith(s1, s2, isDouble)
fmt.Println(res) //[]int{1, 5}
}
DeleteAt
Delete the element of slice from start index to end index - 1.
Signature:
func DeleteAt[T any](slice []T, start int, end ...int)
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
res1 := slice.DeleteAt([]string{"a", "b", "c", "d", "e"}, 3)
fmt.Println(res1) //[]string{"a", "b", "c", "e"}
res2 := slice.DeleteAt([]string{"a", "b", "c", "d", "e"}, 0, 2)
fmt.Println(res2) //[]string{"c", "d", "e"}
}
Drop
Creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0.
Signature:
func Drop[T any](slice []T, n int) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
res1 := slice.Drop([]int{}, 0)
fmt.Println(res1) //[]int{}
res2 := slice.Drop([]int{1, 2, 3, 4, 5}, 1)
fmt.Println(res2) //[]int{2, 3, 4, 5}
res3 := slice.Drop([]int{1, 2, 3, 4, 5}, -1)
fmt.Println(res3) //[]int{1, 2, 3, 4}
}
Every
Return true if all of the values in the slice pass the predicate function.
Signature:
func Every[T any](slice []T, predicate func(index int, t T) bool) bool
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
res := slice.Every(nums, isEven)
fmt.Println(res) //false
}
Filter
Return all elements which match the function.
Signature:
func Filter[T any](slice []T, predicate func(index int, t T) bool) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
res := slice.Filter(nums, isEven)
fmt.Println(res) //[]int{2, 4}
}
Find
Iterates over elements of slice, returning the first one that passes a truth test on function.
Signature:
func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool)
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
res, ok := slice.Find(nums, even)
fmt.Println(res) //2
fmt.Println(ok) //true
}
FindLast
iterates over elements of slice from end to begin, returning the last one that passes a truth test on function.
Signature:
func FindLast[T any](slice []T, predicate func(index int, t T) bool) (*T, bool)
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
res, ok := slice.FindLast(nums, even)
fmt.Println(res) //4
fmt.Println(ok) //true
}
FlattenDeep
flattens slice recursive.
Signature:
func FlattenDeep(slice any) any
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
arr := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
res := slice.FlattenDeep(arr)
fmt.Println(res) //[]string{"a", "b", "c", "d"}
}
ForEach
Iterates over elements of slice and invokes function for each element.
Signature:
func ForEach[T any](slice []T, iteratee func(index int, t T))
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
numbers := []int{1, 2, 3, 4, 5}
var numbersAddTwo []int
slice.ForEach(numbers, func(index int, value int) {
numbersAddTwo = append(numbersAddTwo, value+2)
})
fmt.Println(numbersAddTwo) //[]int{3, 4, 5, 6, 7}
}
GroupBy
Iterates over elements of the slice, each element will be group by criteria, returns two slices.
Signature:
func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T)
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4, 5, 6}
evenFunc := func(i, num int) bool {
return (num % 2) == 0
}
even, odd := slice.GroupBy(nums, evenFunc)
fmt.Println(even) //[]int{2, 4, 6}
fmt.Println(odd) //]int{1, 3, 5}
}
GroupWith
Return a map composed of keys generated from the results of running each element of slice thru iteratee.
Signature:
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []float64{6.1, 4.2, 6.3}
floor := func(num float64) float64 {
return math.Floor(num)
}
res := slice.GroupWith(nums, floor)
fmt.Println(res) //map[float64][]float64{ 4: {4.2}, 6: {6.1, 6.3},}
}
IntSlice
Convert interface slice to int slice.
Signature:
func IntSlice(slice any) []int
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
var nums = []any{1, 2, 3}
res := slice.IntSlice(nums)
fmt.Println(res) //[]int{1, 2, 3}
}
InterfaceSlice
Convert value to interface slice.
Signature:
func InterfaceSlice(slice any) []any
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
var nums = []int{}{1, 2, 3}
res := slice.InterfaceSlice(nums)
fmt.Println(res) //[]any{1, 2, 3}
}
Intersection
Creates a slice of unique values that included by all slices.
Signature:
func Intersection[T any](slices ...[]T) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
s1 := []int{1, 2, 2, 3}
s2 := []int{1, 2, 3, 4}
res := slice.Intersection(s1, s2),
fmt.Println(res) //[]int{1, 2, 3}
}
InsertAt
insert the element into slice at index.
Signature:
func InsertAt[T any](slice []T, index int, value any) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
s := []string{"a", "b", "c"}
res1, _ := slice.InsertAt(s, 0, "1")
fmt.Println(res1) //[]string{"1", "a", "b", "c"}
res2, _ := slice.InsertAt(s, 3, []string{"1", "2", "3"})
fmt.Println(res2) //[]string{"a", "b", "c", "1", "2", "3"}
}
Map
Creates an slice of values by running each element in slice thru function.
Signature:
func Map[T any, U any](slice []T, iteratee func(index int, t T) U) []U
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4}
multiplyTwo := func(i, num int) int {
return num * 2
}
res := slice.Map(nums, multiplyTwo)
fmt.Println(res) //[]int{2, 4, 6, 8}
}
Reverse
Reverse the elements order in slice.
Signature:
func Reverse[T any](slice []T)
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4}
slice.Reverse(nums)
fmt.Println(res) //[]int{4, 3, 2, 1}
}
Reduce
Reduce slice.
Signature:
func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4}
reduceFunc := func(i, v1, v2 int) int {
return v1 + v2
}
res := slice.Reduce(nums, reduceFunc, 0)
fmt.Println(res) //10
}
Shuffle
Creates an slice of shuffled values.
Signature:
func Shuffle[T any](slice []T) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4, 5}
res := slice.Shuffle(nums)
fmt.Println(res) //3,1,5,4,2
}
SortByField
Sort struct slice by field. Slice element should be struct, field type should be int, uint, string, or bool. Default sort type is ascending (asc), if descending order, set sortType to desc
Signature:
func SortByField(slice any, field string, sortType ...string) error
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
type student struct {
name string
age int
}
students := []student{
{"a", 10},
{"b", 15},
{"c", 5},
{"d", 6},
}
err := slice.SortByField(students, "age", "desc")
if err != nil {
fmt.Println(err)
}
fmt.Println(students)
// []students{
// {"b", 15},
// {"a", 10},
// {"d", 6},
// {"c", 5},
// }
}
Some
Return true if any of the values in the list pass the predicate function.
Signature:
func Some[T any](slice []T, predicate func(index int, t T) bool) bool
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
res := slice.Some(nums, isEven)
fmt.Println(res) //true
}
StringSlice
Convert interface slice to string slice.
Signature:
func StringSlice(slice any) []string
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
var s = []any{"a", "b", "c"}
res := slice.StringSlice(s)
fmt.Println(res) //[]string{"a", "b", "c"}
}
Unique
Remove duplicate elements in slice.
Signature:
func Unique[T any](slice []T) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
res := slice.Unique([]int{1, 2, 2, 3})
fmt.Println(res) //[]int{1, 2, 3}
}
Unique
Creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
Signature:
func Union[T any](slices ...[]T) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
s1 := []int{1, 3, 4, 6}
s2 := []int{1, 2, 5, 6}
res := slice.Union(s1, s2)
fmt.Println(res) //[]int{1, 3, 4, 6, 2, 5}
}
UpdateAt
Update the slice element at index. if param index < 0 or index >= len(slice), will return error.
Signature:
func UpdateAt[T any](slice []T, index int, value T) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
s := []string{"a", "b", "c"}
res1, _ := slice.UpdateAt(s, 0, "1")
fmt.Println(res1) //[]string{"1", "b", "c"}
}
Without
Creates a slice excluding all given values.
Signature:
func Without[T any](slice []T, values ...T) []T
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
res := slice.Without([]int{1, 2, 3, 4, 5}, 1, 2)
fmt.Println(res) //[]int{3, 4, 5}
}