0

I modify a function for collecting array difference here to handle multi-dimensional array, the code is not working as expected and debugging it has been very problematic because console.log is just displaying content of the code instead of executing it...

function arrDiff(xs, yx, d) {
  const apply = f => x => f(x);
  const flip = f => y => x => f(x)(y);
  const concat = y => xs => xs.concat(y);
  const createMap = xs => new Map(xs);
  const filter = f => xs => xs.filter(apply(f));

  //left difference
  const differencel = xs => yx => {
    const zs = createMap(yx);
    return filter(([k, x]) => zs.has(x) ? false : true)(xs);
  };
  if (d == 'left') return differencel
  //right difference
  const difference2 = flip(differencel);
  if (d == 'join') return difference2;

  //union
  if (d == "union") {
    const map = new Map([...xs, ...yx]);
    return map;
  }
  // symmetric difference
  const difference = yx => xs => concat(differencel(xs)(yx))
    (flip(differencel)(xs)(yx));
  return difference;
}
const xs = [
  ['a', 1],
  ['b', 2],
  ['c', 2],
  ['d', 3],
  ['e', 4],
  ['f', 5]
];
const ys = [
  ['g', 0],
  ['h', 1],
  ['f', 2],
  ['b', 3],
  ['c', 3],
  ['a', 3],
  ['e', 6],
  ['h', 7]
];
console.log(arrDiff(xs, ys, 'left'));
Community
  • 1
  • 1
Aderemi Dayo
  • 705
  • 1
  • 11
  • 25
  • 3
    `difference` is a function. You're just returning the function, not calling it. – Barmar Apr 14 '17 at 14:57
  • 1
    I don't see the point to store functions like `new Map()`, `concat()` or `filter()` inside new variables. Anyways - are you sure that you want to declare them as currying functions? – kind user Apr 14 '17 at 14:58
  • `difference` ends up getting assigned a function rather than a result, it looks like `difference1` in the end. I can't quite unravel exactly how, but partial application isn't my thing. Somewhere along the line, you've missed out calling it. Some of those are double-level partial applications at which point I lose interest. :-) – T.J. Crowder Apr 14 '17 at 15:14

0 Answers0