Power set: Difference between revisions

No edit summary
Line 1,412:
 
=={{header|JavaScript}}==
 
===Imperative===
 
Uses a JSON stringifier from http://www.json.org/js.html
 
Line 1,432 ⟶ 1,435:
{{out}}
<pre>[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3],[4],[1,4],[2,4],[1,2,4],[3,4],[1,3,4],[2,3,4],[1,2,3,4]]</pre>
 
===Functional (ES 5)===
 
<lang JavaScript>(function () {
 
 
function powerset(xs) {
return xs.reduceRight(function (a, x) {
return a.concat(a.map(function (y) {
return [x].concat(y);
}));
}, [[]]);
}
 
 
 
// TEST
return {
'[1,2,3] ->': powerset([1, 2, 3]),
'empty set ->': powerset([]),
'set which contains only the empty set ->': powerset([[]])
}
 
})();</lang>
 
{{Out}}
 
<lang JavaScript>{
"[1,2,3] ->":[[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]],
"empty set ->":[[]],
"set which contains only the empty set ->":[[], [[]]]
}</lang>
 
=={{header|jq}}==
9,659

edits