Skip to content
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 arrays: Unexpected resutls in trivial test #34

Open
djcoin opened this issue Jun 26, 2013 · 1 comment
Open

immutable arrays: Unexpected resutls in trivial test #34

djcoin opened this issue Jun 26, 2013 · 1 comment

Comments

@djcoin
Copy link

djcoin commented Jun 26, 2013

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

@pk11
Copy link

pk11 commented Dec 28, 2013

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 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants