aboutsummaryrefslogtreecommitdiff
path: root/utils.moon
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2017-09-20 04:21:46 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2017-09-20 04:21:46 -0700
commite073b23fbfd734a55bfc065fdd0ab6b072adaffb (patch)
tree41289db7f0dc3a6638fffa0d48f6bd1505109c73 /utils.moon
parent77d37aaf0f1dcd37939c10e7cd68db414efa6e07 (diff)
Added and fixed up "when"
Diffstat (limited to 'utils.moon')
-rw-r--r--utils.moon14
1 files changed, 14 insertions, 0 deletions
diff --git a/utils.moon b/utils.moon
index c439e8f..b46bf44 100644
--- a/utils.moon
+++ b/utils.moon
@@ -51,13 +51,27 @@ utils = {
start,stop,step = 1,start,1
elseif step == nil
step = 1
+ elseif step == 0
+ error("Range step cannot be zero.")
return setmetatable({:start,:stop,:step}, {
__ipairs: =>
iter = (i)=>
if i <= (@stop-@start)/@step
return i+1, @start+i*@step
return iter, @, 0
+ __index: (i)=>
+ if type(i) != "Number" then return nil
+ if i % 1 != 0 then return nil
+ if i <= 0 or i-1 > (@stop-@start)/@step then return nil
+ return @start + (i-1)*@step
+ __len: =>
+ len = (@stop-@start)/@step
+ if len < 0 then len = 0
+ return len
+
})
+
+ nth_to_last: (list, n) -> list[#list-n+1]
keys: (t)-> [k for k in pairs(t)]
values: (t)-> [v for _,v in pairs(t)]