Cosmo/examples/increment.cosmo

39 lines
774 B
Plaintext

proto Vector
function __init(self)
self.vector = []
self.x = 0
end
function push(self, val)
self.vector[self.x++] = val
end
function pop(self)
return self.vector[--self.x]
end
function __index(self, key)
return self.vector[key]
end
function __iter(self)
// you don't *have* to make a new object, i just wanted to show off anonymous functions
return {__next = (function(self)
return self.vector[self.iterIndex++]
end),
iterIndex = 0,
vector = self.vector
}
end
end
var vector = Vector()
for (var i = 0; i < 100000; i++) do
vector:push(i)
end
local total = 0
for i in vector do
print(i)
end