Add iterator table / list / set comprehensions

This commit is contained in:
Lars Mueller 2022-09-20 20:02:32 +02:00
parent ffba199b96
commit 14e86618d0

@ -109,4 +109,33 @@ function iterator.standard_deviation(...)
return (sum / count)^.5
end
-- Comprehensions ("collectors")
-- Shorthand for `for k, v in ... do t[k] = v end`
function iterator.to_table(...)
local t = {}
for k, v in ... do
t[k] = v
end
return t
end
-- Shorthand for `for k in ... do t[#t + 1] = k end`
function iterator.to_list(...)
local t = {}
for k in ... do
t[#t + 1] = k
end
return t
end
-- Shorthand for `for k in ... do t[k] = true end`
function iterator.to_set(...)
local t = {}
for k in ... do
t[k] = true
end
return t
end
return iterator