Skip to content

Instantly share code, notes, and snippets.

@PaulWoitaschek
Last active January 25, 2020 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulWoitaschek/617435b8b3180c31cc3fbf87b5dd06e7 to your computer and use it in GitHub Desktop.
Save PaulWoitaschek/617435b8b3180c31cc3fbf87b5dd06e7 to your computer and use it in GitHub Desktop.
fun main() {
val words = listOf("a", "abc", "ab", "def", "abcd", "")
val grouped = words
.groupBy {
when {
it.isEmpty() -> WordType.Empty
it.length <= 2 -> WordType.Shorter
else -> WordType.Longer
}
}
.mapValues { (_, words) -> words.size }
// prints {Shorter=2, Longer=3, Empty=1}
print(grouped)
}
enum class WordType {
Longer, Shorter, Empty
}
data class MyResponse(
val longer: Int,
val shorter: Int
)
fun main() {
val words = listOf("a", "abc", "ab", "def", "abcd")
val (longer, shorter) = words.partition { it.length > 2 }
println(MyResponse(longer.size, shorter.size))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment