Mark
04/01/2022, 9:19 AMval getValuesForKey: (String) -> Set<String> =
object : (String) -> Set<String> {
val map = mutableMapOf<String, Set<String>>()
override fun invoke(key: String): Set<String> =
synchronized(this) {
map.getOrPut(key) {
calculateValuesForKey(key)
}
}
}
ephemient
04/01/2022, 2:47 PMclass Memo<K, V>(
val block: (K) -> V,
) : (K) -> V {
private val cache: ConcurrentMap<K, Lazy<V>> = ConcurrentHashMap()
override fun invoke(key: K): V =
cache.computeIfAbsent(key) { lazy { block(it) } }.value
}
Mark
04/02/2022, 1:49 AMephemient
04/02/2022, 2:31 AMMark
04/02/2022, 3:50 AMsynchronized
be added to the standard lib, when it’s JVM-only?ephemient
04/02/2022, 5:11 AM.toSortedSet()
and .toSortedMap()
are also JVM-only, console
and .unsafeCast()
are JS-only, Char.toChars()
and getTimeMicros()
are native-only.use()
on Java 7+ and .asStream()
on Java 8+synchronized
, there's no other way to get the same access to that functionality of the JVM without compiler support, but it doesn't make any sense in JS (which is single-threaded) and the mechanisms on native are incompatibly different, so it's JVM-only