Skip to content

Instantly share code, notes, and snippets.

@gerhardberger
Created August 31, 2012 11:44
Show Gist options
  • Save gerhardberger/3551793 to your computer and use it in GitHub Desktop.
Save gerhardberger/3551793 to your computer and use it in GitHub Desktop.
High-order merge function.
// High-order merge function.
// Dependency: underscore.js [http://underscorejs.org]
// Demo: http://jsfiddle.net/7HB8J/
// First argument: Function (returns a Boolean, that decides, wether it is the same (hence can be overwritten))
// Rest: Array(s)
merge = function (f) {
var as = _.flatten(Array.prototype.slice.call(arguments, 1)).reverse().map(function (a) { return {v: a, uniq: 0}; });
return as.map(function (a) {
if (a.uniq === 1) return a;
else {
a.uniq = 2;
for (var i in as) if ((as[i].uniq === 0) && (f(a.v, as[i].v))) as[i].uniq = 1;
return a;
}
}).filter(function (a) { return a.uniq === 2; }).map(function (a) { return a.v; });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment