Kyu
Kyu

Reputation: 37

How dose it work? (about map function in Kotlin)

I can't understand below logic. I think when put 'a' into () in result, the value of result is 32. Because of (0 until 1-1 -> 0), lastDays[it] is 31. So result is 31 + 1 = 32. But the value is 1. I am studying Kotlin now.

val a = 1
var b = 1

val lastDays = listOf(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

val result = (0 until a - 1).map {
    lastDays[it]
}.sum() + b

Please borrow me your wisdom.

Upvotes: 1

Views: 172

Answers (1)

Tenfour04
Tenfour04

Reputation: 93759

0 until a - 1 results in 0 until 0, so it is an empty range. Mapping the empty range results in an empty list. Calling sum() on an empty list returns 0. Then you add 0 + b where b is 1.

Upvotes: 4

Related Questions