Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
fun <T, M> Iterable<T>.countBy(transformer: (T) -> M) : Map<M, Int> {
val emptyMap = mapOf<M, Int>()
return this.fold(emptyMap) {accumulator, item ->
val transformedItem = transformer(item)
accumulator + Pair(transformedItem, accumulator.getOrElse(transformedItem, {0}) + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.