Help with regards to making a dynamic for loop akin to flash

118 views
Skip to first unread message

Kaelan Evans

unread,
Mar 20, 2017, 1:56:42 PM3/20/17
to Haxe
One of the things I remember I was able to do in flash was a for loop that would only go to the items that were indexed. I have a 2 dimensional array (Map) that indexes items in arbitrary order and location. In flash if I construct a loop like this:

myArray:Array = [null, item, null, null, null, item];

for(i in myArray){
 myArray
[i].doTheThing();
}

The for loop would only loop twice as there are only two items in that array, this is great as it can reduce the load on how many loops it needs to perform. However in haxe, this is the closest I can get to the above example:

myArray:Array = [null, item, null, null, null, item];

for(in 0...myArray.length){
 if(myArray[i] != null) {
  myArray
[i].doTheThing();
 }
}

What's causing my problems is that I have to add a null check to make sure that myArray[i] isn't empty when I run the loop. This is telling me that it's checking every single index anyways, causing a bloat in the loop. I'm definitely sure it's because of how I constructed the loop, it clearly looking like I'm telling it to go through every index, but I can't find any info on having the loop behave like I want in the previous example. Thank you for the help!
Message has been deleted

Mark Knol

unread,
Mar 21, 2017, 4:25:07 AM3/21/17
to Haxe
You have to do `for (value in list)`, it is not `for (index in list)`, this is how Haxe iterators on arrays work.

So, to translate that to your example:

for(item in myArray) {
  item.doTheThing();
}

Regarding the null checks, this is needed, because there are items null in your array, so this would be safest:

for(item in myArray) {
  if(item != null) {
    item.doTheThing();
  }
}

Check out this demo: http://try.haxe.org/#E1498 

Kaelan Evans

unread,
Mar 21, 2017, 11:34:37 AM3/21/17
to Haxe
Okay thank you for the example, but am I stuck with having bloated loops? Because what I primarily want it to have them more optimized. If the object was empty/null in my array, flash wouldn't even bother iterating on it.

Greybeard

unread,
Mar 22, 2017, 3:59:48 AM3/22/17
to Haxe


On Tuesday, March 21, 2017 at 8:34:37 AM UTC-7, Kaelan Evans wrote:
Okay thank you for the example, but am I stuck with having bloated loops? Because what I primarily want it to have them more optimized. If the object was empty/null in my array, flash wouldn't even bother iterating on it.

If you were truly using an array, then it's a pretty good guess that Flash was just doing that null check for you.  If you actually measure the speed of your loop, you will most likely find that loop with null checks is negligibly slower.  If you don't want to visit null nodes, (and particularly if you don't care about ordering), then a linked list, or even a hash table is probably a better data structure to use.  

If you absolutely need an array and you find that the loop really is slow, then you can create parallel data structures (such as bit arrays) that track whether an array entry is set and have much faster scan speeds.  But it's at the cost of extra memory, code, and complexity.

PSvils

unread,
Mar 23, 2017, 3:50:53 PM3/23/17
to Haxe
I agree with above posters, but also logically I think it's a really bad decision if it automatically skips null elements, since the code cannot infer what that null might mean to your application in a logical sense. Because those elements ARE there, just ... well, null.
But having this structure indicates you should take better care in maintaining the data. If you need cheap element removal, and you don't care about element order, a fast technique for static targets is having a constant size array, and track the length manually. When adding an element, add it at index "length", and increase the length by one. When removing a random element, you swap it with the last element, and then decrease the length. (I use this, and is in case you are worried about performance issues due to pushing / random removal constantly)

If you'd like, more context to your problem would help us indicate better possible solutions :)

JLM

unread,
Mar 23, 2017, 6:24:29 PM3/23/17
to Haxe
On Tuesday, 21 March 2017 15:34:37 UTC, Kaelan Evans wrote:
Okay thank you for the example, but am I stuck with having bloated loops? Because what I primarily want it to have them more optimized. If the object was empty/null in my array, flash wouldn't even bother iterating on it.

Flash code wise is far from perfect in regard to Arrays, I think Haxe has far better approach. For instance have you seen this bug.

https://tracker.adobe.com/#/view/FP-3959254

// AS3 'push' fix
arr[ arr.length ] = newVal;

// AS3 clear array fix
arr.length = 0;


Not sure if Haxe avoids this problem but trust me your far better off with Haxe, just embrace the differences and you will soon be upset when a client insists on you using lesser languages, typescript, as3, js, php etc....
Reply all
Reply to author
Forward
0 new messages