1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 09:12:28 +08:00

update: update type constraint any -> Comparable. (#37)

* update: update type constraint any -> Comparable.

* update: update type constraint any -> Comparable.

Co-authored-by: zhanghu <305835360@qq.com>
This commit is contained in:
tangqiu0205
2022-07-07 16:31:48 +08:00
committed by GitHub
parent 3625921912
commit 56b6844a2d

View File

@@ -13,9 +13,9 @@ import (
) )
// Contain check if the value is in the slice or not // 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 { for _, v := range slice {
if reflect.DeepEqual(v, value) { if v == value {
return true return true
} }
} }
@@ -24,7 +24,7 @@ func Contain[T any](slice []T, value T) bool {
} }
// ContainSubSlice check if the slice contain subslice or not // 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 { for _, v := range subslice {
if !Contain(slice, v) { if !Contain(slice, v) {
return false 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 // DifferenceBy it accepts iteratee which is invoked for each element of slice
// and values to generate the criterion by which they're compared. // and values to generate the criterion by which they're compared.
// like lodash.js differenceBy: https://lodash.com/docs/4.17.15#differenceBy, // 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) orginSliceAfterMap := Map(slice, iteratee)
comparedSliceAfterMap := Map(comparedSlice, 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. // Unique remove duplicate elements in slice.
func Unique[T any](slice []T) []T { func Unique[T comparable](slice []T) []T {
if len(slice) == 0 { if len(slice) == 0 {
return []T{} return []T{}
} }
@@ -569,7 +569,7 @@ func Unique[T any](slice []T) []T {
v := slice[i] v := slice[i]
skip := true skip := true
for j := range res { for j := range res {
if reflect.DeepEqual(v, res[j]) { if v == res[j] {
skip = false skip = false
break break
} }
@@ -583,7 +583,7 @@ func Unique[T any](slice []T) []T {
} }
// UniqueBy call iteratee func with every item of slice, then remove duplicated. // 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 { if len(slice) == 0 {
return []T{} 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. // 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 { if len(slices) == 0 {
return []T{} 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. // 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 { if len(slices) == 0 {
return []T{} return []T{}
} }
@@ -649,7 +649,7 @@ func Intersection[T any](slices ...[]T) []T {
} }
// SymmetricDifference oppoiste operation of intersection function // SymmetricDifference oppoiste operation of intersection function
func SymmetricDifference[T any](slices ...[]T) []T { func SymmetricDifference[T comparable](slices ...[]T) []T {
if len(slices) == 0 { if len(slices) == 0 {
return []T{} return []T{}
} }
@@ -765,7 +765,7 @@ func SortByField(slice any, field string, sortType ...string) error {
} }
// Without creates a slice excluding all given values // 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 { if len(values) == 0 || len(slice) == 0 {
return slice 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 // 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. // 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 { for i, v := range slice {
if reflect.DeepEqual(v, value) { if v == value {
return i 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 // 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. // 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-- { for i := len(slice) - 1; i > 0; i-- {
if reflect.DeepEqual(value, slice[i]) { if value == slice[i] {
return i return i
} }
} }