arrays - How to write a function that compute differences between two Map object in javascript -
i tried modify function collecting array/map difference here handle multi-dimensional array, code not working expected... code
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;//it 1 working } // symmetric difference const difference = yx => xs => concat(differencel(xs)(yx)(flip(differencel)(xs)(yx)); return difference; //this 1 returning function definition } 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')); //this dump source code console
i can't debug code again has become more complex can handle, please need function can efficiently reason why not using traditional array... performance point of coming here... in advance
it's not clear me want, assuming in example 2 arrays xs
, ys
, diff (left) running expecting difference same xs
in case? here example "diff left", in speak. using map
not going work thinking because pairs
may have same content different object references , not compare (they different object references).
const diffleft = (a, b) => { return a.filter(x => { const [c, d] = x; const index = b.findindex(y => { const [u, v] = y; return c === u && d === v; }); return index < 0; }); }; const x = [ ['a', 1], ['b', 2], ['c', 2], ['d', 3], ['e', 4], ['f', 5] ]; const y = [ ['g', 0], ['h', 1], ['f', 2], ['b', 3], ['c', 3], ['a', 3], ['e', 6], ['h', 7] ]; console.log(diffleft(x, y));
that code reduces down single line, it's not digestible (as looks has been run through minimiser). of problem code have posted.
const diffleft = (a, b) => a.filter(([c, d]) => b.findindex(([u, v]) => c === u && d === v) < 0); const x = [ ['a', 1], ['b', 2], ['c', 2], ['d', 3], ['e', 4], ['f', 5] ]; const y = [ ['g', 0], ['h', 1], ['f', 2], ['b', 3], ['c', 3], ['a', 3], ['e', 6], ['h', 7] ]; console.log(diffleft(x, y));
Comments
Post a Comment