diff --git a/README.md b/README.md index 073353b..5bb503d 100644 --- a/README.md +++ b/README.md @@ -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 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 GroupBy(slice, function interface{}) (interface{}, interface{}) // groups slice into two categories ``` #### 10. strutil is for processing string diff --git a/README_zh-CN.md b/README_zh-CN.md index f278edb..6b07c88 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -413,6 +413,7 @@ func Unique(slice interface{}) interface{} //去重切片 func Union(slices ...interface{}) interface{} //slice并集, 去重 func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //在切片中index位置更新value func Without(slice interface{}, values ...interface{}) interface{} //slice去除values +func GroupBy(slice, function interface{}) (interface{}, interface{}) ``` #### 10. strutil字符串处理包 diff --git a/slice/slice.go b/slice/slice.go index 269c0c8..8d17ab4 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -169,6 +169,32 @@ func Filter(slice, function interface{}) 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. // The function signature should be func(index int, value interface{}) bool . func Find(slice, function interface{}) interface{} { diff --git a/slice/slice_test.go b/slice/slice_test.go index ae2076f..420cd4f 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -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) { nums := []int{1, 2, 3, 4, 5} even := func(i, num int) bool {