1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

rename: rename InsertByIndex -> InsertAt, UpdateByIndex -> UpdateAt, DeleteByIndex -> DeleteAt for release 2.0

This commit is contained in:
dudaodong
2022-02-10 11:25:34 +08:00
parent 17d271190b
commit 5ae746a1f0
4 changed files with 42 additions and 40 deletions

View File

@@ -440,7 +440,7 @@ func Concat[T any](slice []T, values ...[]T) []T //creates a new slice concatena
func Difference[T comparable](slice1, slice2 []T) []T //creates an slice of whose element not included in the other given slice func Difference[T comparable](slice1, slice2 []T) []T //creates an slice of whose element not included in the other given slice
func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T //it accepts iteratee which is invoked for each element of slice and values to generate the criterion by which they're compared. func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T //it accepts iteratee which is invoked for each element of slice and values to generate the criterion by which they're compared.
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T //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. func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T //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.
func DeleteByIndex[T any](slice []T, start int, end ...int) []T //delete the element of slice from start index to end index - 1 func DeleteAt[T any](slice []T, start int, end ...int) []T //delete the element of slice from start index to end index - 1
func Drop[T any](slice []T, n int) []T //creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0 func Drop[T any](slice []T, n int) []T //creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0
func Every[T any](slice []T, predicate func(index int, t T) bool) bool //return true if all of the values in the slice pass the predicate function func Every[T any](slice []T, predicate func(index int, t T) bool) bool //return true if all of the values in the slice pass the predicate function
func None[T any](slice []T, predicate func(index int, t T) bool) bool // return true if all the values in the slice mismatch the criteria func None[T any](slice []T, predicate func(index int, t T) bool) bool // return true if all the values in the slice mismatch the criteria
@@ -452,7 +452,7 @@ func ForEach [T any] (slice []T, iteratee func(index int, t T)) //iterates over
func IntSlice(slice interface{}) ([]int, error) //convert value to int slice func IntSlice(slice interface{}) ([]int, error) //convert value to int slice
func InterfaceSlice(slice interface{}) []interface{} //convert value to interface{} slice func InterfaceSlice(slice interface{}) []interface{} //convert value to interface{} slice
func Intersection[T comparable](slices ...[]T) []T //creates a slice of unique values that included by all slices. func Intersection[T comparable](slices ...[]T) []T //creates a slice of unique values that included by all slices.
func InsertByIndex[T any](slice []T, index int, value interface{}) []T //insert the value or other slice into slice at index. func InsertAt[T any](slice []T, index int, value interface{}) []T //insert the value or other slice into slice at index.
func Map [T any, U any] (slice []T, iteratee func(index int, t T) U) []U //creates an slice of values by running each element of slice thru iteratee function. func Map [T any, U any] (slice []T, iteratee func(index int, t T) U) []U //creates an slice of values by running each element of slice thru iteratee function.
func Reverse[T any](slice []T)//revere slice func Reverse[T any](slice []T)//revere slice
func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T //creates an slice of values by running each element of slice thru iteratee function func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T //creates an slice of values by running each element of slice thru iteratee function
@@ -462,7 +462,7 @@ func Some[T any](slice []T, predicate func(index int, t T) bool) bool //return t
func StringSlice(slice interface{}) []string //convert value to string slice func StringSlice(slice interface{}) []string //convert value to string slice
func Unique[T comparable](slice []T) []T //remove duplicate elements in slice func Unique[T comparable](slice []T) []T //remove duplicate elements in slice
func Union[T comparable](slices ...[]T) []T //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons. func Union[T comparable](slices ...[]T) []T //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
func UpdateByIndex[T any](slice []T, index int, value T) []T //update the slice element at index. func UpdateAt[T any](slice []T, index int, value T) []T //update the slice element at index.
func Without[T comparable](slice []T, values ...T) []T //creates a slice excluding all given values func Without[T comparable](slice []T, values ...T) []T //creates a slice excluding all given values
func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) //iterate over elements of the slice, each element will be group by criteria, returns two slices func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) //iterate over elements of the slice, each element will be group by criteria, returns two slices
func Count[T any](slice []T, predicate func(index int, t T) bool) int // iterates over elements of slice, returns a count of all matched elements func Count[T any](slice []T, predicate func(index int, t T) bool) int // iterates over elements of slice, returns a count of all matched elements

