Iterating over Floats with a specific step
When you want to iterate over a range of decimals, or skipping something other then one, this class does the trick.
class StepIter { var start:Float; var end:Float; var step:Float; var current:Float; var test:Float->Float->Bool; public function new(start, end, ?step = 1.0) { this.start = start; this.current = start; this.end = end; this.step = step; if (step > 0) { test = function(c, e) return c < e; } else if (step < 0) { test = function(c,e) return c > e; } else { throw "Must step over the range"; } } public function hasNext() return test(current, end) public function next() { var ret = current; current += step; return ret; } }
Usage
for(i in new StepIter(0, 1, 0.1) Lib.print(i); for(i in new StepIter(1, 0, -0.1) Lib.print(i);
version #7157, modified 2009-10-25 02:05:47 by tylermac