Infix functions for readable calls in Kotlin - Time & Space Complexity
Let's see how the time cost changes when using infix functions in Kotlin.
We want to know how the program's work grows as input gets bigger.
Analyze the time complexity of the following code snippet.
infix fun Int.times(str: String): String {
return str.repeat(this)
}
fun main() {
val result = 3 times "Hi"
println(result)
}
This code defines an infix function to repeat a string a number of times, then calls it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The string repeat function internally repeats the string by concatenation.
- How many times: It repeats the string exactly as many times as the integer value (here, 3 times).
As the number of repeats grows, the work to build the final string grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 string concatenations |
| 100 | About 100 string concatenations |
| 1000 | About 1000 string concatenations |
Pattern observation: The work grows roughly in direct proportion to the number of repeats.
Time Complexity: O(n)
This means the time to run grows linearly with how many times the string is repeated.
[X] Wrong: "Using an infix function makes the code faster or slower by itself."
[OK] Correct: The infix keyword only changes how we write the call, not how many steps the program takes.
Understanding how function calls affect time helps you explain your code clearly and shows you think about efficiency.
"What if the infix function repeated the string twice as many times internally? How would the time complexity change?"
Practice
infix functions in Kotlin?Solution
Step 1: Understand the role of infix functions
Infix functions allow calling a function without using dots and parentheses, making code more readable.Step 2: Compare options with this purpose
Only To call functions without dots and parentheses for more readable code correctly describes this purpose; others mention unrelated features.Final Answer:
To call functions without dots and parentheses for more readable code -> Option AQuick Check:
Infix functions improve readability = D [OK]
- Thinking infix allows multiple parameters
- Believing infix changes function speed
- Confusing infix with visibility modifiers
Solution
Step 1: Recall infix function syntax
An infix function must be declared with the keywordinfixbeforefun, have exactly one parameter, and a body.Step 2: Check each option
infix fun add(x: Int): Int = this + x correctly usesinfix fun add(x: Int): Int = this + x. fun infix add(x: Int, y: Int): Int = x + y has two parameters, invalid. infix fun add(x: Int): Int misses function body. fun add infix(x: Int): Int = this + x has wrong keyword order.Final Answer:
infix fun add(x: Int): Int = this + x -> Option DQuick Check:
Infix keyword before fun, one parameter only = A [OK]
- Declaring multiple parameters in infix function
- Placing infix keyword after fun
- Omitting function body
class Calculator {
infix fun add(x: Int): Int = x + 10
}
fun main() {
val calc = Calculator()
println(calc add 5)
}Solution
Step 1: Understand the infix function call
The infix functionaddadds 10 to the parameterx. Callingcalc add 5meanscalc.add(5).Step 2: Calculate the result
5 + 10 = 15, so the printed output is 15.Final Answer:
15 -> Option CQuick Check:
5 + 10 = 15 [OK]
- Confusing infix call with normal method call syntax
- Expecting parameter to be ignored
- Thinking infix changes return value
class Person {
infix fun greet(name: String, age: Int) {
println("Hello $name, age $age")
}
}
fun main() {
val p = Person()
p greet ("Alice", 30)
}Solution
Step 1: Check infix function parameter rules
Infix functions must have exactly one parameter. Here,greethas two parameters, which is invalid.Step 2: Verify other options
Parentheses are optional in infix calls. Infix functions can return Unit (no value). Classes can have infix functions. So only Infix functions cannot have more than one parameter is correct.Final Answer:
Infix functions cannot have more than one parameter -> Option BQuick Check:
One parameter max for infix = A [OK]
- Trying to use multiple parameters in infix function
- Thinking parentheses are always required
- Believing infix functions must return a value
class Box(val value: Int) {
infix fun combine(other: Box): Box = Box(this.value + other.value)
}
fun main() {
val box1 = Box(3)
val box2 = Box(7)
val result = box1 combine box2 combine Box(5)
println(result.value)
}Solution
Step 1: Understand chaining of infix calls
The expressionbox1 combine box2 combine Box(5)is evaluated left to right as(box1 combine box2) combine Box(5).Step 2: Calculate each combine call
First combine: 3 + 7 = 10, returns Box(10). Second combine: 10 + 5 = 15, returns Box(15). Printingresult.valueoutputs 15.Final Answer:
15 -> Option AQuick Check:
3+7=10, 10+5=15 [OK]
- Assuming combine adds values incorrectly
- Thinking infix calls don't chain
- Confusing order of operations
Ready to prove it?
Pass the test and this pattern moves unknown