View File

@@ -440,7 +440,7 @@ func Concat[T any](slice []T, values ...[]T) []T //连接values到slice中
func Difference[T comparable](slice1, slice2 []T) []T //返回切片其元素在slice1中不在slice2中 func Difference[T comparable](slice1, slice2 []T) []T //返回切片其元素在slice1中不在slice2中
func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T //将slice 和comparedSlice中每个元素调用iterateeFn后作比较如果不相等返回slice中的元素 func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T //将slice 和comparedSlice中每个元素调用iterateeFn后作比较如果不相等返回slice中的元素
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T //将slice 和comparedSlice中每个元素调用comparator后作比较如果为false返回slice中的元素 func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T //将slice 和comparedSlice中每个元素调用comparator后作比较如果为false返回slice中的元素
func DeleteByIndex[T any](slice []T, start int, end ...int) []T //删除切片中start到end位置的值(不包含end) func DeleteAt[T any](slice []T, start int, end ...int) []T //删除切片中start到end位置的值(不包含end)
func Drop[T any](slice []T, n int) []T //创建一个新切片当n大于0时删除原切片前n个元素当n小于0时删除原切片后n个元素 func Drop[T any](slice []T, n int) []T //创建一个新切片当n大于0时删除原切片前n个元素当n小于0时删除原切片后n个元素
func Every[T any](slice []T, predicate func(index int, t T) bool) bool//slice中所有元素都符合函数条件时返回true, 否则返回false func Every[T any](slice []T, predicate func(index int, t T) bool) bool//slice中所有元素都符合函数条件时返回true, 否则返回false
func None[T any](slice []T, predicate func(index int, t T) bool) bool //slice中所有元素都不符合函数条件时返回true, 否则返回false func None[T any](slice []T, predicate func(index int, t T) bool) bool //slice中所有元素都不符合函数条件时返回true, 否则返回false
@@ -452,7 +452,7 @@ func ForEach [T any] (slice []T, iteratee func(index int, t T)) //遍历切片
func IntSlice(slice interface{}) ([]int, error) //转成int切片 func IntSlice(slice interface{}) ([]int, error) //转成int切片
func InterfaceSlice(slice interface{}) []interface{} //转成interface{}切片 func InterfaceSlice(slice interface{}) []interface{} //转成interface{}切片
func Intersection[T comparable](slices ...[]T) []T //slice交集去重 func Intersection[T comparable](slices ...[]T) []T //slice交集去重
func InsertByIndex[T any](slice []T, index int, value interface{}) []T //在切片中index位置插入value func InsertAt[T any](slice []T, index int, value interface{}) []T //在切片中index位置插入value
func Map [T any, U any] (slice []T, iteratee func(index int, t T) U) []U //遍历切片,返回遍历结果 func Map [T any, U any] (slice []T, iteratee func(index int, t T) U) []U //遍历切片,返回遍历结果
func Reverse[T any](slice []T) //反转切片 func Reverse[T any](slice []T) //反转切片
func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T //切片reduce操作 func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T //切片reduce操作
@@ -462,7 +462,7 @@ func SortByField(slice interface{}, field string, sortType ...string) error //
func StringSlice(slice interface{}) []string //转为string切片 func StringSlice(slice interface{}) []string //转为string切片
func Unique[T comparable](slice []T) []T //去重切片 func Unique[T comparable](slice []T) []T //去重切片
func Union[T comparable](slices ...[]T) []T //slice并集, 去重 func Union[T comparable](slices ...[]T) []T //slice并集, 去重
func UpdateByIndex[T any](slice []T, index int, value T) []T //在切片中index位置更新value func UpdateAt[T any](slice []T, index int, value T) []T //在切片中index位置更新value
func Without[T comparable](slice []T, values ...T) []T //slice去除values func Without[T comparable](slice []T, values ...T) []T //slice去除values
func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) //根据函数groupFn的逻辑分slice为两组slice func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) //根据函数groupFn的逻辑分slice为两组slice
func Count[T any](slice []T, predicate func(index int, t T) bool) int //遍历slice的元素返回所有匹配元素的数量 func Count[T any](slice []T, predicate func(index int, t T) bool) int //遍历slice的元素返回所有匹配元素的数量

