// String extension properties
val String.wordCount: Int
get() = trim().split("\\s+".toRegex()).filter { it.isNotEmpty() }.size
val <T> List<T>.lastIndex: Int
get() = size - 1
// Pair where both halves are the same: doubled
val <T> T.asPair: Pair<T, T>
get() = this to this
fun main() {
println("hello world foo bar".wordCount) // 4
val xs = listOf("a", "b", "c", "d")
println(xs.lastIndex) // 3 (built-in too, but instructive)
println(42.asPair) // (42, 42)
println("hi".asPair) // (hi, hi)
}
Create a free account and build your private vault. Share publicly whenever you want.