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

Essential vector methods missing? #195

Open
tszekely-spotcap opened this issue Oct 27, 2016 · 1 comment
Open

Essential vector methods missing? #195

tszekely-spotcap opened this issue Oct 27, 2016 · 1 comment

Comments

@tszekely-spotcap
Copy link

tszekely-spotcap commented Oct 27, 2016

Hello, I've started using mori on an experimental project a few weeks ago and all was good until I wanted to do some updates on a vector.

1 . The first thing I tried to do was to update an object from the vector, based on its ID
Since there is no update method for vectors, I'd naturally do something like:

  • find the element's index
  • update/replace the element at index

...but there is no "findIndex" method. the closest thing to a real find method is "some", but it doesn't give you the index, just the value.

I ended up doing:

files: mori.map(
        file => (
          file.getId() === fileInfo.id ?
            file.setInfo(fileInfo) :
            file
        ),
        state.files
      )

but this seems kind of inefficient.

2 . Add some elements to the vector, replacing the existing ones with the same ID (but maybe different values in other properties)

This was very tedious, and I ended up converting the vector to array, doing the operations and then converting the resulting array back to vector. Again, inefficient.

I tried using a set, but both elements with same ID ended up in the result and there's no way of defining the equality check predicate.

Am I missing something here or mori's missing some pretty important things?

@kisai
Copy link

kisai commented Oct 27, 2016

You do that with the updateIn function.

this is the function signature
mori.updateIn(coll, keys, function)

and this is the example from the documentation
var h = mori.hashMap("foo", mori.vector(1, 2, 3)); mori.updateIn(h, ["foo", 1], mori.inc); // => {"foo" [1 3 3]}

the second parameter is an array of the keys, you can use this for nested collections as well as for simple collections.

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