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

Feature request: get-and-delete operation for SortedMap #5

Open
abonander opened this issue Mar 6, 2018 · 3 comments
Open

Feature request: get-and-delete operation for SortedMap #5

abonander opened this issue Mar 6, 2018 · 3 comments

Comments

@abonander
Copy link
Contributor

I just today realized that Immutable.js doesn't provide an operation for this, though I really only need it for SortedMap. I would like a method on SortedMap that removes and returns the value under the given key as well as the updated map. In Flow the function prototype is straightforward:

getAndDelete(key: K): [?V, SortedMap<K, V>] {}

let [val, map] = map.getAndDelete('key');

Apparently in Immutable.js this can be done by returning null from the closure passed to Collection.update() (though this is undocumented) but SortedMap doesn't appear to support this; it just updates the value as null. I know this can be done with get() then delete() but I'd like to avoid the redundant lookup if possible.

@applitopia
Copy link
Owner

Thank you for your suggestion, this would would definitely be more efficient than two separate calls.
It would be nice to have it implemented across all the collections (List, Set, Map, OrderedSet, OrderedMap, SortedSet, SortedMap)

Please note, the get function includes optional notSetValue argument to distinguish the missing key in the map from the key having unusual values like null, undefined, {}, etc.:

get<NSV>(key: K, notSetValue: NSV): V | NSV;

Would it make sense to include this additional argument into getAndDelete()?

@abonander
Copy link
Contributor Author

abonander commented Mar 6, 2018

Would it make sense to include this additional argument into getAndDelete()?

It certainly wouldn't hurt though returning any falsey value is sufficient for my use-case. I'm not sure how default parameters interact with type parameters, though; is this valid syntax?

getAndDelete<NSV = ?V>(key: K, notSetValue: NSV = null): V | NSV {}

@abonander
Copy link
Contributor Author

As for merging it upstream, I figure it would be easier to prototype here first before proposing it for Immutable.js. Implementing it on SortedMap would be enough for me either way.

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

No branches or pull requests

2 participants