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

Implemented single and singleOrDefault #157

Merged
merged 3 commits into from
Feb 28, 2013

Conversation

mairbek
Copy link
Contributor

@mairbek mairbek commented Feb 27, 2013

For issue #77.

throw new IllegalStateException("Expected single entry. Actually more than one entry.");
}

if (!predicate.call(result)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question about this based on reading the MSDN doc: http://msdn.microsoft.com/en-us/library/hh229425(v=vs.103).aspx

Is it supposed to have ONLY a single element that then must match the predicate, or can you have a sequence of N items as long as only 1 item matches the predicate?

Right now this implementation expects the sequence to only have 1 item and the predicate just validates it matches what's expected.

Here is the part that makes me think it should be the second definition:

"Returns the only element of an observable sequence that matches the predicate"

That suggests that a sequence will be filtered with the predicate and then throw an exception if there is more than one.

Thus, single with a predicate would be somewhat like this:

that.filter(predicate).single()

Here is code from the Javascript implementation that confirms my understanding if I'm reading it right (https://rx.codeplex.com/SourceControl/changeset/view/7881e17c060b#Rx/JS/rx.aggregates.js):

    /**
     * Returns the only element of an observable sequence that satisfies the condition in the optional predicate, and reports an exception if there is not exactly one element in the observable sequence.
     * 
     * 1 - res = source.single();
     * 2 - res = source.single(function (x) { return x === 42; });
     * 
     * @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
     * @return Sequence containing the single element in the observable sequence that satisfies the condition in the predicate.
     */
    observableProto.single = function (predicate) {
        if (predicate) {
            return this.where(predicate).single();
        }
        return singleOrDefaultAsync(this, false);
    };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I got it. It definitely makes sense.
Thank you for pointing out!

I'll update the code.

benjchristensen added a commit that referenced this pull request Feb 28, 2013
@benjchristensen benjchristensen merged commit 3cfc156 into ReactiveX:master Feb 28, 2013
@benjchristensen
Copy link
Member

I completed this merge in #160 since it had conflicts.

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.

None yet

2 participants