I am currently learning functional programming using Underscore.js. What I want to do is add two co-ordinates together. Let P1 = (0, 0) and P2 = (1, 1). Therefore, P1 + P2 = (1, 1)
I have a solution, but it seems so convoluted that I'm sure I'm doing something wrong:
// Base 2D Cartesian Point
var Point = function(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.add = function(delta) {
return _.object(_.keys(this),
_.map(
_.zip(
_.values(this),
_.values(delta)),
function(point) {
return _.reduce(point,
function(memo, num) { return memo + num; }, 0);
}
)
);
}
Is this the proper implementation of the add function for points using Underscore.js or is there a better solution?