Files
lo/docs/data/it-countvaluesby.md
T
2025-10-06 17:16:33 +02:00

792 B

name, slug, sourceRef, category, subCategory, signatures, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures variantHelpers similarHelpers position
CountValuesBy countvaluesby it/seq.go#L720 it slice
func CountValuesBy[T any, U comparable](collection iter.Seq[T], transform func(item T) U) map[U]int
core#slice#countvaluesby
204

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

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}