Created
November 8, 2017 22:44
-
-
Save koozdra/756ff09e5a3d8c33a6bc9de7b8abf500 to your computer and use it in GitHub Desktop.
Javascript powerset function
This file contains 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
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