Hi, is there an equivalent of `console.count("some...
# getting-started
a
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
Not really, but you can emulate it: https://pl.kotl.in/LbZBGUszp
e
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")
}