import std.range;
void main()
{
auto arr = [1, 2, 3, 4, 5, 6, 7, 8];
auto foo = cycle(arr);
// nope
foreach (int index, int val; foo)
{
}
// nope
foreach (int index, int val; take(foo, 5))
{
}
// ok
foreach (int index, int val; tak
Andrej Mitrovic:
> Is this because cycle is an infinite range, and index might overflow?
The cause is different: cycle is a range, and they don't define an counter
variable. With opApply sometimes you define the counter too. The compiler
doesn't create a counter variable for free.
Bye,
bearoph
On Thu, 02 Jun 2011 14:17:26 -0400, Andrej Mitrovic wrote:
import std.range;
void main()
{
auto arr = [1, 2, 3, 4, 5, 6, 7, 8];
auto foo = cycle(arr);
// nope
foreach (int index, int val; foo)
{
}
// nope
foreach (int index, int val; take(foo, 5))
{
}
/
Well actually all I wanted was a counter variable. I can put the thing
in a for loop or while loop, but I thought that a counter variable is
something that the compiler can always add without too much thinking.
I guess things aren't so simple.
On Thu, 02 Jun 2011 17:38:38 -0400, Andrej Mitrovic
wrote:
Well actually all I wanted was a counter variable. I can put the thing
in a for loop or while loop, but I thought that a counter variable is
something that the compiler can always add without too much thinking.
I guess things aren't
On Thu, 02 Jun 2011 17:38:38 -0400, Andrej Mitrovic
wrote:
Well actually all I wanted was a counter variable. I can put the thing
in a for loop or while loop, but I thought that a counter variable is
something that the compiler can always add without too much thinking.
Also, BTW, you can st
On 6/2/11, Steven Schveighoffer wrote:
> On Thu, 02 Jun 2011 17:38:38 -0400, Andrej Mitrovic
> wrote:
>
>> Well actually all I wanted was a counter variable. I can put the thing
>> in a for loop or while loop, but I thought that a counter variable is
>> something that the compiler can always add
On Thu, 02 Jun 2011 18:01:21 -0400, Andrej Mitrovic
wrote:
On 6/2/11, Steven Schveighoffer wrote:
On Thu, 02 Jun 2011 17:38:38 -0400, Andrej Mitrovic
wrote:
Well actually all I wanted was a counter variable. I can put the thing
in a for loop or while loop, but I thought that a counter va
Steven Schveighoffer:
> So I'm not sure how this would be solved, but it's definitely complicated.
To solve this problem Python uses the enumerate function:
>>> for c in "abc":
... print c
...
a
b
c
>>> for i,c in enumerate("abc"):
... print i, c
...
0 a
1 b
2 c
In D it's easy to create som