Proposal of a general do-while loop
Benjamin Phillips
blackllotus at gmail.com
Tue Jul 17 14:54:48 PDT 2007
downs Wrote:
> Taro Kawagishi wrote:
> > I think a more natural way to express the logic is to write the code as in listing 4.
> >
> > // listing 4
> > size_t pos = 0;
> > do {
> > pos = text.find(pattern, pos);
> > } while (pos != string::npos) {
> > cout << "pattern found at " << pos << "\n";
> > ++pos;
> > }
> >
> > The meaning of
> >
> > do {
> > aa;
> > } while (bb) {
> > cc;
> > }
> >
> > is
> >
> > while (true) {
> > aa;
> > if (not bb) {
> > break;
> > }
> > cc;
> > }
>
> void doWhile(void delegate() pre, lazy bool cond, void delegate() post) {
> while (true) {
> pre;
> if (!cond()) break;
> post;
> }
> }
>
> // listing 4, modified
> size_t pos=0;
> doWhile ({
> pos=text.find(pattern, pos);
> }, pos!=string::npos, {
> writefln("Pattern found at ", pos);
> ++pos;
> });
>
> Not tested, but should work.
> Have fun!
Being able to create templates that basically act like new keywords is one thing I love about D. I think your solution is better than any of the others posted.
More information about the Digitalmars-d
mailing list