Last active
May 22, 2016 21:52
-
-
Save kubukoz/c81a713e767b24b6674c14f20741798a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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