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

Observable.concat does not #588

Closed
LannyRipple opened this issue Dec 8, 2013 · 4 comments
Closed

Observable.concat does not #588

LannyRipple opened this issue Dec 8, 2013 · 4 comments

Comments

@LannyRipple
Copy link

This issue was discovered by Graeme Ludwig during the Coursera "Principles of Reactive Programming" class.

scala> import rx.lang.scala._
import rx.lang.scala._

scala> Observable(1,2,3,4,5,6,7,8,9,10).groupBy(_ % 2 == 0).filter{_._1 == true}.map{_._2}.concat.subscribe(x => println(x))
2
4
6
8
10
res0: rx.lang.scala.Subscription = rx.lang.scala.Subscription$$anon$1@6e0031c0

scala> Observable(1,2,3,4,5,6,7,8,9,10).groupBy(_ % 2 == 0).filter{_._1 == false}.map{_._2}.concat.subscribe(x => println(x))
1
3
5
7
9
res1: rx.lang.scala.Subscription = rx.lang.scala.Subscription$$anon$1@3f18dc75

scala> Observable(1,2,3,4,5,6,7,8,9,10).groupBy(_ % 2 == 0).map{_._2}.concat.subscribe(x => println(x))
1
3
5
7
9
res2: rx.lang.scala.Subscription = rx.lang.scala.Subscription$$anon$1@320bdadc

The concat method seems to be misplacing the results of one of the Observables. Above was using rxjava-scala 0.15.1

@akarnokd
Copy link
Member

akarnokd commented Dec 8, 2013

Could be the manifestation of Issues #552 and #583, and hopefully fixed by #586.
Excuse my lack of Scala knowledge, but where does this misplacement happen in the examples above?

@headinthebox
Copy link
Contributor

The scala bindings typically add no logic of themselves but just wrap & unwrap the underlying Java classes, so in the common case the issue is in RxJava as @akernokd suggests. @LannyRipple can you repro this in Java.

@akarnokd
Copy link
Member

akarnokd commented Dec 8, 2013

I did some testing (with Java 8) and it prints the same values with and without the concat fix. I think @LannyRipple expected the last one to print {1,3,5,7,9,2,4,6,8,10}, so this is rather the good old groupBy issue: once concat finishes with the odd values, the even group has already finished and there is nothing to print. (#282 and #289). However, the workaround I proposed in #289 works:

Observable<Integer> source = Observable.from(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Func1<Integer, Boolean> gf = v -> v % 2 == 0;
ConnectableObservable<Integer> co = source.replay();
Observable.concat(
     co.groupBy(gf)
          .map(u -> co.where(v -> gf.call(v) == u.getKey()))
).subscribe(System.out::println);
co.connect();

or

Observable.concat(
    source.groupBy(x -> x % 2 == 0).map(u -> u.replay()).doOnEach(v -> v.connect()))
.subscribe(System.out::println);

@LannyRipple
Copy link
Author

Oops. Sorry. I should have mentioned the output I expected which would have been the odd numbers then the evens.

I'll make a mental note and reply back to the class forums that groupBy can cause surprising results. Thanks for the clarification of the issue.

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

No branches or pull requests

3 participants