r/haxe•
phobius

Regular C-style for loops?

Hi r/haxe!

I've been slowly trying to ween myself onto Haxe recently since it seems a more sustainable option.

But one thing that's difficult to look past is the single type of python-style for loops.

A construct I use a lot in image processing and game code is:

// this is c/java pseudocode
int width = 640;
int height = 480;
int area = width * height;
float linear2dArray[] = new float[area];

// perform a series of calculations which rely on X, Y
for (int y=0, offset = 0; y < height ; y++) {
   for (int x=0; x < width; x++, offset++) {
       linear2dArray[offset] = someGenerator( x, y );
   }
}

This is beneficial since it:

  • avoids having to constantly calculate y * width + x
  • avoids speed hit modulo and div when deriving x,y from the linear value:
    • eg. x, y = offset % width, offset / height
  • avoids all sorts of bugs quite easily (although it's easy to forget to add the offset++ and wonder what's happening)

Anyway, whilst I'm not a huge fan of ActionScript style variable definitions, this losing this for loop construct is perhaps the biggest negative for me in using Haxe.

Tl;dr has anyone written a macro which perhaps emulates this functionality, or heard any word of additional for loops being added to the language?


UPDATE I found this via this post. Have yet to test it out (or more importantly, profile it)

Posted Apr 24, 2013, 10:15 AM UTC · edited

42
phobiusOP•

ps. I'm well aware that you can use a similar construct with Haxe-style loops, this is more a question of (a) is anyone familiar enough with the macro language to say whether this would be possible? (b) has anyone found an elegant solution to this problem which works better in Haxe?

2
phobiusOP•

found an implementation, see the update

2