I was looking at the RxScala observables which are created at a given time interval:
val periodic: Observable[Long] = Observable.interval(100 millis)
periodic.foreach(x => println(x))
If I put this in a worksheet, I get this result:
periodic: rx.lang.scala.Observable[Long] = rx.lang.scala.JavaConversions$$anon$2@2cce3493
res0: Unit = ()
This leaves me confused: What do the elements of periodic
actually contain?
Do they contain some index? Do they contain the time interval at which they were created?
As you can read here http://reactivex.io/documentation/operators/interval.html produced elements are Long
values incrementing from 0
.
As for your code and results:
Here, you create the observable, and get Observable[Long]
assigned to periodic
. Everything as expected.
scala> val periodic: Observable[Long] = Observable.interval(100 millis)
periodic: rx.lang.scala.Observable[Long] = rx.lang.scala.JavaConversions$$anon$2@2cce3493
Here, you register a callback, i.e. what happens when value is emmited. The return type of foreach
method is Unit
as it doesn't have a reasonable value and happens just for the side effect of registering callbacks.
periodic.foreach(x => println(x))
res0: Unit = ()
You don't see actual values because execution stops. Try to insert Thread.sleep
.
val periodic: Observable[Long] = Observable.interval(100.millis)
periodic.foreach(x => println(x))
Thread.sleep(1000)
Gives output similar to
periodic: rx.lang.scala.Observable[Long] = rx.lang.scala.JavaConversions$$anon$2@207cb62f
res0: Unit = ()
0
1
2
3
4
5
6
7
8
9
res1: Unit = ()