Skip to content

Instantly share code, notes, and snippets.

@kubukoz
Last active May 22, 2016
Embed
What would you like to do?
fun <T> List<T>.histogram(map: (T) -> Int, showAs: (Boolean, T) -> String): String {
val maxHeight = map { map(it) }.max() ?: 0
return (maxHeight downTo 0).map { i ->
this.map {
showAs(i <= map(it), it)
}.joinToString("")
}.joinToString("\n")
}
//Sample usage:
listOf(1, 2, 3, 4, 1, 2, 4).histogram({ it }, { it, e ->
if (it) "#" else " "
})
/* sample output
# #
## #
### ##
####### */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment