aboutsummaryrefslogtreecommitdiff
path: root/containers.moon
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-12-18 17:34:09 -0800
committerBruce Hill <bruce@bruce-hill.com>2018-12-18 17:34:35 -0800
commit908243ba21503d3b421e0c137dc6bfc8d15bb105 (patch)
tree9fdf94a5c6cd3518b96e8492c2f39b900c310b95 /containers.moon
parent79881757fb924ce1e776f3d8e429eb3e0e185d18 (diff)
Added comprehension form to containers, e.g. List(function(add) add(5)
end)
Diffstat (limited to 'containers.moon')
-rw-r--r--containers.moon23
1 files changed, 21 insertions, 2 deletions
diff --git a/containers.moon b/containers.moon
index c713bb6..26c1032 100644
--- a/containers.moon
+++ b/containers.moon
@@ -95,7 +95,16 @@ _list_mt =
_list_mt.__index.as_lua = _list_mt.as_lua
_list_mt.__index.as_nomsu = _list_mt.as_nomsu
-List = (t)-> setmetatable(t, _list_mt)
+List = (t)->
+ if type(t) == 'table'
+ return setmetatable(t, _list_mt)
+ elseif type(t) == 'function'
+ l = setmetatable({}, _list_mt)
+ add = (...)->
+ for i=1,select('#',...) do l[#l+1] = select(i,...)
+ t(add)
+ return l
+ else error("Unsupported List type: "..type(t))
walk_items = (i)=>
i = i + 1
@@ -150,7 +159,17 @@ _dict_mt =
if ret[k] == nil then ret[k] = -v
else ret[k] -= v
return Dict(ret)
-Dict = (t)-> setmetatable(t, _dict_mt)
+Dict = (t)->
+ if type(t) == 'table'
+ return setmetatable(t, _dict_mt)
+ elseif type(t) == 'function'
+ d = setmetatable({}, _dict_mt)
+ add = (...)->
+ for i=1,select('#',...) do d[select(i,...)] = true
+ add_1_eq_2 = (k, v)-> d[k] = v
+ t(add, add_1_eq_2)
+ return d
+ else error("Unsupported Dict type: "..type(t))
for i,entry in ipairs(Dict({x:99}))
assert(i == 1 and entry.key == "x" and entry.value == 99, "ipairs compatibility issue")