Re: Mathmode.pdf and braket.sty



On Tue, 25 Oct 2005 14:43:51 -0500, Jonathan Fine <jfine@xxxxxxxxx> wrote:

We can optimise this a bit more, at least when processing
a large number of items.

% not tested, but hope that all errors are only (?) typos
\def\new@for@loop #1#2#3,{%
   \def#1{#3}%
   \ifx\@nnil#1%
      \new@for@loop@exit % cunning \fi replacement trick goes here
   \fi
   #2%
   \new@for@loop #1{#2}%
}
\def\new@for@loop@exit #1 \new@for@loop #2#3{\fi}

Small typo here although with more suitable catcode settings for coding it wouldn't be (at least, what I find suitable for coding :-).


The exit function is good. I have added it but in a way that is less cunning and perhaps a little more general.

The thing is that different loop types will contain different code where one effectively would have to design a special exit function for each one. What I did instead was adding an extra stop marker at the end of the internal rearrangement and then simply gobble everything up to that one. The advantage (besides working for all these loop constructs) is that this will then also be the same function people can use for breaking a loop if a certain item was found. Since some functions might very well return the result of such a test *after* exiting the underlying \if...\fi structure, I found it better to not let it first gobble and then re-insert a \fi. After all, I would rather tell people to insert an \expandafter once in a while rather than having to add some obscure code like \iftrue\break@this@loop\fi.

% Generic quark like thingies that belong elsewhere
\def\q@nil{\q@nil}
\def\q@stop{\q@stop}
\long\def\@gobble@to@q@stop #1\q@stop{}

% for comma lists:
\let\new@for@break \@gobble@to@q@stop
\def\new@for#1#2#3{%
  \new@for@loop#1{#3}#2,\q@nil,\q@stop
}
\def\new@for@loop#1#2#3,{%
  \def#1{#3}%
  \ifx\q@nil#1%
    \expandafter\new@for@break
  \fi
  #2\new@for@loop#1{#2}%
}

And if we are willing to forbid loop nesting, we can avoid
having to shuffle tokens about (this is something that
irritates me when writing expandable list processing macros).

Such shuffling is a performance hit.

True. However people tend to use nested loops (I know I have seen it) so that is a severe restriction. It is not something I'd want to do for the loop that assigns a tmp-var as the one(s) we have discussed in this (now wildly OT) thread.


But I've shown you these two tricks, just in case you're
interested.

I'm always interested in tricks... -- Morten .



Relevant Pages

  • Re: How can i clear the contents of an existing db to start again.
    ... > Vincent and Van have told you the mechanics of how to delete from existing ... You could also delete all the tables in a loop and repeat the loop a few times. ... Case "MSys" 'Don't delete the MSys* tables ... Exit Function ...
    (microsoft.public.access.queries)
  • RE: Delete All Rows Each Table
    ... You can loop through the table collection and then perform a sim Query to ... If Err.Number = 0 Then Exit Function ... I have a MS-Access database that has approximately 20 tables. ...
    (microsoft.public.access.modulesdaovba)
  • Do Loop problem
    ... I have a variable number of records in a table I use to build my pass-thru SQL ... Then I perform a loop for how many times I need, ... any suggestions/corrections would be greatly appreciated. ... Exit Function ...
    (microsoft.public.access.modulesdaovba)
  • Re: Stumped why my function doesnt return when I ask it to
    ... And I haven't noticed before because most of my other functions end within a If loop. ... Thank you very much, I put the exit function in, and every thing worked perfectly! ... inArray-> 'lastPage'!= 'token' ... Public Function inArray ...
    (microsoft.public.scripting.vbscript)

Loading