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

map doesn't work with random numbers #719

Closed
samuela opened this issue Jan 3, 2014 · 5 comments
Closed

map doesn't work with random numbers #719

samuela opened this issue Jan 3, 2014 · 5 comments

Comments

@samuela
Copy link

samuela commented Jan 3, 2014

This may be specific to the Scala Adapter or a general issue.. I'm not sure. It may also be simply disallowed somewhere in the documentation but I couldn't find anything mentioning it.

val o = Observable.interval(250 millis)
        .map(x => math.random)
        .take(10)
var id = o map(x => x)

o.subscribe(n => println("n = " + n))
id.subscribe(n => println("id = " + n))

produces

n = 0.16257918600676124
id = 0.015701253635574397
n = 0.65811522451999
id = 0.6745847570884935
n = 0.08592558068181289
id = 0.6761688548540207
n = 0.1511738754239501
id = 0.37751043543314455
n = 0.3335425497156287
id = 0.2433811198187099
...

Of course, we expect the values of the two Observables to be equal but they aren't related whatsoever.

@samuelgruetter
Copy link
Contributor

This behavior is the expected behavior. Remove the line var id = ... and replace the line id.subscribe(...) by a second o.subscribe(...), and you will see that the two outputs are still different. The reason is that everywhere in Rx, each subscriber gets its own version of the Observable it subscribes to. So here, each subscriber gets its own "Observable.interval", so x => math.random is executed seperately for each subscriber. If you want several observers to share the same Observable, you should use the publish operator.

@headinthebox
Copy link
Contributor

You have discovered the distinction between "hot" and "cold" observables and the evil that is side-effects.

http://www.introtorx.com/content/v1.0.10621.0/14_HotAndColdObservables.html

@chrisgrimm
Copy link

following up on a previous comment, your code should read something like this:

val (start, o) = Observable.interval(250 millis) map {x => math.random} take 10 publish
val id = o map {x => x}
start()
o subscribe {x => println(s"n = $x")}
id subscribe {x => println(s"id = $x")}

@headinthebox
Copy link
Contributor

Note that we will be changing the Scala bindings to use ConnectableObservable so while this code work now, in the future you would write something like so:

val observable = Observable.interval(250 millis).map(x => math.random).take(10).publish
val id = observable.map(x => x)

observable.subscribe(x => println(s"n = $x"))
id.subscribe(x => println(s"id = $x"))

observable.connect

@samuela
Copy link
Author

samuela commented Jan 3, 2014

Ah, gotcha.. thanks for the clarification!

@samuela samuela closed this as completed Jan 3, 2014
jihoonson pushed a commit to jihoonson/RxJava that referenced this issue Mar 6, 2020
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

4 participants