Skip to content

Implemented the 'any' operator#385

Merged
benjchristensen merged 4 commits into
ReactiveX:masterfrom
zsxwing:any
Sep 21, 2013
Merged

Implemented the 'any' operator#385
benjchristensen merged 4 commits into
ReactiveX:masterfrom
zsxwing:any

Conversation

@zsxwing

@zsxwing zsxwing commented Sep 16, 2013

Copy link
Copy Markdown
Member

This implements the operator Any from #24 in all two variants.

However, I encountered two problems.

Updated: the online document http://msdn.microsoft.com/en-us/library/hh211993(v=vs.103).aspx is wrong. See my later discussion.

Another question is if I add the any method to rx.Observable<T>, some unit tests will fail as the method any in rx.Observable<T> overrides the method org.mockito.Matchers.any(java.lang.Class<T>) in some unit tests (e.g., rx.subjects.ReplaySubject<T>). Do I need to use another method name, or just modify the unit tests? Now the any methods in rx.Observable<T> are commented out.

Thanks.

@cloudbees-pull-request-builder

Copy link
Copy Markdown

RxJava-pull-requests #284 SUCCESS
This pull request looks good

@benjchristensen

Copy link
Copy Markdown
Member

I haven't looked at the first question yet, but on the second one we'll likely need to stop importing org.mockito.Matchers.* and call it directly org.mockito.Matchers.any.

@zsxwing

zsxwing commented Sep 17, 2013

Copy link
Copy Markdown
Member Author

Sorry that I missed the keyword empty in my description. The first question is I'm not sure how to handle an empty observable sequence.

@cloudbees-pull-request-builder

Copy link
Copy Markdown

RxJava-pull-requests #285 ABORTED

@zsxwing

zsxwing commented Sep 17, 2013

Copy link
Copy Markdown
Member Author

I tested the C# Any today. The environment is VS2010 .Net 4.0.

I found the description in my VS is Determines whether any element of an observable sequence satisfies a condition. This is totally different from the online document http://msdn.microsoft.com/en-us/library/hh211993(v=vs.103).aspx.

I always did some tests for Any. Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reactive.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var any = Observable.Empty().Any();
            any.Subscribe(
                x => Console.WriteLine("subscriber got " + x) // subscriber got False
            );
            any = Observable.Range(1, 5).Any();
            any.Subscribe(
                x => Console.WriteLine("subscriber got " + x) // subscriber got True
            );
            
            any = Observable.Empty().Any(
                x => true
            );
            any.Subscribe(
                x => Console.WriteLine("subscriber got " + x) // subscriber got False
            );
            any = Observable.Range(1, 5).Any(
                x => x > 3
            );
            any.Subscribe(
                x => Console.WriteLine("subscriber got " + x) // subscriber got True
            );
            any = Observable.Range(1, 5).Any(
                x => x > 5
            );
            any.Subscribe(
                x => Console.WriteLine("subscriber got " + x) // subscriber got False
            );
            Console.ReadLine();
        }
    }
}

Here is the output:

subscriber got False
subscriber got True
subscriber got False
subscriber got True
subscriber got False

In summary,

  • If an observable sequence is empty, any emits false.
  • If one element of an observable sequence satisfies the condition, any emits true.
  • If all elements of an observable sequence do not satisfy the condition, any emits false.

@zsxwing

zsxwing commented Sep 17, 2013

Copy link
Copy Markdown
Member Author

I have implemented the correct 'any' operator. Please take a look. Thanks!

@cloudbees-pull-request-builder

Copy link
Copy Markdown

RxJava-pull-requests #286 ABORTED

@samuelgruetter

Copy link
Copy Markdown
Contributor

In Scala, we will probably use isEmpty instead of any() (negated), and exists(somePredicate) instead of any(somePredicate). You could use these names also in Java, to avoid confusion and name conflicts.

@benjchristensen

Copy link
Copy Markdown
Member

@zsxwing I don't have time tonight but will definitely get to this in the near future, thank you for getting involved!

@benjchristensen benjchristensen merged commit c8f1199 into ReactiveX:master Sep 21, 2013
@zsxwing zsxwing deleted the any branch September 23, 2013 11:15
rickbw pushed a commit to rickbw/RxJava that referenced this pull request Jan 9, 2014
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

Successfully merging this pull request may close these issues.

4 participants