Created
August 31, 2012 11:44
-
-
Save gerhardberger/3551793 to your computer and use it in GitHub Desktop.
High-order merge function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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