July 18, 2007
Steve Teale wrote:
> 
> Do we need moderators on this group, or are there just a lot of people around who have more time than things to do.
> 
> Kill this thread, it's a waste of Oxygen.

I don't get it.  Are you reading these posts out loud?  Anyway don't worry too much.  The oxygen's still there, it's just attached to a carbon atom.  I think I heard there was some way to get rid of that carbon atom, too.

--bb
July 19, 2007
"Taro Kawagishi" <tarok@acm.org> wrote in message news:f7hplb$1ovc$1@digitalmars.com...
<snip>
> I can write the same logic as in listing 2 and 3 below, but their meanings would be less clear than listing 1, because the looping condition is in the if statement together with the break statement in it, and you need to spot the if statement in the while body to understand it.
<snip>

On seeing a while (true), I think it goes without saying that you need to look for a break or return statement somewhere to find the terminating condition.

BTW here are two other ways of doing your particular case:

   // listing 5
   size_t pos = 0;
   while ((pos = text.find(pattern, 0)) != string::npos) {
       cout << "pattern found at " << pos << "\n";
       ++pos;
   }

   // listing 6
   for (size_t pos = 0; (pos = text.find(pattern, 0)) != string::npos; ++pos) {
       cout << "pattern found at " << pos << "\n";
   }

but I realise this is no use in the more general case when you've got more to do in the portion before the break.

> 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;
>    }
<snip>

