Cosmo/examples/iterator.cosmo

29 lines
432 B
Plaintext
Raw Normal View History

proto Range
func __init(self, x)
self.max = x
end
func __iter(self)
self.i = 0
return self
end
func __next(self)
if self.i >= self.max then
return nil // exit iterator loop
end
2020-12-31 19:57:02 +00:00
return self.i++
end
end
2020-12-31 19:57:02 +00:00
for i in Range(10) do
print("->" .. i)
end
local total = 0
2020-12-31 19:57:02 +00:00
for i in Range(1000000) do
total = total + i
end
print("total: " .. total)