www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - (St)Range behavior

reply "Chris" <wendlec tcd.ie> writes:
I have three lazy ranges tied together in a foreach loop like so:

Item[] items; // An array of Item-struct

foreach (item; items.range1.range2.range3) {
   // ...
}

The funny thing is that when I run the foreach loop it does 
everything in reverse. So I changed it to

foreach (item; items.range3.range2.range1) {
   // ...
}

And now it works as expected. I've never had such strange 
behavior and have no clue what could have caused it. In all my 
other ranges it works from left to right 1. step, 2. step, 3. 
step and not from right to left.
Oct 25 2013
parent reply "Meta" <jared771 gmail.com> writes:
On Friday, 25 October 2013 at 11:06:18 UTC, Chris wrote:
 I have three lazy ranges tied together in a foreach loop like 
 so:

 Item[] items; // An array of Item-struct

 foreach (item; items.range1.range2.range3) {
   // ...
 }

 The funny thing is that when I run the foreach loop it does 
 everything in reverse. So I changed it to

 foreach (item; items.range3.range2.range1) {
   // ...
 }

 And now it works as expected. I've never had such strange 
 behavior and have no clue what could have caused it. In all my 
 other ranges it works from left to right 1. step, 2. step, 3. 
 step and not from right to left.
Can you post some code?
Oct 25 2013
parent reply "Chris" <wendlec tcd.ie> writes:
On Friday, 25 October 2013 at 12:57:51 UTC, Meta wrote:
 On Friday, 25 October 2013 at 11:06:18 UTC, Chris wrote:
 I have three lazy ranges tied together in a foreach loop like 
 so:

 Item[] items; // An array of Item-struct

 foreach (item; items.range1.range2.range3) {
  // ...
 }

 The funny thing is that when I run the foreach loop it does 
 everything in reverse. So I changed it to

 foreach (item; items.range3.range2.range1) {
  // ...
 }

 And now it works as expected. I've never had such strange 
 behavior and have no clue what could have caused it. In all my 
 other ranges it works from left to right 1. step, 2. step, 3. 
 step and not from right to left.
Can you post some code?
And it is also circular. range3 sends it to range 1 again I must have screwed it up somewhere. Everything is ok, if I do it step by step: foreach (item; items.range1) {} foreach (item; items.range2) {} foreach (item; items.range3) {} Has anyone ever come across a bug like this? Each range works fine on its own. When I tie them together, they go upside down and in circles. Weird. Maybe I can extract some test code.
Oct 25 2013
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Friday, 25 October 2013 at 14:02:54 UTC, Chris wrote:
 On Friday, 25 October 2013 at 12:57:51 UTC, Meta wrote:
 On Friday, 25 October 2013 at 11:06:18 UTC, Chris wrote:
 I have three lazy ranges tied together in a foreach loop like 
 so:

 Item[] items; // An array of Item-struct

 foreach (item; items.range1.range2.range3) {
 // ...
 }

 The funny thing is that when I run the foreach loop it does 
 everything in reverse. So I changed it to

 foreach (item; items.range3.range2.range1) {
 // ...
 }

 And now it works as expected. I've never had such strange 
 behavior and have no clue what could have caused it. In all 
 my other ranges it works from left to right 1. step, 2. step, 
 3. step and not from right to left.
Can you post some code?
And it is also circular. range3 sends it to range 1 again I must have screwed it up somewhere. Everything is ok, if I do it step by step: foreach (item; items.range1) {} foreach (item; items.range2) {} foreach (item; items.range3) {} Has anyone ever come across a bug like this? Each range works fine on its own. When I tie them together, they go upside down and in circles. Weird. Maybe I can extract some test code.
How strange. Sorry, without seeing some code I doubt it's possible to understand what's happening.
Oct 25 2013
parent "Chris" <wendlec tcd.ie> writes:
On Friday, 25 October 2013 at 16:17:24 UTC, John Colvin wrote:
 On Friday, 25 October 2013 at 14:02:54 UTC, Chris wrote:
 On Friday, 25 October 2013 at 12:57:51 UTC, Meta wrote:
 On Friday, 25 October 2013 at 11:06:18 UTC, Chris wrote:
 I have three lazy ranges tied together in a foreach loop 
 like so:

 Item[] items; // An array of Item-struct

 foreach (item; items.range1.range2.range3) {
 // ...
 }

 The funny thing is that when I run the foreach loop it does 
 everything in reverse. So I changed it to

 foreach (item; items.range3.range2.range1) {
 // ...
 }

 And now it works as expected. I've never had such strange 
 behavior and have no clue what could have caused it. In all 
 my other ranges it works from left to right 1. step, 2. 
 step, 3. step and not from right to left.
Can you post some code?
And it is also circular. range3 sends it to range 1 again I must have screwed it up somewhere. Everything is ok, if I do it step by step: foreach (item; items.range1) {} foreach (item; items.range2) {} foreach (item; items.range3) {} Has anyone ever come across a bug like this? Each range works fine on its own. When I tie them together, they go upside down and in circles. Weird. Maybe I can extract some test code.
How strange. Sorry, without seeing some code I doubt it's possible to understand what's happening.
I'll try to extract some code I can post next week, or if I fix it, will let you know what it was.
Oct 26 2013