Pretty, don’t you think?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
write a function that returns all subsets of a given array
O(2^n)
*/

function subsets(array) {
if (array.length === 0) return [[]]
else {
let [first, ...rest] = array
let restSubsets = subsets(rest)
return [
...restSubsets,
...restSubsets.map(subset => [first, ...subset])
]
}
}