Intermediate Level

HI

I am trying examples on this platform

But i dont understand the solution of this problem. I know how concat, map and reduce methods work but still i dont understand the logic of the solution. How can i improve the understanding to next level?

practice doing algorithms

break down that line

etc

many things you can do

const powerset = arr => arr.reduce((a, v) => a.concat(a.map(r => [v].concat(r))), [[]]);

to start I would break this donw in loggable steps
so

function powerset (arr) {
  return arr.reduce((a, v) => a.concat(a.map(r => [v].concat(r))), [[]]);
}

and then again

function reduceCallback(a, v) {
  return a.concat(a.map(r => [v].concat(r)))
}
function powerset (arr) {
  return arr.reduce(reduceCallback, [[]]);
}

and again

function reduceCallback(a, v) {
  function mapCallback(r) {
    return [v].concat(r)
  }
  return a.concat(a.map(mapCallback))
}

function powerset(arr) {
  return arr.reduce(reduceCallback, [
    []
  ]);
}
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.