-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d32c94
commit 63ca023
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} | ||
} | ||
} |