1
0
Fork 0

Better `has_uniform_structure`.

This commit is contained in:
Jon Michael Aanes 2017-10-22 12:29:43 +02:00
parent ef0a4c82ed
commit a9d4d35fc1
1 changed files with 5 additions and 1 deletions

View File

@ -209,13 +209,17 @@ local function has_uniform_structure (t)
-- Find the key and value types.
local first_key = next(t)
if first_key == nil then return true end
local key_type, value_type = type(first_key), type(t[first_key])
local key_type, value_type = type(first_key), type(t[first_key])
local nr_elems = (value_type == 'table') and nr_elements_in_table(t[first_key]) or nil
-- Ensure every other key value pair is the same.
for key, value in pairs(t) do
if type(key) ~= key_type or type(value) ~= value_type then
return false
end
if nr_elems and nr_elems ~= nr_elements_in_table(value) then
return false
end
end
return true