fun processUsers(users: List<String>): Map<String, Int> {
val result = mutableMapOf<String, Int>()
// Local function — captures `result` from outer scope
fun record(name: String, score: Int) {
result[name] = (result[name] ?: 0) + score
}
users.forEach { user ->
when {
user.startsWith("admin_") -> record(user, 100)
user.length > 8 -> record(user, 50)
else -> record(user, 10)
}
}
return result
}
fun main() {
val scores = processUsers(listOf("admin_root", "alice", "verylongname", "bob"))
println(scores)
}
Create a free account and build your private vault. Share publicly whenever you want.