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: pop_if() #60

Open
garro95 opened this issue Feb 20, 2025 · 0 comments
Open

Feature request: pop_if() #60

garro95 opened this issue Feb 20, 2025 · 0 comments
Assignees

Comments

@garro95
Copy link
Owner

garro95 commented Feb 20, 2025

Please add a new method pop_if() that removes and returns the "top" element from the queue as a Some((item, priority)), but only if the "top" element matches the given predicate. Otherwise, a None should be returned.

The rationale is that currently we need to write a code like this:

while queue.peek().is_some_and(|(item, priority)| some_condition(...)) {
    (item, priority) = queue.pop().unwrap();
    /* ... */
}

But the unwrap() is kind of ugly, because it shouldn't actually be needed. That is because, at this point, we already know for sure there is a next element. But, even more important, the peek() to investigate the "top" element plus the additional pop() to actually remove it, requires two lookups – where this operation should be possible with just a single one!

Hence, this should be simplified to:

while let Some((item, priority)) = queue.pop_if(|(item, priority)| some_condition(...)) {
    /* ... */
}

Or is there a better way already ???

Originally posted by @lordmulder in #55

@garro95 garro95 self-assigned this Feb 20, 2025
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

1 participant