-
Notifications
You must be signed in to change notification settings - Fork 7.6k
add Observable.sort() instance methods #1386
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
Conversation
RxJava-pull-requests #1325 SUCCESS |
I am not a big fan of having any sort of |
Sorting doesn't make sense for infinite streams of course. On one project I use rxjava in a high performance computing role to process chunks of data on disk. Many thousands of chunks of data need to be sorted within each chunk then aggregated chunk by chunk in parallel later merged and aggregated with another function. Works well and quickly, very happy with it. Observables come in all flavours small, large, infinite, etc and a sort method makes sense for the small (collection wholly fits in memory) to me. |
But you are not sorting an observable, you are sorting a list created from an observable. Saying xs.sort() seems like it is harmless, but it isn't. Truth in advertising. And for your scenario, of course I lack the details, but I really wonder why you turn sorted lists back to observables, and don't work at the level of |
Note it sucks that Java does not have extension methods. In .NET or Scala you can easily add this as a convenience method for your specific use-case. |
my scenario, nope not that simple. The top level code is here if you are interested but it might be a bit too much detail! In short I have 115m timestamped position reports for ships over a year and for each ship I load the reports and sort by time (they arrive out of order due to different delivery latencies). At that point I can join the dots and start calculating metrics about grids on the earth like distance travelled per cell. you then end up with nifty vessel traffic density plots. I take your point about extension methods. The language you use changes the things you care about eh. I'm a long term scala person but haven't used rxjava with it yet. I'm not really fussed about this pull request, just thought I'd put it out there as a practical thing and expected pushback as the |
Very deliberate. It is not a streaming operation. |
I'd like to explore this a bit further to see if I can glean a rule to apply in other similar situations. The
I'm assuming that the 2nd point is not a problem, asynchronous processing can change order anyway and we often don't mind. Re the first point let's look at the the We are left with the fact that the entire stream is buffered. In general this sounds risky if your buffer has practical limits (like available memory). So lets reword the objection as that a potentially large stream is buffered. I went looking for card carrying Observable methods that have this characteristic and there are a few, among them
No doubt I've missed something important. Enlighten me @headinthebox. |
Well-behaved operators should produce output "in constant time" after receiving input. This has nothing to do with back pressure, please don't confuse orthogonal concerns. Changing the order is ok, as soon as you do flatMap or merge that will happen. So
|
The If I could go back to the beginning of RxJava, I'd probably argue for these types of operators to be on a different |
Yes, me too! |
I'm still searching for a bit of precision about these undesirable operators. The idea of producing output within constant time is interesting but I can manufacture a variant of I'm suspecting that it hinges on emission after completion of source. Note that Note also that So how's this: an operator is undesirable if
On this basis, the following operators would be considered undesirable or "bad"
The above criterion does not categorize |
Your list is correct (and include Adding a The way I'd prefer to handle these flattening requirements is with a I think the main drawback of an operator called None of those however are necessarily reasons for not adding |
IMHO you are overanalyzing this a bit :-). Of course creating a "stuttering" (term they use for this in process algebra) stream when you sort is not very useful. I am not I understand what the problem is with 'materialize' emitting after completion, it just turns an That aside, your list is spot on. You miss |
I'm more interested in the criterion I put forward than the list and I wasn't suggesting there is a problem with emitting after completion like
|
None of this is an argument for adding |
Not sure that works, for example under this definition Not you could try to define a You may want to take a look at http://en.wikipedia.org/wiki/Streaming_algorithm as well for interesting algorithms over infinite streams. |
That's a good characterization of it ... the stream vs non-stream operators. I think the question comes down to what's more important ... having easy (familiar ... in your face ... dangling right off We already crossed that line (Rx.Net led the way). So, do we try and pull back, or do we just accept it and let them all be in here. If we do let them all in, do we distinguish between them in any way? |
Note that there is no problem combining Rx with say Java8 streams when you do |
To be fair @benjchristensen in .NET we pulled back as well. No API is perfect, but that should not be a reason to make it more imperfect, |
@headinthebox I totally agree ... I was just commenting on how we're in a weird middle-ground where we have a mixture, so it's hard to say "no" to adding new non-stream operators when precedent already exists. |
Using the bad definition, |
I'm going to try to redefine "bad". Define a well behaved stream operator as one that
|
I've had a great time using the functional and streaming aspects of the api together. It's an absolute winner in the java space especially pre java 8 but also post. We could add javadoc to the poorly behaved stream operators so that people know and allow useful functional constructs to enter the api. I'm not a fan of making things verbose in the API because it's educational in some sense (toSortedList.flatten). Documentation can help there. |
Perhaps this. Stream Operator Criterion |
I'm going to pass on this for now ... sorting of streams is generally a bad idea, and we already have |
Added two
Observable.sort
instance methods that are equivalent toI guess you might ask well why don't people just code that. I think it adds the following:
Observable<List<T>>