Back to Lo

It Countvaluesby

docs/data/it-countvaluesby.md

1.53.0489 B
Original Source

CountValuesBy counts the number of each element returned from transform function. Is equivalent to chaining Map and CountValues.

go
type Person struct {
    Name string
    Age  int
}

collection := func(yield func(Person) bool) {
    yield(Person{"Alice", 25})
    yield(Person{"Bob", 30})
    yield(Person{"Charlie", 25})
    yield(Person{"Diana", 30})
}

countsByAge := it.CountValuesBy(collection, func(p Person) int {
    return p.Age
})
// countsByAge contains {25: 2, 30: 2}