Skip to content

Instantly share code, notes, and snippets.

@hiroshi-maybe
Created March 11, 2013 02:18
Show Gist options
  • Select an option

  • Save hiroshi-maybe/5131502 to your computer and use it in GitHub Desktop.

Select an option

Save hiroshi-maybe/5131502 to your computer and use it in GitHub Desktop.
Power set generator.
var power_set_generator = function(set) {
var _index=0, _set=set, _max_size= 1<<set.length;
return Object.create(Object.prototype, {
next : { value : function() {
var result = [], n=_index,i=0;
if (_index >= _max_size) return;
for (; n; n>>=1, i+=1) if (n & 1) result.push(_set[i]);
_index+=1;
return result;
}}
});
};
// var generator = power_set_generator(["a","b","c"]);
/*
while (val = generator.next()) {
console.log(val);
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment