Concept Flow - It keyword for single parameter
Start Lambda
Single Parameter?
Yes
Use 'it' as parameter
Execute Lambda Body
Return Result
End
When a lambda has one parameter, Kotlin lets you use 'it' to refer to it without naming it explicitly.
val numbers = listOf(1, 2, 3) val doubled = numbers.map { it * 2 } println(doubled)
| Step | Lambda Input (it) | Operation | Result | Output List State |
|---|---|---|---|---|
| 1 | 1 | 1 * 2 | 2 | [2] |
| 2 | 2 | 2 * 2 | 4 | [2, 4] |
| 3 | 3 | 3 * 2 | 6 | [2, 4, 6] |
| 4 | - | End of list | - | [2, 4, 6] |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| it | - | 1 | 2 | 3 | - |
| doubled | [] | [2] | [2, 4] | [2, 4, 6] | [2, 4, 6] |
Kotlin lambdas with one parameter can use 'it' implicitly.
No need to name the parameter explicitly.
Use 'it' inside the lambda body to refer to the parameter.
Example: list.map { it * 2 } doubles each element.
If more than one parameter, 'it' cannot be used.