Skip to content

Instantly share code, notes, and snippets.

@balaam
Created August 14, 2012 21:01
Show Gist options
  • Select an option

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

Select an option

Save balaam/3353022 to your computer and use it in GitHub Desktop.
Get all combinations and all sub combinations in a table (I'm sure there's a term for this!)
function AllCombinations(list, stop)
stop = stop or #list
if stop == 0 then
return list
end
local collect = {}
for k, v in pairs(list) do
if type(v) ~= "table" then
table.insert(collect, {v})
else
table.insert(collect, v)
end
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