Wednesday, July 28, 2021

javascript get all subsets of an array, then sort subsets by length, and preserve order

This stackoverflow  had an elegant all permutations generator:

const getAllSubsets = 
      theArray => theArray.reduce(
        (subsets, value) => subsets.concat( => [value,...set])
const elems = [1,2,3];

I can't say I really understand how it works! But here is the output:


I realized I would like it better if we sorted by length... the empty set, each single entry set, each 2 entry set... 

          .sort((a,b)=>a.length - b.length)

That made


Much better! But still, it would be cool if each individual array was sorted - not on the value but based on the order in the original set:

So I end up making

          .sort((a,b)=>a.length - b.length)
          .map(arr=>arr.sort((a,b)=>elems.indexOf(a) - elems.indexOf(b)  ))

That's cool! let me change elems to ["Foo", "Bar", "Baz"]:



UPDATE: the Typescript version that function is

const getAllSubsets = (theArray:string[]) => theArray.reduce(
        (subsets, value) => subsets.concat(
 => [value,...set])
        ), [[] as string[]]

No comments:

Post a Comment