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

merge: merge main branch and refector slice func with generics

This commit is contained in:
dudaodong
2022-01-24 11:10:12 +08:00
13 changed files with 198 additions and 11 deletions

View File

@@ -13,9 +13,9 @@ import (
)
// Contain check if the value is in the iterable type or not
func Contain[T comparable](slice []T, value T) bool {
func Contain[T any](slice []T, value T) bool {
for _, v := range slice {
if v == value {
if reflect.DeepEqual(v, value) {
return true
}
}
@@ -71,11 +71,14 @@ func Chunk[T any](slice []T, size int) [][]T {
return res
}
// Difference creates an slice of whose element in slice1 but not in slice2
func Difference[T comparable](slice1, slice2 []T) []T {
var res []T
for _, v := range slice1 {
if !Contain(slice2, v) {
// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey
func Compact[T any](slice []T) []T {
res := make([]T, 0, 0)
for _, v := range slice {
if !reflect.DeepEqual(v, nil) &&
!reflect.DeepEqual(v, false) &&
!reflect.DeepEqual(v, "") &&
!reflect.DeepEqual(v, 0) {
res = append(res, v)
}
}
@@ -83,6 +86,46 @@ func Difference[T comparable](slice1, slice2 []T) []T {
return res
}
// Concat creates a new slice concatenating slice with any additional slices and/or values.
func Concat[T any](slice []T, values ...[]T) []T {
res := append([]T{}, slice...)
for _, v := range values {
res = append(res, v...)
}
return res
}
// Difference creates an slice of whose element in slice but not in comparedSlice
func Difference[T comparable](slice, comparedSlice []T) []T {
var res []T
for _, v := range slice {
if !Contain(comparedSlice, v) {
res = append(res, v)
}
}
return res
}
// 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, t T) T) []T {
orginSliceAfterMap := Map(slice, iteratee)
comparedSliceAfterMap := Map(comparedSlice, iteratee)
res := make([]T, 0, 0)
for i, v := range orginSliceAfterMap {
if !Contain(comparedSliceAfterMap, v) {
res = append(res, slice[i])
}
}
return res
}
// Every return true if all of the values in the slice pass the predicate function.
// The fn function signature should be func(int, T) bool .
func Every[T any](slice []T, fn func(index int, t T) bool) bool {

View File

@@ -29,6 +29,7 @@ func TestChunk(t *testing.T) {
assert := internal.NewAssert(t, "TestChunk")
arr := []string{"a", "b", "c", "d", "e"}
r1 := [][]string{{"a"}, {"b"}, {"c"}, {"d"}, {"e"}}
assert.Equal(r1, Chunk(arr, 1))
@@ -45,6 +46,27 @@ func TestChunk(t *testing.T) {
assert.Equal(r5, Chunk(arr, 5))
}
func TestCompact(t *testing.T) {
assert := internal.NewAssert(t, "TesCompact")
assert.Equal([]int{}, Compact([]int{0}))
assert.Equal([]int{1, 2, 3}, Compact([]int{0, 1, 2, 3}))
assert.Equal([]string{}, Compact([]string{""}))
assert.Equal([]string{" "}, Compact([]string{" "}))
assert.Equal([]string{"a", "b", "0"}, Compact([]string{"", "a", "b", "0"}))
assert.Equal([]bool{true, true}, Compact([]bool{false, true, true}))
}
func TestConcat(t *testing.T) {
assert := internal.NewAssert(t, "Concat")
// assert.Equal([]int{0}, Concat([]int{}, 0))
// assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, 4, 5))
assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, []int{4, 5}))
assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, []int{4}, []int{5}))
// assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, []int{4}, 5))
}
func TestEvery(t *testing.T) {
nums := []int{1, 2, 3, 5}
isEven := func(i, num int) bool {
@@ -377,6 +399,17 @@ func TestDifference(t *testing.T) {
assert.Equal([]int{1, 2, 3}, Difference(s1, s2))
}
func TestDifferenceBy(t *testing.T) {
assert := internal.NewAssert(t, "TestDifferenceBy")
s1 := []int{1, 2, 3, 4, 5} //after add one: 2 3 4 5 6
s2 := []int{3, 4, 5} //after add one: 4 5 6
addOne := func(i int, v int) int {
return v + 1
}
assert.Equal([]int{1, 2}, DifferenceBy(s1, s2, addOne))
}
func TestSortByFielDesc(t *testing.T) {
assert := internal.NewAssert(t, "TestWithout")