1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-10 15:52: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:
donutloop
2022-01-02 14:24:56 +01:00
committed by GitHub
parent 94b1a1d383
commit b4a49fccfd
4 changed files with 52 additions and 0 deletions

View File

@@ -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 {