Shorthand argument names ($0, $1) in Swift - Time & Space Complexity
We want to see how using shorthand argument names affects the speed of code.
Does using $0, $1 change how long the code takes to run as input grows?
Analyze the time complexity of the following code snippet.
let numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map { $0 * 2 }
This code doubles each number in the list using shorthand argument $0 inside a map.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The map function loops over each element in the array.
- How many times: Once for every item in the array.
As the list gets bigger, the code runs more times, once per item.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times the doubling operation |
| 100 | 100 times the doubling operation |
| 1000 | 1000 times the doubling operation |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the list gets bigger.
[X] Wrong: "Using $0 makes the code run faster or slower than naming the argument."
[OK] Correct: The shorthand $0 is just a shortcut for naming the argument. It does not change how many times the code runs or how long each step takes.
Understanding how shorthand arguments work helps you write clean code without worrying about slowing it down. This skill shows you know both style and performance basics.
"What if we replaced map with a nested loop using $0 and $1? How would the time complexity change?"
Practice
$0 represent in a Swift closure?Solution
Step 1: Understand shorthand argument names
In Swift closures,$0refers to the first argument passed to the closure.Step 2: Identify
Since$0meaning$0is always the first argument, it represents the first input value the closure receives.Final Answer:
The first argument passed to the closure -> Option BQuick Check:
$0= first argument [OK]
- Confusing $0 with the return value
- Thinking $0 is the closure itself
- Mixing $0 with the last argument
Solution
Step 1: Understand sorting with closures
To sort ascending, the closure should return true if the first element is less than the second.Step 2: Match shorthand arguments to sorting logic
Using$0 < $1means the first element is less than the second, which sorts ascending.Final Answer:
let sorted = numbers.sorted { $0 < $1 } -> Option DQuick Check:
Ascending sort uses $0 < $1 [OK]
- Reversing $0 and $1 in comparison
- Using > instead of < for ascending
- Confusing sorting order logic
let names = ["Anna", "Bob", "Charlie"]
let reversed = names.map { String($0.reversed()) }
print(reversed)Solution
Step 1: Understand the map and reversed usage
Themapapplies the closure to each name.$0is each string.$0.reversed()reverses characters.Step 2: Convert reversed characters back to String
WrappingString($0.reversed())creates a reversed string for each name.Final Answer:
["annA", "boB", "eilrahC"] -> Option CQuick Check:
map + reversed string = reversed names [OK]
- Forgetting to convert reversed characters back to String
- Expecting original array without changes
- Confusing reversed string with spaced characters
let numbers = [3, 1, 2]
let sorted = numbers.sorted { $2 < $1 }
print(sorted)Solution
Step 1: Check shorthand argument usage
Shorthand arguments start at $0 for the first, $1 for the second. There is no $2 in a two-argument closure.Step 2: Identify invalid argument index
Using$2causes a compile error because only $0 and $1 exist here.Final Answer:
Using $2 which does not exist in this closure -> Option AQuick Check:
Only $0 and $1 allowed in two-argument closures [OK]
- Assuming $2 exists in two-argument closures
- Forgetting shorthand argument limits
- Confusing sorted and sort method differences
let pairs = [(3, 5), (1, 2), (4, 4)], which closure using shorthand argument names correctly sorts the array by the sum of each tuple's elements in descending order?Solution
Step 1: Understand tuple access and sorting logic
Each element is a tuple. To sort by sum descending, compare sums of $0 and $1 tuples.Step 2: Check each option's correctness
pairs.sorted { $0.0 + $0.1 > $1.0 + $1.1 } sums $0.0 + $0.1 and compares to $1.0 + $1.1 with > for descending order, which is correct.Final Answer:
pairs.sorted { $0.0 + $0.1 > $1.0 + $1.1 } -> Option AQuick Check:
Sum tuples and use > for descending sort [OK]
- Mixing $0 and $1 order in comparison
- Trying to add tuples directly without accessing elements
- Using incorrect tuple element indices
Ready to prove it?
Pass the test and this pattern moves unknown
