Interestingly, while I still had the time for it, I thought about extending the lazy evaluation Sjoerd and I had created (the Beyond lib) with this feature. Interesting because the implementation I had thought about was very similar to the Scheme impl, without my being aware of it.
My basic idea was to wrap the expression inside a function. The first time the value is requested it's calculated and the result is tacked as a property onto the function object. Something like:
function sum() { return 1 + 2; }
function force(f) {
return "result" in f ? f.result : f.result = f();
}
var a = force(sum);
var b = force(sum);
|