HaxeFoundation / code-cookbook

The Haxe Code Cookbook - A community driven resource website for learning Haxe in practise
https://code.haxe.org
112 stars 87 forks source link

Add EnumerateIterator custom iterator #173

Closed Kallekro closed 8 months ago

Kallekro commented 8 months ago

Added custom iterator EnumerateIterator that returns both index and item when iterating.

I personally use this pattern very often, so wanted to share.

kLabz commented 8 months ago

Thing is, that's not needed.

https://try.haxe.org/#62b71c31

    var arr = ["haxe", "is", "for", "winners"];
    for (idx => item in arr) {
      trace(idx, item);
    }

Works already, because of keyValueIterator() that is present on Array and can be added to your types (also works through static extension). See https://haxe.org/manual/lf-iterators.html#since-haxe-4.0.0

Kallekro commented 8 months ago

Oh really, that's kinda hilarious I didn't know that all this time!

But thanks for the heads up! Maybe the manual could show a small example of it being used on an array? Because I actually saw that documentation, but thought it only applied to maps.