-
-
Save PaulWoitaschek/617435b8b3180c31cc3fbf87b5dd06e7 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 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 | |
} |
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
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