Foreach with byte problems

Steven Schveighoffer schveiguy at yahoo.com
Fri Feb 25 11:44:44 PST 2011


On Fri, 25 Feb 2011 14:34:35 -0500, Andrej Mitrovic  
<andrej.mitrovich at gmail.com> wrote:

> Can this be simplified?:
>
> byte[] arr = to!(byte[])(array(iota(byte.min, byte.max+1)));
>
> The +1 turns byte.max into an int that can store 128. So when I call
> array on an iota, I'll get back an int[] of [-128, ..., 127]. And I
> have to convert that to a byte[].

This is a good puzzle.  You may have to do something with map and casting,  
but I don't see how it can be done with iota without a cast.

The right thing to do would be this:

for(byte i = -127; i < byte.max + 1; i++)
   arr ~= i;

which does everything as a byte, but does the comparisons as ints.

But I don't know how to make iota do this.

-Steve


More information about the Digitalmars-d-learn mailing list