immutable-js / immutable-js

Immutable persistent data collections for Javascript which increase efficiency and simplicity.
https://immutable-js.com
MIT License
32.92k stars 1.78k forks source link

Having trouble combining Vector::withMutations with Vector::splice #37

Closed nathansobo closed 10 years ago

nathansobo commented 10 years ago

Should the following code example log true? On 2.0.4 it's logging false for me. Am I doing something wrong?

vector = Immutable.Vector(1, 2, 3)
a = vector.splice(1, 1, 'a', 'b')
b = vector.withMutations (v) -> v.splice(1, 1, 'a', 'b')
console.log Immutable.is(a, b)
leebyron commented 10 years ago

This is a bit subtle, but splice is not available as a mutative method. It always returns a sequence. Because splice needs to push the indices of the elements after it, it always needs to create a new vector.

I agree that this behavior isn't super obvious though. I think the code you wrote should have done what you expected. Unfortunately right now it doesn't. The answer is to (outside of withMutations) do vect.splice(...).toVector()

leebyron commented 10 years ago

This behavior is also made clearer by RRB, so now tracking this in #38