Files
learngo/27-functional-programming/log-parser-exp/groupby.go
2019-08-06 02:44:17 +03:00

20 lines
350 B
Go

package main
type groupFunc func(result) (key string)
func groupBy(results []result, keyer groupFunc) []result {
grouped := make(map[string]result, len(results))
for _, cur := range results {
key := keyer(cur)
grouped[key] = cur.add(grouped[key])
}
out := results[:0]
for _, r := range grouped {
out = append(out, r)
}
return out
}