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

Add hint on how to filter OrderedMaps to README #38

Open
WilliamFrei opened this issue Apr 8, 2024 · 0 comments
Open

Add hint on how to filter OrderedMaps to README #38

WilliamFrei opened this issue Apr 8, 2024 · 0 comments

Comments

@WilliamFrei
Copy link

WilliamFrei commented Apr 8, 2024

To filter elements from the OrderedMap, the most obvious approach would be the following:

for pair := om.Oldest(); pair != nil; pair = pair.Next() {
	if Predicate(pair.Value) {
		om.Delete(pair.Key)
	}
}

This does not work: It will only delete the first match.

It is also not possible to fix this in this repository, as the cause for this behaviour lies in the list implementation used.

The following code works:

for pair := om.Oldest(); pair != nil; {
	key, value := pair.Key, pair.Value
	pair = pair.Next()
	if Predicate(value) {
		om.Delete(key)
	}
}

The same problem occurs if the iteration order is reversed, and is also independent of the Predicate parameters (e.g. removing the first 10 or all elements with the first pattern will not work).

I suggest adding a hint on how to filter the OrderedMap with the second snippet to the README.

I can also create a PR if you do not have the time.

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

1 participant