immer icon indicating copy to clipboard operation
immer copied to clipboard

Add iterator version for map::find()

Open arximboldi opened this issue 5 years ago • 6 comments

arximboldi avatar Aug 14 '19 18:08 arximboldi

I've got code working for this, but I'm struggling to come up with a name for the iterator based method. Any suggestions ?

basicer avatar Oct 26 '21 20:10 basicer

Would you like me to look at the code? Maybe find() is ok. Or lookup(). Or we can rename the current one as lookup(). Note that the current version, I believe, covers most cases of find() with lower overhead.

arximboldi avatar Oct 27 '21 00:10 arximboldi

Sorry for the delay getting back to you, it took a bit longer than I thought to get approval from work to share the patch.

https://github.com/basicer/immer/commit/0dcde00eaea6e03909eed4c33c81705798181898

I agree the current version is better for most use-cases. The situation I was running up against is an API where I return some pairs in the map to the caller, limited to some number. The caller can then make another request for the next page by providing the last key they saw. Without this, I either had to make a side lookup table of page ending keys to iterators, or reiterate the entire map to get back to the beginning of the next page.

basicer avatar Dec 07 '21 20:12 basicer

Thanks @basicer! Maybe I will add a version of this based on that at some point. I'm not so sure about the usefulness of beginOrAfter(), whats your use-case for that?

Also, yous aid you needed approval, are you using Immer at your work at Roblox? :smiley:

arximboldi avatar Dec 08 '21 09:12 arximboldi

We are considering it!

The use case for beginAtOrAfter() is that if the key your looking for is from an old version of the map, you can still iterate. It doesn't exactly work though, because if the key you provide that doesn't exist happens to be in between two entries on the collision list, there's no way to know where that key would have been in the collision list.

basicer avatar Dec 08 '21 21:12 basicer

Ah, interesting... good luck in your experiments with it, I hope it ends up being useful!

arximboldi avatar Dec 22 '21 09:12 arximboldi