www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - foreach_reverse error

reply "ixid" <nuaccount gmail.com> writes:
With this code foreach works but foreach_reverse does not:

void main()
{   int[] ints = [2,2,2,5,5,5,4,4,4];
     auto temp = group(ints);
     foreach_reverse(i;temp)
         i[1].writeln;
}

Is this a known issue? Is there a sensible reason for this?
May 09 2012
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 05/09/2012 10:50 PM, ixid wrote:
 With this code foreach works but foreach_reverse does not:

 void main()
 { int[] ints = [2,2,2,5,5,5,4,4,4];
   auto temp = group(ints);
   foreach_reverse(i;temp)
   i[1].writeln;
 }

 Is this a known issue? Is there a sensible reason for this?

group returns a lazy forward range. use foreach(i; group(retro(ints)))
May 09 2012
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 05/09/2012 11:33 PM, ixid wrote:
 Thanks, sorry to have not understand such a basic point, it's rather
 hard to get to grips with everything like this in D.

No worries.
May 09 2012
prev sibling parent mta`chrono <chrono mta-international.net> writes:
 group returns a lazy forward range. use foreach(i; group(retro(ints)))

Yet another reason foreach_reverse needs to go. T

No please don't! There are hundred and ten very usefull cases.
May 11 2012
prev sibling next sibling parent "ixid" <nuaccount gmail.com> writes:
Thanks, sorry to have not understand such a basic point, it's 
rather hard to get to grips with everything like this in D.
May 09 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, May 09, 2012 at 11:09:15PM +0200, Timon Gehr wrote:
 On 05/09/2012 10:50 PM, ixid wrote:
With this code foreach works but foreach_reverse does not:

void main()
{ int[] ints = [2,2,2,5,5,5,4,4,4];
  auto temp = group(ints);
  foreach_reverse(i;temp)
  i[1].writeln;
}

Is this a known issue? Is there a sensible reason for this?

group returns a lazy forward range. use foreach(i; group(retro(ints)))

Yet another reason foreach_reverse needs to go. T -- It only takes one twig to burn down a forest.
May 09 2012
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
H. S. Teoh:

 Yet another reason foreach_reverse needs to go.

Nope, the specific kind of range of group not being what the OP desired is not foreach_reverse fault. Bye, bearophile
May 09 2012
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 11 May 2012 03:43:53 -0400, mta`chrono  
<chrono mta-international.net> wrote:

 group returns a lazy forward range. use foreach(i; group(retro(ints)))

Yet another reason foreach_reverse needs to go. T

No please don't! There are hundred and ten very usefull cases.

No, there is only one. foreach_reverse(i; 0..10) {...} Every other usage of foreach_reverse can be rewritten with foreach. I would argue that this should work: foreach(i; 10..0) but it would be quite odd given that you would start with 9 and end with 0. I don't have a really good answer for this, foreach(i; 9..-1) isn't really acceptable, especially if i is unsigned. -Steve
May 11 2012