hughfdjackson / immutable

neatly packages immutable equivalents to JavaScript's Objects and Arrays.
101 stars 7 forks source link

immutable arrays: Unexpected resutls in trivial test #34

Open djcoin opened 11 years ago

djcoin commented 11 years ago

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 :

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 ])) )

pk11 commented 10 years ago

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 ]