Power set: Difference between revisions

Line 1,540:
=={{header|JavaScript}}==
 
===ImperativeES5===
====Iteration====
 
Uses a JSON stringifier from http://www.json.org/js.html
 
Line 1,563:
<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)composition====
 
{{trans|Haskell}}
Line 1,596 ⟶ 1,597:
"set which contains only the empty set ->":[[], [[]]]
}</lang>
 
===ES6===
 
<lang JavaScript>(() => {
'use strict';
 
// powerset :: [a] -> [[a]]
const powerset = xs =>
xs.reduceRight((a, x) => a.concat(a.map(y => [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