Skip to content

Instantly share code, notes, and snippets.

@koozdra
Created November 8, 2017 22:44
Show Gist options
  • Save koozdra/756ff09e5a3d8c33a6bc9de7b8abf500 to your computer and use it in GitHub Desktop.
Save koozdra/756ff09e5a3d8c33a6bc9de7b8abf500 to your computer and use it in GitHub Desktop.
Javascript powerset function
const _ = require('lodash');
function supersetInner(src) {
if (src.length === 0) return [];
const withouts = _.map(src, elem => superset(_.without(src, elem)));
return [src, ..._.flatten(withouts)];
}
function superset(src) {
return _.uniqWith(supersetInner(src), _.isEqual).sort();
}
console.log(JSON.stringify(superset([1, 2, 3, 4])));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment