use bind for partial application of power set function

This commit is contained in:
d98762625 2018-03-25 17:27:14 +01:00
parent e8bb9e264d
commit 951568ce22
1 changed files with 31 additions and 26 deletions

View File

@ -70,7 +70,7 @@ class SetOps {
"Set Difference": this.runSetDifference, "Set Difference": this.runSetDifference,
"Symmetric Difference": this.runSymmetricDifference, "Symmetric Difference": this.runSymmetricDifference,
"Cartesian Product": this.runCartesianProduct, "Cartesian Product": this.runCartesianProduct,
"Power Set": this.runPowerSet(itemDelimiter), "Power Set": this.runPowerSet.bind(undefined, itemDelimiter),
}[operation] }[operation]
.apply(this, sets.map(s => s.split(itemDelimiter))); .apply(this, sets.map(s => s.split(itemDelimiter)));
@ -113,6 +113,7 @@ class SetOps {
/** /**
* Get the intersection of the two sets. * Get the intersection of the two sets.
*
* @param {Object[]} a * @param {Object[]} a
* @param {Object[]} b * @param {Object[]} b
* @returns {Object[]} * @returns {Object[]}
@ -125,6 +126,7 @@ class SetOps {
/** /**
* Get elements in set a that are not in set b * Get elements in set a that are not in set b
*
* @param {Object[]} a * @param {Object[]} a
* @param {Object[]} b * @param {Object[]} b
* @returns {Object[]} * @returns {Object[]}
@ -137,6 +139,7 @@ class SetOps {
/** /**
* Get elements of each set that aren't in the other set. * Get elements of each set that aren't in the other set.
*
* @param {Object[]} a * @param {Object[]} a
* @param {Object[]} b * @param {Object[]} b
* @return {Object[]} * @return {Object[]}
@ -147,6 +150,7 @@ class SetOps {
} }
/** /**
* Return the cartesian product of the two inputted sets.
* *
* @param {Object[]} a * @param {Object[]} a
* @param {Object[]} b * @param {Object[]} b
@ -159,38 +163,39 @@ class SetOps {
} }
/** /**
* Return the power set of the inputted set.
* *
* @param {Object[]} a * @param {Object[]} a
* @returns {Object[]} * @returns {Object[]}
*/ */
runPowerSet(delimiter) { runPowerSet(delimiter, a) {
return function(a) { // empty array items getting picked up
a = a.filter(i => i.length);
if (!a.length) {
return [];
}
// empty array items getting picked up /**
a = a.filter(i => i.length); * Decimal to binary function
if (!a.length) { * @param {*} dec
return []; */
} const toBinary = (dec) => (dec >>> 0).toString(2);
const result = new Set();
// Get the decimal number to make a binary as long as the input
const maxBinaryValue = parseInt(Number(a.map(i => "1").reduce((p, c) => p + c)), 2);
// Make an array of each binary number from 0 to maximum
const binaries = [...Array(maxBinaryValue + 1).keys()]
.map(toBinary)
.map(i => i.padStart(toBinary(maxBinaryValue).length, "0"));
/** // XOR the input with each binary to get each unique permutation
* binaries.forEach((binary) => {
* @param {*} dec const split = binary.split("");
*/ result.add(a.filter((item, index) => split[index] === "1"));
const toBinary = (dec) => (dec >>> 0).toString(2); });
const result = new Set();
const maxBinaryValue = parseInt(Number(a.map(i => "1").reduce((p, c) => p + c)), 2);
const binaries = [...Array(maxBinaryValue + 1).keys()]
.map(toBinary)
.map(i => i.padStart(toBinary(maxBinaryValue).length, "0"));
binaries.forEach((binary) => { // map for formatting & put in length order.
const split = binary.split(""); return [...result].map(r => r.join(delimiter)).sort((a, b) => a.length - b.length);
result.add(a.filter((item, index) => split[index] === "1"));
});
// map for formatting & put in length order.
return [...result].map(r => r.join(delimiter)).sort((a, b) => a.length - b.length);
};
} }
} }