aboutsummaryrefslogtreecommitdiff
path: root/containers.lua
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.lua
parent79881757fb924ce1e776f3d8e429eb3e0e185d18 (diff)
Added comprehension form to containers, e.g. List(function(add) add(5)
end)
Diffstat (limited to 'containers.lua')
-rw-r--r--containers.lua36
1 files changed, 34 insertions, 2 deletions
diff --git a/containers.lua b/containers.lua
index ceb92fd..d4514af 100644
--- a/containers.lua
+++ b/containers.lua
@@ -235,7 +235,21 @@ local _list_mt = {
_list_mt.__index.as_lua = _list_mt.as_lua
_list_mt.__index.as_nomsu = _list_mt.as_nomsu
List = function(t)
- return setmetatable(t, _list_mt)
+ if type(t) == 'table' then
+ return setmetatable(t, _list_mt)
+ elseif type(t) == 'function' then
+ local l = setmetatable({ }, _list_mt)
+ local add
+ add = function(...)
+ for i = 1, select('#', ...) do
+ l[#l + 1] = select(i, ...)
+ end
+ end
+ t(add)
+ return l
+ else
+ return error("Unsupported List type: " .. type(t))
+ end
end
local walk_items
walk_items = function(self, i)
@@ -396,7 +410,25 @@ local _dict_mt = {
end
}
Dict = function(t)
- return setmetatable(t, _dict_mt)
+ if type(t) == 'table' then
+ return setmetatable(t, _dict_mt)
+ elseif type(t) == 'function' then
+ local d = setmetatable({ }, _dict_mt)
+ local add
+ add = function(...)
+ for i = 1, select('#', ...) do
+ d[select(i, ...)] = true
+ end
+ end
+ local add_1_eq_2
+ add_1_eq_2 = function(k, v)
+ d[k] = v
+ end
+ t(add, add_1_eq_2)
+ return d
+ else
+ return error("Unsupported Dict type: " .. type(t))
+ end
end
for i, entry in ipairs(Dict({
x = 99