Skip to content

Instantly share code, notes, and snippets.

@orther
Last active February 18, 2017 16:26
Show Gist options
  • Save orther/cdbb839bf001e18f909c409f8000b1a3 to your computer and use it in GitHub Desktop.
Save orther/cdbb839bf001e18f909c409f8000b1a3 to your computer and use it in GitHub Desktop.
const contains = (coll) => (item) => coll.includes(item);
const intersection = (a, b) => a.filter(contains(b));
const uniq = (list) => [...new Set(list)];
const intersectionAll = ([head, ...tail]) => uniq(tail.reduce(intersection, head))
const all = [
[5, 10, 15, 20, 6],
[15, 88, 1, 5, 7, 6],
[1, 10, 15, 5, 20, 6],
[8, 10, 15, 5, 20, 6],
[8, 10, 15, 5, 20], // removed 6
[8, 10, 15, 5, 20, 6],
];
console.log(intersection(all[0],all[1])) // => [5, 15, 6]
console.log(intersectionAll(all)); // => [5, 15]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment