In another thread The best way to construct a function with memory, it was described how to back in file a function:
$runningLogFile = "/some/directory/runningLog.txt";
flog[x_, y_] := flog[x, y] = f[x, y] /.
v_ :> (PutAppend[Unevaluated[flog[x, y] = v;], $runningLogFile]; v)
I feel like I understand most of the ingredients here without understanding exactly how this works. Any chance someone could walk me through exactly how this is evaluated?
Let’s walk through the evaluation of
flog[1, 2], step-by-step…flog[1, 2]
When this expression is evaluated, Mathematica will substitute
1forxand2foryin the definition offloggiven in the question. This yields the next step in our tour:flog[1, 2] =
f[1, 2] /. v_ :> (PutAppend[Unevaluated[flog[1, 2] = v;], $runningLogFile];
v)
Note carefully that the assignment here,
flog[1, 2] = ..., is part of the definition offlogitself./.is an infix operator that is an alternate representation of the ReplaceAll function.ReplaceAllwill apply a replacement rule to the value of the first argument. Hold that thought — we’ll come back to it. The first argument isflog[1, 2] = f[1, 2]. This expression will evaluatef[1, 2]and then assign the result toflog[1, 2]. For the sake of discussion, let’s assume thatf[1, 2]returns345. Thus, a new definition will be added toflog, namelyflog[1, 2] = 345. After assignment, we can check the definition offlog:Observe that where
flogonly had a single definition initially but now it has two — the newly addedflog[1, 2]definition caching the result of that call. This is frequently called “memoization”.flog[1, 2] = 345may have the side-effect of establishing a new definition forflogbut, like every expression in Mathematica, it yields a value as well. The value is345which, after much ado, will be the first argument toReplaceAll.The second argument to
ReplaceAllis an invocation of the:>operator, an infix expression of the RuleDelayed function. In an effort to keep this post to a manageable size, we’ll simply note that the rule evaluates to itself in this context.So, now we have an expression that involves
/.to evaluate…345 /. v_ :> (PutAppend[Unevaluated[flog[1, 2] = v;], $runningLogFile]; v
A replacement expression matches its first argument (
345) with the pattern component of the replacement rule (v_).v_matches345(or anything else for that matter) and gives345the namevfor purposes of replacement.ReplaceAllthen substitutes345for every occurrence ofvin the right hand side of the rule. The result is the next expression to be evaluated…(PutAppend[Unevaluated[flog[1, 2] = 345;], $runningLogFile]; 345)
Here we have two expressions separated by a semicolon. Incidentally,
;is an infix operator that expands to CompoundExpression. The first expression involves PutAppend which writes the value of its first argument to the file named as the value of the second argument. Note, however, that the first argument is wrapped in Unevaluated. This suppresses the evaluation of the first argument so that it will be written exactly as-is to the file:flog[1, 2] = 345;. Should the current Mathematica session end, the written expression can be read into a future Mathematica session to re-establish the memoized result forflog[1, 2].CompoundExpressiondiscards the value of all arguments except the last. Here, the last argument is345. Since we have come to the end of our expression, this will be the final return value of the original call. That is,flog[1, 2]returns345— although as we saw there were side-effects that saved this result to memory and disk for future reference.Future calls to
flog[1, 2]Now if
flog[1, 2]is called again, Mathematica will find the new definitionflog[1, 2] = 345.345will be returned directly, without any of the complications that we discussed above. In particular, it won’t even callf[1, 2]again. This, of course, was the whole motivation for this example. The assumption was thatfwas very expensive to calculate, justifying all of these gymnastics to minimize the number of times that calculation occurs.