Skip to content

intersection

Subhajit Sahu edited this page Jun 15, 2020 · 26 revisions

Gives values present in both iterables. 🏃 📼 📦 🌔 📒

Similar: union, intersection, difference, symmetricDifference.

iterable.intersection(x, y, [fc], [fm]);
// x:  an iterable
// y:  another iterable
// fc: compare function (a, b)
// fm: map function (v, i, x)

⏱️ Compare function => O(n²).

const iterable = require('extra-iterable');

var x = [1, 2, 3, 4];
var y = [2, 3, 5];
[...iterable.intersection(x, y)];
// [ 2, 3 ]

var y = [-2, -3, -5];
[...iterable.intersection(x, y, (a, b) => Math.abs(a) - Math.abs(b))];
// [ 2, 3 ]

[...iterable.intersection(x, y, null, v => Math.abs(v))];
// [ 2, 3 ]

references

Clone this wiki locally