Something like
pythonic_map = function(fun) {
var args = [].slice.call(arguments, 1)
return args[0].map(function(_, i) {
return fun.apply(null, args.map(function(x) { return x[i] }));
});
}
Example:
function add(a, b, c) {
return a + b + c
}
z = pythonic_map(add, [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12])
console.log(z) // [15,18,21,24]
This uses the length of the first argument, to use the shortest/longest argument:
pythonic_map = function(fun) {
var args = [].slice.call(arguments, 1);
return args.reduce(function(m, x) {
return (x.length < m.length) ? x : m; // or > for the longest
}).map(function(_, i) {
return fun.apply(null, args.map(function(x) { return x[i] }));
});
}
Array#reducemay be best here. Something like[[1,2,3,4],[5,6,7,8],[9,10,11,12]].reduce(function(a,b) {return b.map(function(_,i) {return (a[i]||0)+b[i];});},[]);should do it.map, butzipWith sumwhat you are looking formapin clojure =)