All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlinx.jupyter.util.Memoization.kt Maven / Gradle / Ivy

Go to download

Implementation of REPL compiler and preprocessor for Jupyter dialect of Kotlin (IDE-compatible)

There is a newer version: 0.12.0-333
Show newest version
package org.jetbrains.kotlinx.jupyter.util

import java.util.Optional
import java.util.concurrent.ConcurrentHashMap

fun  createCachedFun(calculate: (A) -> V): (A) -> V {
    return createCachedFun({ it }, calculate)
}

fun  createCachedFun(
    calculateKey: (A) -> K,
    calculate: (A) -> V,
): (A) -> V {
    val cache = ConcurrentHashMap, Optional>()

    @Suppress("UNCHECKED_CAST")
    return { argument ->
        val key = Optional.ofNullable(calculateKey(argument)) as Optional
        cache.getOrPut(key) {
            Optional.ofNullable(calculate(argument)) as Optional
        }.orElse(null)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy