I was reading an article at https://medium.com/@aarshkshah1992/scala-functional-memoization-and-lazy-loading-caches-de116f24828
and this part is really interesting
def memoizedIsPrime: Int => Boolean = {
def checkIfPrime(i: Int): Boolean = {
2 to (i - 1) forall (x => i % x != 0)
}
var cache = Map.empty[Int, Boolean]
i => {
if (!cache.contains(i)) {
print(s"Calling isPrime since input ${i} has not been seen before and caching the output")
cache = cache updated(i, checkIfPrime(i))
}
else print(s"Input ${i} has been seen before , returning cached output")
cache(i)
}
}
val isPrime = memoizedIsPrime
Can anyone explain how i is being accessed and the how the code part after => is really working?
Thanks in advance :)