https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

Ayfri

07/20/2021, 8:36 AM
Hi, is there an equivalent of
console.count("something")
in Kotlin JVM ? Documentation of console.count : https://developer.mozilla.org/en-US/docs/Web/API/Console/count
h

hho

07/20/2021, 8:54 AM
Not really, but you can emulate it: https://pl.kotl.in/LbZBGUszp
e

ephemient

07/20/2021, 8:56 AM
fewer accesses like this… and could use ConcurrentMap/AtomicInt if concurrency is a potential consideration
Copy code
val counts = mutableMapOf<String, Int>()

fun count(label: String = "default") {
    val count = counts.getOrElse(label) { 0 } + 1
    counts[label] = count
    println("$label: $count")
}

fun countReset(label: String = "default") {
    counts.remove(label)
    println("$label: 0")
}
3 Views