Skip to content

Commit

Permalink
Added table.shuffle
Browse files Browse the repository at this point in the history
  • Loading branch information
Otto Robba committed Nov 30, 2016
1 parent 0319737 commit 3943a20
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lua/extra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ function table.count(t)
return count
end

function table.shuffle(t)
if type(t) ~= "table" then
error("table expected, but got a "..type(t), 2)
end

for a = #t, 2, -1 do
local b = math.random(a)
t[a], t[b] = t[b], t[a]
end
end

local
function table_tostring(t, indent, seen, depth)
seen = seen or {}
Expand Down Expand Up @@ -154,7 +165,7 @@ function table_tostring(t, indent, seen, depth)
str = str .. prefix .. tab .. table_tostring(t[i], indent + 1, seen, depth + 1) .. ",\n"
end
else
table.sort(keys, function(k1, k2)
table.sort(keys, function(k1, k2)
local t1 = type(k1)
local t2 = type(k2)
if t1 == "string" and t2 == "string" then
Expand Down

0 comments on commit 3943a20

Please sign in to comment.