I'm not sure I like this notation.  There are always going to be people who prefer to put curly brackets on a line of their own.  And then, given a snippet of code

       ...
   }
   while (cond)
   {
       ...

it'll be necessary to scroll, possibly through several screens of code, just to find out whether the block above the while is part of the loop as well.

Moreover, I don't know if it's a common pitfall to write

   do {
       ...
   }

(with no while clause) intending an infinite loop (or one that must be broken out of).  But should somebody fall into this trap and follow it immediately with a while loop, it would take on a whole new meaning.

> The current while loop and do-while loop will be specialized forms of this general do-while loop.
>
> The advantage of the new construct will be seen if you have more complex statements within do and while blocks.
> I believe allowing this extended construct will be smooth since it will not break the existing code.
<snip>

I'm still not sure it gains anything practically over the if-break method. Moreover, what if you want multiple terminating conditions at different points in the loop?  You can put any number of breaks in a loop; your idea OTOH doesn't easily extend to multiple exit points.

(For the record, Beta Basic on the ZX Spectrum had an "EXIT IF" statement, really the equivalent of if-break though it also looked a bit like your idea, especially as the automatic indentation would show that particular statement at the same level as the DO and LOOP.  If you like, you _could_ outdent your if-break statements to achieve the same effect....)

Stewart. 

July 21, 2007
Taro Kawagishi wrote:
> Hello all,
> 
> every once in a while I feel uneasy when I find I can't fit my logic into a do-while or while loop in a concise way.
> Here is a C++ example:
> 
> void
> find_string_occurrences(const string& text, const string& pattern) {
> 
>     // listing 1
>     size_t pos = text.find(pattern, 0);
>     while (pos != string::npos) {
>         cout << "pattern found at " << pos << "\n";
>         ++pos;
>         pos = text.find(pattern, pos);
>     }
> 
> }
> ...
to me the best solution would appear something along the lines of:
   loop
   {  size_t pos = text.find(pattern, 0);
      // declare a new pos on each iteration??
      if (pos == string::npos)  break;
      cout << "pattern found at " << pos << "\n";
      // tango is not standard D, but I think I understand
      // what you are doing
      ++pos;
      pos = text.find(pattern, pos);
   }

I'd prefer the Ada-esque
     exit when (pos == string::npos);
but that means introducing new key words (more than just "loop").  OTOH, I guess there's nothing wrong with having an un-parameterized "do" instead of loop, as in:
   do
   {  ... do stuff ...
      when(condition) break;
   }
in the case the "when" is semantically the same as an if, but it clarifies what's going on.  One could limit it's use to "only usable within a do loop".  But I would prefer the syntax of either:
      exit when (condition);
or
      break when (condition);
as I feel that these are clearer.
July 21, 2007
downs wrote:

> 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!

I suppose this, or something similar, would work...but UGH!! ugly.  I suppose that "De gustibus non disputandum est.", but... care for some okra pie with garlic ice cream?
July 21, 2007
Gregor Richards wrote:
> while ((pos = text.find(pattern, pos)) != string::npos) {
>     ...
> }
> 
> 
> Yeesh.
> 
>  - Gregor Richards


I agree this is the best way to solve this particular problem.  How much different is?

while ((pos = text.find(pattern, pos)) != string::npos) {
     ...
}

from

do {pos = text.find(pattern, pos)} while (pos != string::npos) {
     ...
}

do/while may be more neat if you have more then one pre-condition however.
July 21, 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!

Nice.  Not that I really think we need this new construct however this is what I was thinking. (untested)


struct WhileStruct
{
	void delegate() pre;
	bool delegate() cond;
	void opCall(void delegate() post)
	{	
		while (true)
		{	
			pre();
			if (!cond()) break;
			post();
		}
	}
}

struct Do
{

	void delegate() pre;
	static Do opCall(void delegate() pre) //could be a free function
	{
		Do d;
		d.pre = pre;
		return d;
	}

	WhileStruct While(lazy bool cond)
	{
		WhileStruct w;
		w.pre = pre;
		w.cond = {return cond();};
		return w;
	}

}

void main()
{
//	Do({...}).  //Note that "dot" is the nastiest part
//	While(...)({...});

//ie
	Do({pos=text.find(pattern, pos);}).
	While(pos != string::npos)
	({
		writefln("Pattern found at ", pos);
		++pos;
	});
	
}


Here are some other forms I think would be possible to code in a library in D.

Do({...})(...)({...});
Do[{...}]()[{...}];

Do({...})
(While(...))({...});


while (Do(..., {...}))
{

};

Do({...}) = While(...)
({});

Do({...}) = (...) = ({});
Do({...}) ~ (...) ~ ({});

Do({...});    //Essentially stores a delegate on the stack
While(...)
({...});

The power of D.
July 22, 2007
"Charles D Hixson" <charleshixsn@earthlink.net> kirjoitti viestissä news:f7t5ki$1mn0$1@digitalmars.com...
> I'd prefer the Ada-esque
>      exit when (pos == string::npos);
> but that means introducing new key words (more than just "loop").  OTOH, I guess there's nothing wrong with having an un-parameterized "do" instead of loop, as in:
>    do
>    {  ... do stuff ...
>       when(condition) break;
>    }
> in the case the "when" is semantically the same as an if, but it clarifies what's going on.  One could limit it's use to "only usable within a do loop".  But I would prefer the syntax of either:
>       exit when (condition);
> or
>       break when (condition);
> as I feel that these are clearer.

I'd say no to a new keyword just for this.

However, a syntax like

   break if (condition);

would, in my opinion, be better than

   if (condition) break; 

July 22, 2007
Rioshin an'Harthen wrote:
> "Charles D Hixson" <charleshixsn@earthlink.net> kirjoitti viestissä news:f7t5ki$1mn0$1@digitalmars.com...
>> I'd prefer the Ada-esque
>>      exit when (pos == string::npos);
>> but that means introducing new key words (more than just "loop").  OTOH, I guess there's nothing wrong with having an un-parameterized "do" instead of loop, as in:
>>    do
>>    {  ... do stuff ...
>>       when(condition) break;
>>    }
>> in the case the "when" is semantically the same as an if, but it clarifies what's going on.  One could limit it's use to "only usable within a do loop".  But I would prefer the syntax of either:
>>       exit when (condition);
>> or
>>       break when (condition);
>> as I feel that these are clearer.
> 
> I'd say no to a new keyword just for this.
> 
> However, a syntax like
> 
>    break if (condition);
> 
> would, in my opinion, be better than
> 
>    if (condition) break;

I sit back and watch as D slowly evolves into Ruby... ;)

-- Chris Nicholson-Sauls
1 2 3 4
Next ›   Last »