Is there an easy way to combine pure functions into a single pure function? For example, say I have
f = #1^2 &;
g = #1 - 2 &;
and I want to define a new pure function h
that is the difference of the two functions (as currently defined, so that h
will not change if f
or g
are redefined). h = f - g
doesn't work because it doesn't combine them into a single function. The best I've been able to come up with is
h = Evaluate[f@# - g@#] &
but this seems a bit hack-ish. Is there a more natural way to combine them?
Edit: I was looking for something where Mathematica performs its automatic simplifications, just as if we'd had f = x^2; g = x - 2; h = f - g
, but for pure functions.
#1^2 + #1 - 2 &
? And see mathematica.stackexchange.com/questions/33112/… $\endgroup$h = f@# + g@# &
? $\endgroup$h
to not be affected by later changes tof
andg
is an important point you should put in the question. $\endgroup$