Modul:Iterators
Dokumentaci tohoto modulu lze vytvořit na stránce Nápověda:Modul:Iterators
-- @brief
-- Various iterators.
--
-- @author
-- [[meta:User:Danny B.]]
local _module = {}
----------------------------------------
-- @brief
-- Iterator for table sorted by its keys.
--
-- @param
-- tbl Table to be traversed
-- sorter nil | Sorting function
--
-- @return
-- Iterator Table sorted by its keys
function _module.kpairs( tbl, sorter )
local keys = {}
for key in pairs( tbl ) do
table.insert( keys, key )
end
table.sort( keys, sorter )
local i = 0
local iterator = function ()
i = i + 1
if keys[i] == nil then
return nil
else
return keys[i], tbl[keys[i]]
end
end
return iterator
end
----------------------------------------
return _module