mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 02:02:27 +08:00
Slice: Add GroupBy func (#10)
Group by: split slice into two groups, applies on each slice element a predicate func to categorize this element. Changes * Add groub by func * Add test case for this func
This commit is contained in:
@@ -412,6 +412,7 @@ func Unique(slice interface{}) interface{} //remove duplicate elements in slice
|
|||||||
func Union(slices ...interface{}) interface{} //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
func Union(slices ...interface{}) interface{} //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
||||||
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //update the slice element at index.
|
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //update the slice element at index.
|
||||||
func Without(slice interface{}, values ...interface{}) interface{} //creates a slice excluding all given values
|
func Without(slice interface{}, values ...interface{}) interface{} //creates a slice excluding all given values
|
||||||
|
func GroupBy(slice, function interface{}) (interface{}, interface{}) // groups slice into two categories
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 10. strutil is for processing string
|
#### 10. strutil is for processing string
|
||||||
|
|||||||
@@ -413,6 +413,7 @@ func Unique(slice interface{}) interface{} //去重切片
|
|||||||
func Union(slices ...interface{}) interface{} //slice并集, 去重
|
func Union(slices ...interface{}) interface{} //slice并集, 去重
|
||||||
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //在切片中index位置更新value
|
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //在切片中index位置更新value
|
||||||
func Without(slice interface{}, values ...interface{}) interface{} //slice去除values
|
func Without(slice interface{}, values ...interface{}) interface{} //slice去除values
|
||||||
|
func GroupBy(slice, function interface{}) (interface{}, interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 10. strutil字符串处理包
|
#### 10. strutil字符串处理包
|
||||||
|
|||||||
@@ -169,6 +169,32 @@ func Filter(slice, function interface{}) interface{} {
|
|||||||
return res.Interface()
|
return res.Interface()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GroupBy iterate over elements of the slice, each element will be group by criteria, returns two slices
|
||||||
|
// The function signature should be func(index int, value interface{}) bool .
|
||||||
|
func GroupBy(slice, function interface{}) (interface{}, interface{}) {
|
||||||
|
sv := sliceValue(slice)
|
||||||
|
fn := functionValue(function)
|
||||||
|
|
||||||
|
elemType := sv.Type().Elem()
|
||||||
|
if checkSliceCallbackFuncSignature(fn, elemType, reflect.ValueOf(true).Type()) {
|
||||||
|
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||||
|
}
|
||||||
|
|
||||||
|
groupB := reflect.MakeSlice(sv.Type(), 0, 0)
|
||||||
|
groupA := reflect.MakeSlice(sv.Type(), 0, 0)
|
||||||
|
|
||||||
|
for i := 0; i < sv.Len(); i++ {
|
||||||
|
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
|
||||||
|
if flag.Bool() {
|
||||||
|
groupA = reflect.Append(groupA, sv.Index(i))
|
||||||
|
} else {
|
||||||
|
groupB = reflect.Append(groupB, sv.Index(i))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return groupA.Interface(), groupB.Interface()
|
||||||
|
}
|
||||||
|
|
||||||
// Find iterates over elements of slice, returning the first one that passes a truth test on function.
|
// Find iterates over elements of slice, returning the first one that passes a truth test on function.
|
||||||
// The function signature should be func(index int, value interface{}) bool .
|
// The function signature should be func(index int, value interface{}) bool .
|
||||||
func Find(slice, function interface{}) interface{} {
|
func Find(slice, function interface{}) interface{} {
|
||||||
|
|||||||
@@ -161,6 +161,30 @@ func TestFilter(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGroupBy(t *testing.T) {
|
||||||
|
nums := []int{1, 2, 3, 4, 5, 6}
|
||||||
|
evenFunc := func(i, num int) bool {
|
||||||
|
return (num % 2) == 0
|
||||||
|
}
|
||||||
|
expectedEven := []int{2, 4, 6}
|
||||||
|
even, odd := GroupBy(nums, evenFunc)
|
||||||
|
|
||||||
|
t.Log("odd", odd)
|
||||||
|
|
||||||
|
t.Log("even", even)
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(IntSlice(even), expectedEven) {
|
||||||
|
internal.LogFailedTestInfo(t, "GroupBy even", nums, expectedEven, even)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedOdd := []int{1, 3, 5}
|
||||||
|
if !reflect.DeepEqual(IntSlice(odd), expectedOdd) {
|
||||||
|
internal.LogFailedTestInfo(t, "GroupBy odd", nums, expectedOdd, odd)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFind(t *testing.T) {
|
func TestFind(t *testing.T) {
|
||||||
nums := []int{1, 2, 3, 4, 5}
|
nums := []int{1, 2, 3, 4, 5}
|
||||||
even := func(i, num int) bool {
|
even := func(i, num int) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user