There are many cases where I am conflicted over using With[]
versus using pure functions when dealing with constants, knowing the output is the same either way. For example, consider
f1[input_] := With[{const = g[input]},h[const]]
f2[input_] := h[#] &@g[input]
where g
and h
are complicated functions defined inline – not defined elsewhere using set delayed.
Of course, the output is the same...
f1[3]
(*h[g[3]]*)
f2[3]
(*h[g[3]]*)
Is there a general rule about which paradigm is faster or uses less memory?
Are there any general major considerations I should be making in determining when to use one over the other?
f[g[input]]
? Or the same more cryptical:(f@*g)[x]
$\endgroup$g
andh
be inline. This usually makes for better performance. Also, doing as you suggest is messy whenh
refers to its input many times. (I am a bit confused though. It seems you are usingf
the way I am usingh
.) $\endgroup$h
is inline. So what I mean is thath[g[input]]
is literally, something likeg[input]^g[input]+Range[1,g[input]
, not defined elsewhere with set delayed. In this example,g[input]
is evaluated 3 times. $\endgroup$With
I can use a variable name to help me remember what it stands for as well as break up a really long composition of operations into pieces. Generally speaking, readibility is worth valuing over insignificant speed advantages. $\endgroup$