-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Immutable version #18
Comments
Hi. Yes, the vectors are mutable on purpose. The idea behind it is that,.. let's say you have a space ship that moves. So the ship has a position vector and a and a velocity vector. To move the ship you add the velocity vector to the position vector: var ship = {
position: new Victor(100, 200),
velocity: new Victor(1, 0),
move: function () {
this.position.add(this.velocity);
}
};
setInterval(function () {
ship.move();
draw();
}, 1000 / 16); instead of doing: // let's assume Victor is immutable
var ship = {
...
move: function () {
this.position = this.position.add(this.velocity);
}
};
... Do you think this is a bad idea? You can use the var ship = {
...
move: function () {
// add only half the velocity without modifying the velocity
this.position.add(this.velocity.clone().divide(new Victor(2, 2)));
}
};
... What are your thoughts? |
Sure, I'm not saying mutability is inherently bad. My scenario where I prefer things to be immutable is more math style: var a = Vector(23, 12);
var b = Vector(25, 13);
var c = a.normalize().add(b.normalize()); to calculate angle bisector. With mutable stuff I need var a = Vector(23, 12);
var b = Vector(25, 13);
var c = a.clone().normalize().add(b.clone().normalize()); With more calculations it's |
I would also prefer if the methods such as |
I made a Immutable vector library https://github.com/yukulele/Vector.js |
i agree to have an immutable version. this just makes things much more simpler to reason about. this is extremely useful when you want to simulate something. immutability allows to travel back to the time. |
+1 for immutability feature. Will be awesome now that functional Javascript is becoming more and more popular. see Redux and co. |
Gah, just been bitten by this. I was really surprised that operation are mutable. Should clearly mention this on the website |
They are mutable, because of the ability to chain. It would be nice to state that the library is not immutable on the If you check out the docs e.g. the
My opinionI will side with the author and say that this: this.position.add(this.velocity); Is easier to read than: this.position = this.position.clone().add(this.velocity); // or
this.position = Victor.fromObject(this.position).add(this.velocity); |
The ability to chain has nothing to do with mutability, it's just a matter of whether the methods return let a = new Victor(1, 2);
let b = new Victor(3, 4);
let ret = new Victor(0, 0);
a.add(b, ret);
// ret has been modified by the add method Though I think in C, the ret value is generally uninitialized, which we can't do in JavaScript due to the lack of pointers, but objects are shared by reference, so Totally agree with your opinion examples. Would be nice if JavaScript supported operator overloading, then you could do |
It was very annoying for me that methods like
add
ordivide
modified the vector. My temporary solution is my immutable-vector2d library. Do you have any ideas as how to merge my project into this one?The text was updated successfully, but these errors were encountered: