Possible Duplicate:
How to avoid nested With[]?
I have many situations where I have a constant that is local to a function, with other constants that are computed from it. I see that I can implement this using nested With
statements, like
leapSeconds = With[{
usnoData =
Import["http://maia.usno.navy.mil/ser7/tai-utc.dat", "Text",
CharacterEncoding -> "UTF8"]},
With[{
lineLength = StringPosition[usnoData, "\n"][[1, 1]],
lsCount = Length[StringCases[usnoData, "\n"]] + 1},
DateList[#] & /@ (StringTake[
usnoData, {2 + #*lineLength, 9 + #*lineLength}] & /@
Range[0, lsCount - 1])]]
but this seems cumbersome. Is there a better way to do this sort of thing (I know that for this example I could dispense with the constants, but assume for the exercise that I do indeed need the constants)? Is nesting With
statments a reasonable approach, or something I've just come up with by not understanding Mathematica very well?
LetL
, described in my answer to that question. The only other option is to separate every single constant's computation into a separate function, and then chain those functions, but often this may not be desirable, both because of extra boilerplate of parameter-passing, and because those new functions may not be general enough to justify their existence. $\endgroup$