immutable
immutable copied to clipboard
immutable arrays: Unexpected resutls in trivial test
First, thanks for your library !
Well, then i'm having a hard time using arrays. Here are so crucial points that I think I did not quite get how immutable arrays are supposed to work :
-
array's length the array length is not correct, eg:
im.array([1, 2]).dissoc(1).length => 2 im.array([1, 2]).dissoc(1).toJSON() => [ 1 ]
And "im.array([1, 2]).dissoc(0).get(0)" does not work (undefined). So in my code for example I'm not able to pick values one by one until the array is finished as I don't know how to check for emptyness.
-
dissoc array.dissoc leaves a "hole", eg:
im.array([1, 2, 3, 4, 5]).dissoc(2).toJSON() => [ 1, 2, , 4, 5 ] ) ( leading to ~ same behaviour as array.assoc(2, null) which would be ok. ) To me but I may be mistaken, creating an array and then dissoc an elem at index i should be the same as creating from the start an array without this elem at index i.
Thanks for any insights
A bunch of test
Using a simple immutable array [ 5, 6 ]:
JSON.stringify(im.array([ 5, 6 ])) => '[5,6]' ok
JSON.stringify(im.array([ 5, 6 ]).dissoc(1)) => '[5]' ok
JSON.stringify( (im.array([ 5, 6 ]).dissoc(0)).get(0) ) => undefined I would have expected 6 ( im.array(6).get(0) )
JSON.stringify( (im.array([ 5, 6 ]).dissoc(0)).get(1) ) => '6' Would have expected undefined ( im.array(6).get(1) )
JSON.stringify( (im.array([ 5, 6 ]).dissoc(1)).get(0) ) => '5' ok
JSON.stringify( (im.array([ 5, 6 ]).dissoc(1)).get(1) ) => undefined ok
JSON.stringify(im.array([ 5, 6 ]).dissoc(0)) => '[null,6]' I would have expected '[ 6 ]' ( JSON.stringify(im.array([ 6 ])) )
this behavior is actually consistent with mutable Array.delete
. You could filter out undefined
elements for the cost of O(n) like:
im.array([1,2,3]).dissoc(0).filter(function(e) {return e != undefined}).toJSON(); // [ 2, 3 ]