1

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?

orome
  • 12,819
  • 3
  • 52
  • 100
  • Yes, duplicate question. Though the answers there don't really address the point. – orome Dec 04 '12 at 19:09
  • Then I don't see what the question is. My answer is this: yes, this situation is very common in practice, and then I use 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. – Leonid Shifrin Dec 04 '12 at 19:13
  • Yes, sorry, I wasn't clear: closing is fine. – orome Dec 04 '12 at 19:16
  • I think this could probably be reformulated in a way which would emphasize that you are looking for alternative solutions. If that's so, it does not have to be closed. – Leonid Shifrin Dec 04 '12 at 19:19
  • @LeonidShifrin: Actually, it's more the opposite: I'm interested in whether this is what the community commonly does in this such situations. – orome Dec 04 '12 at 19:29
  • I would say yes, although this is based on my personal perception and may be not entirely correct. – Leonid Shifrin Dec 04 '12 at 19:32
  • @LeonidShifrin: Well. I'd accept that (from an experienced user) as a valid answer to this question. (The other question seems more focused on some alternatives.) – orome Dec 04 '12 at 19:35

0 Answers0