View File

@@ -433,11 +433,11 @@ func IntSlice(slice interface{}) []int {
return out return out
} }
// DeleteByIndex delete the element of slice from start index to end index - 1. // DeleteAt delete the element of slice from start index to end index - 1.
func DeleteByIndex[T any](slice []T, start int, end ...int) []T { func DeleteAt[T any](slice []T, start int, end ...int) []T {
size := len(slice) size := len(slice)
if start < 0 || start > size-1 { if start < 0 || start >= size {
return slice return slice
} }
@@ -482,8 +482,8 @@ func Drop[T any](slice []T, n int) []T {
return slice[n:size] return slice[n:size]
} }
// InsertByIndex insert the value or other slice into slice at index. // InsertAt insert the value or other slice into slice at index.
func InsertByIndex[T any](slice []T, index int, value interface{}) []T { func InsertAt[T any](slice []T, index int, value interface{}) []T {
size := len(slice) size := len(slice)
if index < 0 || index > size { if index < 0 || index > size {
@@ -505,8 +505,8 @@ func InsertByIndex[T any](slice []T, index int, value interface{}) []T {
return slice return slice
} }
// UpdateByIndex update the slice element at index. // UpdateAt update the slice element at index.
func UpdateByIndex[T any](slice []T, index int, value T) []T { func UpdateAt[T any](slice []T, index int, value T) []T {
size := len(slice) size := len(slice)
if index < 0 || index >= size { if index < 0 || index >= size {

View File

@@ -291,21 +291,21 @@ func TestInterfaceSlice(t *testing.T) {
assert.Equal(expect, InterfaceSlice(strs)) assert.Equal(expect, InterfaceSlice(strs))
} }
func TestDeleteByIndex(t *testing.T) { func TestDeleteAt(t *testing.T) {
assert := internal.NewAssert(t, "TestDeleteByIndex") assert := internal.NewAssert(t, "TestDeleteAt")
assert.Equal([]string{"a", "b", "c"}, DeleteByIndex([]string{"a", "b", "c"}, -1)) assert.Equal([]string{"a", "b", "c"}, DeleteAt([]string{"a", "b", "c"}, -1))
assert.Equal([]string{"a", "b", "c"}, DeleteByIndex([]string{"a", "b", "c"}, 3)) assert.Equal([]string{"a", "b", "c"}, DeleteAt([]string{"a", "b", "c"}, 3))
assert.Equal([]string{"b", "c"}, DeleteByIndex([]string{"a", "b", "c"}, 0)) assert.Equal([]string{"b", "c"}, DeleteAt([]string{"a", "b", "c"}, 0))
assert.Equal([]string{"a", "c"}, DeleteByIndex([]string{"a", "b", "c"}, 1)) assert.Equal([]string{"a", "c"}, DeleteAt([]string{"a", "b", "c"}, 1))
assert.Equal([]string{"a", "b"}, DeleteByIndex([]string{"a", "b", "c"}, 2)) assert.Equal([]string{"a", "b"}, DeleteAt([]string{"a", "b", "c"}, 2))
assert.Equal([]string{"b", "c"}, DeleteByIndex([]string{"a", "b", "c"}, 0, 1)) assert.Equal([]string{"b", "c"}, DeleteAt([]string{"a", "b", "c"}, 0, 1))
assert.Equal([]string{"c"}, DeleteByIndex([]string{"a", "b", "c"}, 0, 2)) assert.Equal([]string{"c"}, DeleteAt([]string{"a", "b", "c"}, 0, 2))
assert.Equal([]string{}, DeleteByIndex([]string{"a", "b", "c"}, 0, 3)) assert.Equal([]string{}, DeleteAt([]string{"a", "b", "c"}, 0, 3))
assert.Equal([]string{}, DeleteByIndex([]string{"a", "b", "c"}, 0, 4)) assert.Equal([]string{}, DeleteAt([]string{"a", "b", "c"}, 0, 4))
assert.Equal([]string{"a"}, DeleteByIndex([]string{"a", "b", "c"}, 1, 3)) assert.Equal([]string{"a"}, DeleteAt([]string{"a", "b", "c"}, 1, 3))
assert.Equal([]string{"a"}, DeleteByIndex([]string{"a", "b", "c"}, 1, 4)) assert.Equal([]string{"a"}, DeleteAt([]string{"a", "b", "c"}, 1, 4))
} }
func TestDrop(t *testing.T) { func TestDrop(t *testing.T) {
@@ -325,26 +325,28 @@ func TestDrop(t *testing.T) {
assert.Equal([]int{}, Drop([]int{1, 2, 3, 4, 5}, -6)) assert.Equal([]int{}, Drop([]int{1, 2, 3, 4, 5}, -6))
} }
func TestInsertByIndex(t *testing.T) { func TestInsertAt(t *testing.T) {
assert := internal.NewAssert(t, "TestInsertByIndex") assert := internal.NewAssert(t, "TestInsertAt")
strs := []string{"a", "b", "c"} strs := []string{"a", "b", "c"}
assert.Equal([]string{"a", "b", "c"}, InsertByIndex(strs, -1, "1")) assert.Equal([]string{"a", "b", "c"}, InsertAt(strs, -1, "1"))
assert.Equal([]string{"a", "b", "c"}, InsertByIndex(strs, 4, "1")) assert.Equal([]string{"a", "b", "c"}, InsertAt(strs, 4, "1"))
assert.Equal([]string{"1", "a", "b", "c"}, InsertByIndex(strs, 0, "1")) assert.Equal([]string{"1", "a", "b", "c"}, InsertAt(strs, 0, "1"))
assert.Equal([]string{"a", "b", "c", "1"}, InsertByIndex(strs, 3, "1")) assert.Equal([]string{"a", "1", "b", "c"}, InsertAt(strs, 1, "1"))
assert.Equal([]string{"1", "2", "3", "a", "b", "c"}, InsertByIndex(strs, 0, []string{"1", "2", "3"})) assert.Equal([]string{"a", "b", "1", "c"}, InsertAt(strs, 2, "1"))
assert.Equal([]string{"a", "b", "c", "1", "2", "3"}, InsertByIndex(strs, 3, []string{"1", "2", "3"})) assert.Equal([]string{"a", "b", "c", "1"}, InsertAt(strs, 3, "1"))
assert.Equal([]string{"1", "2", "3", "a", "b", "c"}, InsertAt(strs, 0, []string{"1", "2", "3"}))
assert.Equal([]string{"a", "b", "c", "1", "2", "3"}, InsertAt(strs, 3, []string{"1", "2", "3"}))
t.Log(strs) t.Log(strs)
} }
func TestUpdateByIndex(t *testing.T) { func TestUpdateAt(t *testing.T) {
assert := internal.NewAssert(t, "TestUpdateByIndex") assert := internal.NewAssert(t, "TestUpdateAt")
assert.Equal([]string{"a", "b", "c"}, UpdateByIndex([]string{"a", "b", "c"}, -1, "1")) assert.Equal([]string{"a", "b", "c"}, UpdateAt([]string{"a", "b", "c"}, -1, "1"))
assert.Equal([]string{"1", "b", "c"}, UpdateByIndex([]string{"a", "b", "c"}, 0, "1")) assert.Equal([]string{"1", "b", "c"}, UpdateAt([]string{"a", "b", "c"}, 0, "1"))
assert.Equal([]string{"a", "b", "2"}, UpdateByIndex([]string{"a", "b", "c"}, 2, "2")) assert.Equal([]string{"a", "b", "2"}, UpdateAt([]string{"a", "b", "c"}, 2, "2"))
assert.Equal([]string{"a", "b", "c"}, UpdateByIndex([]string{"a", "b", "c"}, 3, "2")) assert.Equal([]string{"a", "b", "c"}, UpdateAt([]string{"a", "b", "c"}, 3, "2"))
} }
func TestUnique(t *testing.T) { func TestUnique(t *testing.T) {