Skip to content

Instantly share code, notes, and snippets.

@lukesh
Created December 20, 2012 01:22
Show Gist options
  • Save lukesh/4342266 to your computer and use it in GitHub Desktop.
Save lukesh/4342266 to your computer and use it in GitHub Desktop.
Overloading _.intersection
var arr1 = [1,2,3]
,arr2 = [2,3,4]
,arr3 = [3,4,5];
function overloadIntersection() {
var cached = _.intersection;
_.intersection = function() {
var rest = arguments, arr = [];
if(rest.length == 0) {
throw "You must pass at least one array to _.intersection."
}
arr = rest.length == 1 ? rest[0] : rest;
return eval(_.reduce(arr, function(m, v, i) { return m + 'arr['+i+'],'; }, 'cached(').slice(0, -1) + ')');
}
}
overloadIntersection();
console.log(_.intersection([arr1, arr2, arr3]));
console.log(_.intersection(arr1, arr2, arr3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment