File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed
language-adaptors/rxjava-scala/src
examples/scala/rx/lang/scala/examples Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -1228,4 +1228,11 @@ class RxScalaDemo extends JUnitSuite {
1228
1228
}
1229
1229
o.subscribe(println(_))
1230
1230
}
1231
+
1232
+ @ Test def switchMapExample () {
1233
+ val o = Observable .interval(300 millis).take(5 ).switchMap[String ] {
1234
+ n => Observable .interval(50 millis).take(10 ).map(i => s " Seq ${n}: ${i}" )
1235
+ }
1236
+ o.toBlocking.foreach(println)
1237
+ }
1231
1238
}
Original file line number Diff line number Diff line change @@ -2237,6 +2237,23 @@ trait Observable[+T]
2237
2237
)
2238
2238
}
2239
2239
2240
+ /**
2241
+ * Returns a new Observable by applying a function that you supply to each item emitted by the source
2242
+ * Observable that returns an Observable, and then emitting the items emitted by the most recently emitted
2243
+ * of these Observables.
2244
+ *
2245
+ * <img width="640" height="350" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/switchMap.png">
2246
+ *
2247
+ * @param f a function that, when applied to an item emitted by the source Observable, returns an Observable
2248
+ * @return an Observable that emits the items emitted by the Observable returned from applying a function to
2249
+ * the most recently emitted item emitted by the source Observable
2250
+ */
2251
+ def switchMap [R ](f : T => Observable [R ]): Observable [R ] = {
2252
+ toScalaObservable[R ](asJavaObservable.switchMap[R ](new Func1 [T , rx.Observable [_ <: R ]] {
2253
+ def call (t : T ): rx.Observable [_ <: R ] = f(t).asJavaObservable
2254
+ }))
2255
+ }
2256
+
2240
2257
/**
2241
2258
* Given an Observable that emits Observables, creates a single Observable that
2242
2259
* emits the items emitted by the most recently published of those Observables.
You can’t perform that action at this time.
0 commit comments