Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
May 09, 2012 foreach_reverse error | ||||
---|---|---|---|---|
| ||||
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 Re: foreach_reverse error | ||||
---|---|---|---|---|
| ||||
Posted in reply to ixid | 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 Re: foreach_reverse error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | 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 Re: foreach_reverse error | ||||
---|---|---|---|---|
| ||||
Posted in reply to ixid | 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 Re: foreach_reverse error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | 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 10, 2012 Re: foreach_reverse error | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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 11, 2012 Re: foreach_reverse error | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | >> 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 Re: foreach_reverse error | ||||
---|---|---|---|---|
| ||||
Posted in reply to mta`chrono | 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
|
Copyright © 1999-2021 by the D Language Foundation