Cosmo/examples/increment.cosmo

39 lines
754 B
Plaintext
Raw Normal View History

2020-12-05 23:58:56 +00:00
proto Vector
func __init(self)
2021-01-09 05:07:44 +00:00
self.vector = []
2020-11-20 21:10:49 +00:00
self.x = 0
end
func push(self, val)
2020-11-20 21:32:12 +00:00
self.vector[self.x++] = val
2020-11-20 21:10:49 +00:00
end
func pop(self)
2020-11-20 21:32:12 +00:00
return self.vector[--self.x]
2020-11-20 21:10:49 +00:00
end
func __index(self, key)
2020-11-20 21:32:12 +00:00
return self.vector[key]
2020-11-20 21:10:49 +00:00
end
func __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
2020-11-20 21:10:49 +00:00
end
let vector = Vector()
2020-11-20 21:10:49 +00:00
for (let i = 0; i < 100000; i++) do
vector:push(i)
2020-11-20 21:10:49 +00:00
end
local total = 0
for i in vector do
print(i)
2020-11-20 21:10:49 +00:00
end