From: Simon Désaulniers Date: Wed, 14 Feb 2018 22:00:36 +0000 (-0500) Subject: helpers: powerset and trivial_partition_set X-Git-Url: https://git.madduck.net/etc/awesome.git/commitdiff_plain/2bb4741bb4c5a97ebadf1eceafe0d5e2dae1fd29?ds=inline;hp=--cc helpers: powerset and trivial_partition_set - the powerset is the set of all combinations of a given set; - the trivial partition set is the simplest partition of a set. For e.g., the trivial partition set of {a, b, c}, is simply {{a}, {b}, {c}}. --- 2bb4741bb4c5a97ebadf1eceafe0d5e2dae1fd29 diff --git a/helpers.lua b/helpers.lua index ab87f76..ce65576 100644 --- a/helpers.lua +++ b/helpers.lua @@ -170,6 +170,29 @@ function helpers.spairs(t) end end +-- create trivial partition of a set. The trivial partition set is the simplest +-- partition of a set. For e.g., the trivial partition set of {a, b, c}, is +-- simply {{a}, {b}, {c}}. +function helpers.trivial_partition_set(set) + local ss = {} + for _,e in pairs(set) do + ss[#ss+1] = {e} + end + return ss +end + +-- creates the powerset of a given set +function helpers.powerset(s) + if not s then return {} end + local t = {{}} + for i = 1, #s do + for j = 1, #t do + t[#t+1] = {s[i],unpack(t[j])} + end + end + return t +end + -- }}} return helpers