Skip to content

Commit ed030db

Browse files
update scala README
1 parent 6730544 commit ed030db

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

language-adaptors/rxjava-scala/README.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,71 @@
11
# Scala Adaptor for RxJava
22

3-
There's an old Scala adaptor ( `rx.lang.scala.RxImplicits` with test `rx.lang.scala.RxImplicitsTest` ), which is deprecated. All other classes in `rx.lang.scala` belong to the new adaptor.
3+
This adaptor allows to use RxJava in Scala with anonymous functions, e.g.
44

5-
# Binaries
5+
```scala
6+
val o = Observable.interval(200 millis).take(5)
7+
o.subscribe(n => println("n = " + n))
8+
Observable(1, 2, 3, 4).reduce(_ + _)
9+
```
10+
11+
For-comprehensions are also supported:
12+
13+
```scala
14+
val first = Observable(10, 11, 12)
15+
val second = Observable(10, 11, 12)
16+
val booleans = for ((n1, n2) <- (first zip second)) yield (n1 == n2)
17+
```
18+
19+
Further, this adaptor attempts to expose an API which is as Scala-idiomatic as possible. This means that certain methods have been renamed, their signature was changed, or static methods were changed to instance methods. Some examples:
20+
21+
```scala
22+
// instead of concat:
23+
def ++[U >: T](that: Observable[U]): Observable[U]
24+
25+
// instance method instead of static:
26+
def zip[U](that: Observable[U]): Observable[(T, U)]
27+
28+
// the implicit evidence argument ensures that dematerialize can only be called on Observables of Notifications:
29+
def dematerialize[U](implicit evidence: T <:< Notification[U]): Observable[U]
30+
31+
// additional type parameter U with lower bound to get covariance right:
32+
def onErrorResumeNext[U >: T](resumeFunction: Throwable => Observable[U]): Observable[U]
33+
34+
// curried in Scala collections, so curry fold also here:
35+
def fold[R](initialValue: R)(accumulator: (R, T) => R): Observable[R]
36+
37+
// using Duration instead of (long timepan, TimeUnit duration):
38+
def sample(duration: Duration): Observable[T]
39+
40+
// called skip in Java, but drop in Scala
41+
def drop(n: Int): Observable[T]
42+
43+
// there's only mapWithIndex in Java, because Java doesn't have tuples:
44+
def zipWithIndex: Observable[(T, Int)]
45+
46+
// corresponds to Java's toList:
47+
def toSeq: Observable[Seq[T]]
48+
49+
// the implicit evidence argument ensures that switch can only be called on Observables of Observables:
50+
def switch[U](implicit evidence: Observable[T] <:< Observable[Observable[U]]): Observable[U]
51+
52+
// Java's from becomes apply, and we use Scala Range
53+
def apply(range: Range): Observable[Int]
54+
55+
// use Bottom type:
56+
def never: Observable[Nothing]
57+
```
58+
59+
Also, the Scala Observable is fully covariant in its type parameter, whereas the Java Observable only achieves partial covariance due to limitations of Java's type system (or if you can fix this, your suggestions are very welcome).
60+
61+
For more examples, see [RxScalaDemo.scala](https://github.com/Netflix/RxJava/blob/master/language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/examples/RxScalaDemo.scala).
62+
63+
Scala code using Rx should only import members from `rx.lang.scala` and below.
64+
65+
Work on this adaptor is still in progress, and for the moment, the best source of documentation are the comments in the source code of [`rx.lang.scala.Observable`](https://github.com/Netflix/RxJava/blob/master/language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/Observable.scala).
66+
67+
68+
## Binaries
669

770
Binaries and dependency information for Maven, Ivy, Gradle and others can be found at [http://search.maven.org](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22rxjava-scala%22).
871

0 commit comments

Comments
 (0)