make (range) return an Iterable & Iterator

This commit is contained in:
2021-07-24 11:21:02 -06:00
parent 8a1ea8c8ca
commit 3652d8e107

View File

@@ -239,11 +239,11 @@ class Prelude {
} }
// Ranges with a min, exclusive max, and step size, just like Python. // Ranges with a min, exclusive max, and step size, just like Python.
public static function range(min, max, step):Iterator<Int> { public static function range(min, max, step):Iterator<Int> & Iterable<Int> {
if (step <= 0 || max < min) if (step <= 0 || max < min)
throw "(range...) can only count up"; throw "(range...) can only count up";
var count = min; var count = min;
return { var iterator = {
next: () -> { next: () -> {
var oldCount = count; var oldCount = count;
count += step; count += step;
@@ -253,6 +253,11 @@ class Prelude {
count < max; count < max;
} }
}; };
return {
iterator: () -> iterator,
next: () -> iterator.next(),
hasNext: () -> iterator.hasNext()
};
} }
static function _joinPath(parts:Array<Dynamic>) { static function _joinPath(parts:Array<Dynamic>) {