# A smarter nested With?

I often find myself writing code that looks a bit like this:

f[x_Integer] :=
With[
{
range = Range[2] + x
},
With[
{
a = range[[1]],
b = range[[2]],
c = g[range]
},
h[a,b,c]
]
];


It would be nice if I could avoid Withs and just write

f[x_Integer] :=
Let[
range = Range[2] + x,
{a,b} = range,
c = g[range]
,
h[a,b,c]
];


which would then automatically expand to the above at definition time.

What I'm asking is a bit similar to this question. There are additional requirements however. The new scoping construct (Let in the above) should:

• Group sequential disjoint assignments into single Withs.
• Thread over List assignments.

Of course, it should not evaluate the left-hand-sides and the right-hand-sides of the assignments while expanding to Withs.

Any proposals for such a scoping construct? (I'll post my version soon).

• Your example doesn't need any scoping: f[x_Integer] := h[Sequence@@#,g@#]&@(Range[2]+x) – Bob Hanlon Nov 5 '14 at 14:24
• Why do you want to rewrite using With rather than preserving a higher abstraction such as LetL? – Mr.Wizard Nov 5 '14 at 14:56
• Also: what is the reason to prefer With over Module? Assignments such as {a,b} = range are simpler with the latter. – Mr.Wizard Nov 5 '14 at 15:00
• @Mr.Wizard This should be a generalization of LetL, which doesn't do the two points I mentioned (Leonid's answer below does). Module doesn't allow you to do threaded assignments in the first argument, forcing you to write Module[{a,b},{a,b}=Range[2];...], which is duplication I don't like. Also, it'd like to inject into held expressions -- another reason not to go with Module. – Teake Nutma Nov 5 '14 at 15:07
• If you are ok with undocumented features, you can supply multiple arguments to With in Mathematica 10.3 and above: e.g. With[{c = d}, {b = c}, {a = b}, a] (You'll have to also tolerate the red syntax coloring in the front end, or turn it off manually) [and I just realized that it doesn't satisfy your second requirement of threading over List assignments] – QuantumDot May 4 '16 at 20:03

With this helper function:

SetAttributes[partThread, HoldAll];
Join @@ Replace[
Hold[s_, i_] :> Hold[s = rhs[[i]]],
{1}];


The following modification of LetL seems to work according to your specs:

ClearAll[Let, let];
SetAttributes[{Let, let}, HoldAll];

Let /: Verbatim[SetDelayed][lhs_, rhs : HoldPattern[Let[__, _]]] :=
Block[{With}, Attributes[With] = {HoldAll};
lhs := Evaluate[rhs /. HoldPattern[With[{}, b_]] :> b]
];

Let[args___, body_] := let[{args}, body, {}, {}];

let[{}, body_, {}, _] := With[{}, body];

let[{Set[{s___}, rhs_], rest___}, body_, dec_, syms_] :=
Module[{temp},
let[{temp = rhs, d, rest}, body, dec, syms]
];

let[
{Set[sym_, rhs_], rest___},
body_,
{decs___},
{syms___}
] /; FreeQ[Unevaluated[rhs], Alternatives[syms]] :=
let[{rest}, body, {decs, sym = rhs}, {syms, HoldPattern[sym]}];

let[{args___}, body_, {decs__}, _] :=
Block[{With},
Attributes[With] = {HoldAll};
With[{decs},Evaluate[let[{args}, body, {}, {}]]]
];


This works quite similarly to LetL. What it does in addition to LetL is that it collects previous declarations into auxiliary lists stored as extra arguments of let, so that it can group together disjoint declarations. It also threads over arguments, using the partThread helper function. In all other respects it is the same code as LetL.

f[x_Integer] :=
Let[range = Range[2] + x, {a, b} = range, c = g[range], h[a, b, c]];


we can check what was generated:

?f

Globalf

  f[x_Integer]:=
With[{range=Range[2]+x},
With[{a=range[[1]],b=range[[2]],c=g[range]},h[a,b,c]]]

• @MichaelE2 That was an evaluation leak, thanks for reporting. I seem to have fixed it, although perhaps not very elegantly. – Leonid Shifrin Nov 5 '14 at 14:40
• You're welcome. It seems fixed now. – Michael E2 Nov 5 '14 at 14:51
• @MichaelE2 Thanks for double-checking. I've not done this sort of things for some while, got a bit rusty. – Leonid Shifrin Nov 5 '14 at 14:52
• That's a bit more elegant than what I had :). One question though: why doesn't Alternatives leak evaluation here? – Teake Nutma Nov 5 '14 at 15:09
• @TeakeNutma because I wrapped symbols in HoldPattern, and use Unevaluated for the rhs. HoldPattern is exactly the right tool for the job here, since it is invisible to the pattern-matcher (and so FreeQ` works fine). – Leonid Shifrin Nov 5 '14 at 15:09