tofu.internal.CachedMatcher.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tofu-core_2.13 Show documentation
Show all versions of tofu-core_2.13 Show documentation
Opinionated Set of tool for functional programming in scala
package tofu.internal
private [tofu] final case class CachedMatcher[-A, +B](f: A => Option[B]) extends PartialFunction[A, B] {
private[this] var cachedInput: Option[A] = None
private[this] var cachedOutput: Option[B] = None
@inline private[this] def calc(input: A): Option[B] = {
if (!cachedInput.contains(input)) {
cachedInput = Some(input)
cachedOutput = f(input)
}
cachedOutput
}
def isDefinedAt(x: A): Boolean = calc(x).isDefined
def apply(x: A): B = calc(x).get
}