I sometimes want to write a function which needs to apply a bunch of transformations that are not functions of a single parameter only:
f[x_] := h[g3[g2[g1[x, 37], Some -> Stuff], "foo"], "bar"];
I find this hard to read code. And I would like to give names to the intermediate results.
If these were single parameter functions one could just write f = h @* g3 @* g2 @* g1
but that does not work in this case. One could try to curry away the additional parameters, but sometimes the some other intermediate result needs to be used again.
Ideally I would like to have the following which just sets the a
, b
and c
as values but allows me to have subsequent values depending on preceeding ones, so exacly what With
will not let me do.
f[x_] := specialScope[
{a = g1[x, 37],
b = g2[a, Some -> Stuff],
c = g3[c, "foo"]},
h[c, "bar"]];
My current solution is to use Module
, but I think that it creates more overhead than needed as the variables do not need to be mutable within the scope.
f[x_] := Module[
{a, b, c},
a = g1[x, 37];
b = g2[a, Some -> Stuff];
c = g3[c, "foo"];
h[c, "bar"]];
Is there something like that? I believe that Haskell's where
supports this use case.
LetL
function by @LeonidShifrin from there. Other alternatives are theGeneralUtilities`Where
function and the undocumented form ofWith
(see answer by @MichaelE2 there) $\endgroup$