Pau
10/05/2020, 11:43 AMval cardStats = mutableMapOf<String, Int>()
Once the card is created in other map for example: "France" to "Paris", this is automatically created:
cardStats = mutableMapOf("France" to 0)
In the guessing function, if they miss it adds up fail attempts into the card:
cardStats[ansDef] += 1
But I get this:
Operator call corresponds to a dot-qualified call 'cardStats[ansDef].plusAssign(1)' which is not allowed on a nullable receiver 'cardStats[ansDef]'.
Mikael Alfredsson
10/05/2020, 11:50 AMVampire
10/05/2020, 12:02 PMcardStats[ansDef] = (cardStats[ansDef] ?: 0) + 1
cardStats.compute(ansDef) { _, stat -> (stat ?: 0) + 1 }
cardStats[ansDef] = cardStats.getOrDefault(ansDef, 0) + 1
cardStats[ansDef] = cardStats.getOrPut(ansDef) { 0 } + 1
Pau
10/05/2020, 12:10 PMephemient
10/05/2020, 5:10 PM.compute()
and .getOrDefault()
are Java 8+ only, while .getOrElse()
is in the Kotlin stdlibVampire
10/05/2020, 5:11 PMephemient
10/05/2020, 5:26 PMVampire
10/05/2020, 5:27 PM