I went and added an iterator() method to Range in lang/Numbers.ooc, and then David's code works just fine :)
I was quite surprised about this, because Range is a cover so I couldn't see any way to implement the Iterable class, but it turns out this is not needed.
Should I make a pull request with this in the SDK?
Range: cover {
min, max: Int
new: static func (.min, .max) -> This {
this : This
this min = min
this max = max
return this
}
reduce: func (f: Func (Int, Int) -> Int) -> Int {
acc := f(min, min + 1)
for(i in min + 2..max) acc = f(acc, i)
acc
}
iterator: func -> RangeIterator<Int> {
RangeIterator<Int> new(this)
}
}
RangeIterator: class <T> extends Iterator<T> {
i: Int
range: Range
init: func (=range) {
i = range min
}
hasNext?: func -> Bool {
i < range max-1
}
next: func -> T {
i += 1
i
}
remove: func -> Bool { false }
}