Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

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 questionthis 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).

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).

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).

edited tags
Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Source Link
Teake Nutma
  • 6k
  • 1
  • 25
  • 49

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).