I enjoy using "%" to capture the previous output as it allows me to split big tasks into chunks without having to introduce lots of intermediate variable names. Consider an overly simple example:
x = 2;
x + 4;
%^2;
y = % - 3;
{x, y}
(*{2, 33}*)
How can I accomplish a similar thing inside Module. I naively tried
f[x_] := Module[{y},
x + 4;
%^2;
y = % - 3;
{x, y}
]
f[2]
(*{2, -3 + Null}*)
but that didn't work.
%
can work inside aModule
, but if you don't want unnecessary variables, can't you do something likey = x + 4; y = y^2; y = y - 3
? $\endgroup$f[x_] := Module[{y}, x + 4 // #^2 & // (y = # - 3) &; {x, y}]
? $\endgroup$//
might look better (but still strange). It's also more typing and one should not forget the extra()
in things like(y = # - 3) &
. $\endgroup$