Created
August 14, 2012 20:55
-
-
Save balaam/3352959 to your computer and use it in GitHub Desktop.
Get all combinations of items in a table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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