Skip to content

Commit

Permalink
Update group function
Browse files Browse the repository at this point in the history
  • Loading branch information
ledongthuc committed Jan 2, 2022
1 parent 2d32c94 commit 63ca023
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ valid := Some(list, func(item int) bool { item % 2 == 0 })
valid := Some(list, func(item string) bool { len(item) < 20 })
```

## Group

![goterators-Group](https://user-images.githubusercontent.com/1828895/147878206-bef39880-96db-4269-b54e-2dcbb06f6bac.png)

- Group function group elements into nested list by group condition.

```
groups := Group(list, func(item Product) groupKey {
return item.ComposeGroupKey()
}) // Group contains [ [ Product1, Product2, Product3 ], [ Product4, Product5 ] ]
```

## Flat

![goterators-Flat](https://user-images.githubusercontent.com/1828895/147876403-25e84044-d761-45b7-b126-6ad8a7c5a4d1.png)
Expand Down
2 changes: 1 addition & 1 deletion flat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestFlatNested(t *testing.T) {

for index := range expectedItems[groupIndex] {
if expectedItems[groupIndex][index] != actualItems[groupIndex][index] {
t.Errorf("Index %v, expected = %v, actual = %v", index, expectedItems[groupIndex][index], actualItems[groupIndex][index])
t.Errorf("Group index %v, Index %v, expected = %v, actual = %v", groupIndex, index, expectedItems[groupIndex][index], actualItems[groupIndex][index])
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package goterators

func Group[T any, G comparable](source []T, buildGroup func(item T) G) [][]T {
output := map[G][]T{}
for index, item := range source {
group := buildGroup(item)
output[group] = append(output[group], source[index])
}
return output
}
60 changes: 60 additions & 0 deletions group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package goterators

import "testing"

func TestGroup(t *testing.T) {
type Product struct {
productType string
productName string
}
testSource := []Product{{
productType: "food",
productName: "Coca-laco",
}, {
productType: "sport",
productName: "Old Balance",
}, {
productType: "sport",
productName: "Noke",
}, {
productType: "food",
productName: "Chip chip",
}}
expectedItems := [][]Product{
{
{
productType: "food",
productName: "Coca-laco",
}, {
productType: "food",
productName: "Chip chip",
},
},
{
{
productType: "sport",
productName: "Old Balance",
}, {
productType: "sport",
productName: "Noke",
},
},
}

actualItems := Group(testSource, func(item Product) string {
return item.productType
})
if len(expectedItems) != len(actualItems) {
t.Fatalf("Expected length = %v, actual length = %v", len(expectedItems), len(actualItems))
}
for groupIndex := range expectedItems {
if len(expectedItems[groupIndex]) != len(actualItems[groupIndex]) {
t.Fatalf("Group index %v, expected = %v, actual = %v", groupIndex, expectedItems[key], actualItems[key])
}
for index := range expectedItems[groupIndex] {
if expectedItems[groupIndex][index] != actualItems[groupIndex][index] {
t.Errorf("Group index %v, Index %v, expected = %v, actual = %v", groupIndex, index, expectedItems[key][index], actualItems[key][index])
}
}
}
}

0 comments on commit 63ca023

Please sign in to comment.