-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Conversation
throw new IllegalStateException("Expected single entry. Actually more than one entry."); | ||
} | ||
|
||
if (!predicate.call(result)) { |
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.
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);
};
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.
Oh, I got it. It definitely makes sense.
Thank you for pointing out!
I'll update the code.
Manual merge of mairbek/single Pull #157
I completed this merge in #160 since it had conflicts. |
…merge Manual merge of mairbek/single Pull ReactiveX#157
For issue #77.