commonMain.Memoize.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functional-jvm Show documentation
Show all versions of functional-jvm Show documentation
Toolbox of utilities/helpers for Kotlin development
package com.juul.tuulbox.functional
/** Returns a locally-memoized version of this function. Two separate invocations of [memoize] will have two separate caches. */
public fun (() -> O).memoize(): () -> O {
val value by lazy { this() }
return { value }
}
/** Returns a locally-memoized version of this function. Two separate invocations of [memoize] will have two separate caches. */
public fun ((I1) -> O).memoize(): (I1) -> O {
val cache = mutableMapOf()
return { i1 -> cache.getOrPut(i1) { this(i1) } }
}
/** Returns a locally-memoized version of this function. Two separate invocations of [memoize] will have two separate caches. */
public fun ((I1, I2) -> O).memoize(): (I1, I2) -> O {
val cache = mutableMapOf, O>()
return { i1, i2 -> cache.getOrPut(Pair(i1, i2)) { this(i1, i2) } }
}
/** Returns a locally-memoized version of this function. Two separate invocations of [memoize] will have two separate caches. */
public fun ((I1, I2, I3) -> O).memoize(): (I1, I2, I3) -> O {
val cache = mutableMapOf, O>()
return { i1, i2, i3 -> cache.getOrPut(Triple(i1, i2, i3)) { this(i1, i2, i3) } }
}
/** Returns a locally-memoized version of this function. Two separate invocations of [memoize] will have two separate caches. */
public fun ((I1, I2, I3, I4) -> O).memoize(): (I1, I2, I3, I4) -> O {
// TODO: This is slightly less efficient than a Tuple4 data class.
val cache = mutableMapOf, O>()
return { i1, i2, i3, i4 -> cache.getOrPut(listOf(i1, i2, i3, i4)) { this(i1, i2, i3, i4) } }
}
/** Returns a locally-memoized version of this function. Two separate invocations of [memoize] will have two separate caches. */
public fun ((I1, I2, I3, I4, I5) -> O).memoize(): (I1, I2, I3, I4, I5) -> O {
// TODO: This is slightly less efficient than a Tuple5 data class.
val cache = mutableMapOf, O>()
return { i1, i2, i3, i4, i5 -> cache.getOrPut(listOf(i1, i2, i3, i4, i5)) { this(i1, i2, i3, i4, i5) } }
}
/** Returns a locally-memoized version of this function. Two separate invocations of [memoize] will have two separate caches. */
public fun ((I1, I2, I3, I4, I5, I6) -> O).memoize(): (I1, I2, I3, I4, I5, I6) -> O {
// TODO: This is slightly less efficient than a Tuple6 data class.
val cache = mutableMapOf, O>()
return { i1, i2, i3, i4, i5, i6 -> cache.getOrPut(listOf(i1, i2, i3, i4, i5, i6)) { this(i1, i2, i3, i4, i5, i6) } }
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy