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

Idiomatic filtering of values. #768

Closed
andrask opened this issue Jan 20, 2014 · 7 comments
Closed

Idiomatic filtering of values. #768

andrask opened this issue Jan 20, 2014 · 7 comments

Comments

@andrask
Copy link

andrask commented Jan 20, 2014

Is there a predicate to filter out null values or any values from a signal? Currently a new function has to be created to do this but it would be easy to create a general function.

// Filter out null values
signal.filter(new Func1<Date, Boolean>() {
    @Override
    public Boolean call(Date t1) {
        return (t1 != null);
    }
}).map(...);
@benjchristensen
Copy link
Member

We do not have a collection of common predicates. The closest to this idea we have currently is the rx.util.functions.Not predicate that negates whatever predicate it is given.

@andrask
Copy link
Author

andrask commented Jan 20, 2014

Guava has lots of predicates that could be reused here but that would be another dependency. As a simplified solution ReactiveCocoa has the ignore: operation that ignores elements equalling to the given parameter.

At the moment what I need is a simple method to ignore the default value of a BehaviorSubject. Filtering out nulls works in this case. But without predicates the code will be ugly.

Obviously skip does not work as it would skip the stored value for all new subscribers instead of just the default one. buffer with 1 is an alternative but that needs an additional map to flatten the one item lists. Do you have a better idea for how this could be done?

@akarnokd
Copy link
Member

That sounds you need PublishSubject instead of BehaviorSubject as the former doesn't have a default value.

@andrask
Copy link
Author

andrask commented Jan 21, 2014

But I need the 1 long buffer. That's why publishsubject + buffer + map
would solve the issue but it is just ugly and unnecessarily complex for a
general purpose like this. In other RX implementations there are operations
that support this behavior.

2014/1/21 akarnokd [email protected]

That sounds you need PublishSubject instead of BehaviorSubject as the
former doesn't have a default value.


Reply to this email directly or view it on GitHubhttps://github.com//issues/768#issuecomment-32826352
.

@akarnokd
Copy link
Member

So you need the latest value when someone subscribes but not an initial value? This can be achieved via replay(int).

@andrask
Copy link
Author

andrask commented Jan 21, 2014

There is a fundamental difference between behavior subject and publish
subject. Behavior subject is a connected signal: whenever a send happens,
that is stored. Publish subject starts working only after it is subscribed
to. (Error and Completed may be registered without a subscription, I
haven't tried those.) As a result, an explicit subscription is needed to
capture all events before a useful subscription is made.

Now the question is whether there is a connect method in the library that
just sits at the end of the line and ignores every send. Its only purpose
is to keep the signal hot to record all events in the replay.

2014/1/21 akarnokd [email protected]

So you need the latest value when someone subscribes but not an initial
value? This can be achieved via the replay(int)http://netflix.github.io/RxJava/javadoc/rx/Observable.html#replay(int)
.


Reply to this email directly or view it on GitHubhttps://github.com//issues/768#issuecomment-32835556
.

@benjchristensen
Copy link
Member

Here is how filtering can be done:

public class TestFilter {

    public static void main(String[] args) {
        Observable.range(0, 20).filter(i -> i % 2 == 0).subscribe(System.out::println);
        Observable.range(0, 20).filter(TestFilter::filterEven).subscribe(System.out::println);
    }

    public static boolean filterEven(int i) {
        return i % 2 != 0;
    }
}

If a library of common filters is desired, that could be a good contrib module, but does not belong in the core.

jihoonson pushed a commit to jihoonson/RxJava that referenced this issue Mar 6, 2020
Update `Battle of the Circuit Breakers: Resilience4J vs Istio` from slides to Youtube talk
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

3 participants