Skip to content

Instantly share code, notes, and snippets.

@balaam
Created August 14, 2012 20:55
Show Gist options
  • Select an option

  • Save balaam/3352959 to your computer and use it in GitHub Desktop.

Select an option

Save balaam/3352959 to your computer and use it in GitHub Desktop.
Get all combinations of items in a table
function AllCombinations(list, stop)
stop = stop or #list
if stop == 0 then
return list
end
local collect = {}
for k, v in pairs(list) do
local results = AllCombinations(list, stop - 1)
for i, j in pairs(results) do
if type(j) ~= "table" then
j = {j}
end
table.insert(j, v)
table.insert(collect, j)
end
end
return collect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment