Step-by-Step Explanation
✅ Step 1: Function Definition
This function:
-
Takes one number x
-
Returns x + 1
✅ Example:
f(1) → 2
f(5) → 6
✅ Step 2: First map()
m = map(f, [1, 2, 3])This applies f() to each element:
| Original | After f(x) |
|---|---|
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
Important:
map() does NOT execute immediately
-
It creates a lazy iterator
So at this point:
m → (2, 3, 4) # not yet computed✅ Step 3: Second map()
m = map(f, m)Now we apply f() again on the result of the first map:
| First map | Second map |
|---|---|
| 2 | 3 |
| 3 | 4 |
| 4 | 5 |
So the final values become:
(3, 4, 5)✅ Step 4: Convert to List
print(list(m))This executes the lazy iterator and prints:
[3, 4, 5]✅ Final Output
[3, 4, 5]


0 Comments:
Post a Comment