-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Flatten operator #295
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
Comments
It looks a bit similar to the Not only would this be the reverse operation of Note that this does have some limitations. To me The only usable syntax I can come up with is: |
that's a good point actually, didn't think about this. If we can't have the extra readability by flatten being an instance method, it might even suffice to have a flatten Func1 for now, which could be used like this:
This is not much longer, doesn't read much worse, and would have the benefit of not impacting the Observable class interface. |
I don't have much opinion on the syntax, but I'd find this useful. I end up Dave On Tuesday, June 18, 2013, Matthias Käppler wrote:
|
Here is how to flatten var observable = Observable.Return(new[] { 1, 2, 3, 4, 5 });
observable // IObservable<int[]> observable
.SelectMany(x => x) // returns Observable<int>
.Subscribe(Console.WriteLine); Here is the public static IObservable<TResult> SelectMany<TSource, TResult>(this IObservable<TSource> source, Func<TSource, IEnumerable<TResult>> selector); |
Can anyone see a way of doing this that is type-safe? Perhaps |
I'm slightly confused by this discussion and I'm not sure if everyone is talking about the same thing... ;-) But I think @benjchristensen you're looking for something like this: // in Observable.java:
public static <T> Observable<T> flatten(Observable<? extends List<? extends T>> source) {
Func1<List<? extends T>, Observable<T>> f = new Func1<List<? extends T>, Observable<T>>() {
public Observable<T> call(List<? extends T> t1) {
return Observable.from(t1);
}
};
return Observable.concat(source.map(f));
}
// in some test file:
@Test public void testFlattenFromList() {
Observable<List<Integer>> o = Observable.from(Arrays.asList(1, 2), Arrays.asList(3, 4), Arrays.asList(5, 6));
for (Integer i : flatten(o).toBlockingObservable().toIterable()) System.out.println(i);
} |
Yes, this looks straightforward. Rx.NET has a SelectMany overload for this kind of operation. I think using a |
With new lift operation on Observables it's quite easy to create flatten operator. I've created something like this:
Usage:
I'm not sure how good is that code, but it looks doable with type safety so It might be good moment to add it do core operators? |
That works well to pull in via lift() because you can provide the correctly typed implementation for the It could be done as a static In short, Java doesn't allow conditional extension methods on a class based on its generic type, so a |
Sure, we shouldn't force to add that to |
Other notable places where we've done this are We could go crazy with the overloads to make things type safe
ok that was a silly idea. |
Yes, |
We have yet to start exposing custom operators for use via |
Originally from #1408 by @ccmtaylor In my code, I often make API calls that return moviesService
.listMovies()
.flatMap(movies -> Observable.from(movies))
.map(movie -> doSomethingWith(movie)); In Java 8 / scala, this isn't too bad, but with Java 7 / Android syntax, the flatMap call is not pretty. Since we can't add a
|
As this discussion continues, some items to keep in mind...
|
Note that here you can use That said, I agree with Ben, it is painful that Java has no extension methods so it becomes either/or. the saving grace (mmm) is that static methods do not need a receiver, it is less ugly to write |
Here is how to do it with Observable<Iterable<Integer>> o = Observable.from(Arrays.asList(1, 2), Arrays.asList(3, 4));
o.flatMapIterable(is -> is).forEach(System.out::println); |
Hello, there's no more from method in rxjava 2. Is there a workaround? |
@vincent-paing check again, it is still called |
@akarnokd yea but there's no longer "from" method. I tried used fromIterable(myArrayList) but IDE gives me error due to different type |
It is called |
@akarnokd Thanks, that works :D |
This is more a request for discussion.
I'm not sure .NET's Rx has something like this, but one thing I miss in RxJava is an operator to flatten an
Observable<List<T>>
toObservable<T>
. As far as I can tell, this currently requires one to write somewhat clunky code using flatMap:i.e. there seems to be no standard reverse operation for toList. Is this something you would deem worth having in the library itself? It would simplify the above call to:
The text was updated successfully, but these errors were encountered: