mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
merge main branch
This commit is contained in:
@@ -13,9 +13,9 @@ import (
|
||||
)
|
||||
|
||||
// Contain check if the value is in the slice or not
|
||||
func Contain[T any](slice []T, value T) bool {
|
||||
func Contain[T comparable](slice []T, value T) bool {
|
||||
for _, v := range slice {
|
||||
if reflect.DeepEqual(v, value) {
|
||||
if v == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ func Contain[T any](slice []T, value T) bool {
|
||||
}
|
||||
|
||||
// ContainSubSlice check if the slice contain subslice or not
|
||||
func ContainSubSlice[T any](slice, subslice []T) bool {
|
||||
func ContainSubSlice[T comparable](slice, subslice []T) bool {
|
||||
for _, v := range subslice {
|
||||
if !Contain(slice, v) {
|
||||
return false
|
||||
@@ -108,7 +108,7 @@ func Difference[T comparable](slice, comparedSlice []T) []T {
|
||||
// DifferenceBy it accepts iteratee which is invoked for each element of slice
|
||||
// and values to generate the criterion by which they're compared.
|
||||
// like lodash.js differenceBy: https://lodash.com/docs/4.17.15#differenceBy,
|
||||
func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T {
|
||||
func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T {
|
||||
orginSliceAfterMap := Map(slice, iteratee)
|
||||
comparedSliceAfterMap := Map(comparedSlice, iteratee)
|
||||
|
||||
@@ -558,7 +558,7 @@ func UpdateAt[T any](slice []T, index int, value T) []T {
|
||||
}
|
||||
|
||||
// Unique remove duplicate elements in slice.
|
||||
func Unique[T any](slice []T) []T {
|
||||
func Unique[T comparable](slice []T) []T {
|
||||
if len(slice) == 0 {
|
||||
return []T{}
|
||||
}
|
||||
@@ -569,7 +569,7 @@ func Unique[T any](slice []T) []T {
|
||||
v := slice[i]
|
||||
skip := true
|
||||
for j := range res {
|
||||
if reflect.DeepEqual(v, res[j]) {
|
||||
if v == res[j] {
|
||||
skip = false
|
||||
break
|
||||
}
|
||||
@@ -583,7 +583,7 @@ func Unique[T any](slice []T) []T {
|
||||
}
|
||||
|
||||
// UniqueBy call iteratee func with every item of slice, then remove duplicated.
|
||||
func UniqueBy[T any](slice []T, iteratee func(item T) T) []T {
|
||||
func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T {
|
||||
if len(slice) == 0 {
|
||||
return []T{}
|
||||
}
|
||||
@@ -598,7 +598,7 @@ func UniqueBy[T any](slice []T, iteratee func(item T) T) []T {
|
||||
}
|
||||
|
||||
// Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
||||
func Union[T any](slices ...[]T) []T {
|
||||
func Union[T comparable](slices ...[]T) []T {
|
||||
if len(slices) == 0 {
|
||||
return []T{}
|
||||
}
|
||||
@@ -616,7 +616,7 @@ func Union[T any](slices ...[]T) []T {
|
||||
}
|
||||
|
||||
// Intersection creates a slice of unique values that included by all slices.
|
||||
func Intersection[T any](slices ...[]T) []T {
|
||||
func Intersection[T comparable](slices ...[]T) []T {
|
||||
if len(slices) == 0 {
|
||||
return []T{}
|
||||
}
|
||||
@@ -649,7 +649,7 @@ func Intersection[T any](slices ...[]T) []T {
|
||||
}
|
||||
|
||||
// SymmetricDifference oppoiste operation of intersection function
|
||||
func SymmetricDifference[T any](slices ...[]T) []T {
|
||||
func SymmetricDifference[T comparable](slices ...[]T) []T {
|
||||
if len(slices) == 0 {
|
||||
return []T{}
|
||||
}
|
||||
@@ -765,7 +765,7 @@ func SortByField(slice any, field string, sortType ...string) error {
|
||||
}
|
||||
|
||||
// Without creates a slice excluding all given values
|
||||
func Without[T any](slice []T, values ...T) []T {
|
||||
func Without[T comparable](slice []T, values ...T) []T {
|
||||
if len(values) == 0 || len(slice) == 0 {
|
||||
return slice
|
||||
}
|
||||
@@ -782,9 +782,9 @@ func Without[T any](slice []T, values ...T) []T {
|
||||
|
||||
// IndexOf returns the index at which the first occurrence of a value is found in a slice or return -1
|
||||
// if the value cannot be found.
|
||||
func IndexOf[T any](slice []T, value T) int {
|
||||
func IndexOf[T comparable](slice []T, value T) int {
|
||||
for i, v := range slice {
|
||||
if reflect.DeepEqual(v, value) {
|
||||
if v == value {
|
||||
return i
|
||||
}
|
||||
}
|
||||
@@ -794,9 +794,9 @@ func IndexOf[T any](slice []T, value T) int {
|
||||
|
||||
// LastIndexOf returns the index at which the last occurrence of a value is found in a slice or return -1
|
||||
// if the value cannot be found.
|
||||
func LastIndexOf[T any](slice []T, value T) int {
|
||||
func LastIndexOf[T comparable](slice []T, value T) int {
|
||||
for i := len(slice) - 1; i > 0; i-- {
|
||||
if reflect.DeepEqual(value, slice[i]) {
|
||||
if value == slice[i] {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user