Infix functions in Kotlin
Level 4196 keystrokes
An infix function with exactly one parameter can be called without a dot or parentheses: a f b instead of a.f(b). Here both are extension functions, on Int and on String.
Kotlin code
// Infix extension on Int: this is the Int, so the string is repeated this times1infix fun Int.times(str: String) = str.repeat(this)// Infix extension on String that pairs the receiver with the argument2infix fun String.onto(other: String) = Pair(this, other)3 // Same as 3.times("Hi"): "HiHiHi"4val result = 3 times "Hi"// Same as "Alice".onto("Bob"): Pair("Alice", "Bob")5val pair = "Alice" onto "Bob"6 // Print HiHiHi7println(result)// Pair's toString() prints (Alice, Bob)8println(pair)Output
HiHiHi (Alice, Bob)
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Kotlin code and how often they appear.
"× 6(× 6)× 6=× 4.× 3:× 2,× 1