-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add useful predicates to filter based on the previous value #341
Conversation
I needed this to avoid sending configuration updates if the configuration didn't change and thought it might be generally useful. |
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
This directory should have been removed when the `util` package was removed. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
The new `ChangedOnly` predicate is is just a special case of `OnlyIfPrevious` that skips messages that are equal to the previous one. This is such a common use case that it deserves its own predicate. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great :)
|
The names for these are very confusing to me. Does I'd suggest I also realize that the |
Yeah, this is a problem I always have with I actually considered various options and leaned towards the ones I chose after bouncing with ChatGPT, but always trying to stay in tune with how Python's built-in
(as a lisp guy I'm surprised is not natural for you :P) So,
Yeah, no, is the other way around 😆 I'd prefer to do the current v1.4 release with this as it is, as I would like to use it already, but we can create issues for future improvements, the predicates are experimental so we can do breaking changes until we stabilize it. |
The problem is that the
I think
I'm not sure we should do that because the alternative names are unfamiliar and might be hard to find. But clarifying the predicate names goes a long way already. |
But but this is a problem with the ambiguity of |
For me is one or the other, once you know For more context, here is copilot response to my ask for name options: Ah, good point about avoiding "filter" since it would lead to redundant expressions like
Among these, I would recommend either
My top choice would be Would you like me to elaborate on any of these suggestions or propose more alternatives with different naming patterns? I decided to believe "only" was a common pattern in filters but maybe it hallucinated because googling I can't find anything about that being a pattern for filter predicates. Asking about that: I need to correct my previous suggestion as I can't actually find good examples of predicates or filters using the "Only" suffix pattern. Looking at some common libraries and languages: Python's itertools and built-ins:
Rust's Iterator:
C++'s algorithms:
Java's Stream:
Looking at these, better naming patterns would follow:
So I should revise my suggestion -
Would you like me to elaborate on any of these patterns with more examples from other libraries? Still not really giving predicate names examples, but at least it confirms that both |
This PR started as a way to remove duplicates from a receiver, but then was expanded to
OnlyIfPrevious
, a more generic approach accepting any type of sub-predicate to compare with the previous message.OnlyIfPrevious
is used as a base to implement also a specific predicate to remove the duplicates (ChangedOnly
), as this is a very common use case.OnlyIfPrevious
could be made even more generic by accepting also a function to determine what to save as the last message instead of always saving the new message. Such functionality would allow to implement a filter that ensured the received messages are monotonically increasing for example, by saving as the last message themax(old, new)
instead of just thenew
message.But this is left as a future improvement, as it is also not trivial because such function should be able to also receive the sentinel, which can complicate the API a bit.