mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-25 16:53:46 +01:00
Add iterator table / list / set comprehensions
This commit is contained in:
parent
ffba199b96
commit
14e86618d0
29
iterator.lua
29
iterator.lua
@ -109,4 +109,33 @@ function iterator.standard_deviation(...)
|
|||||||
return (sum / count)^.5
|
return (sum / count)^.5
|
||||||
end
|
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
|
return iterator
|
Loading…
Reference in New Issue
Block a user