参考透明度这个术语是什么意思?我听说它被描述为"它意味着你可以用平等替换等于",但这似乎是一个不充分的解释.
theory computer-science functional-programming referential-transparency
In all the examples I can find on memoization / internal function cache in Functional Programming in JavaScript, the examples are either mutating or reassigning the cache.
Here's an example taken from https://scotch.io/tutorials/understanding-memoization-in-javascript#toc-a-functional-approach
function memoizer(fun){
let cache = {}
return function (n){
if (cache[n] != undefined ) {
return cache[n]
} else {
let result = fun(n)
cache[n] = result
return result
}
}
}
Run Code Online (Sandbox Code Playgroud)
The only tiny improvement I can come up with is to use reassigning instead of …