Skip to content

Commit b56f0a5

Browse files
Merge pull request #1226 from zsxwing/zip
Fix bug in `zipWithIndex` and set `zip(that, selector)` public in RxScala
2 parents 07d6583 + 1b30485 commit b56f0a5

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/Observable.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,18 +385,16 @@ trait Observable[+T]
385385
* is the minumum of the number of `onNext` invocations of `this` and `that`.
386386
*/
387387
def zip[U](that: Observable[U]): Observable[(T, U)] = {
388-
zip(that, (t: T, u: U) => (t, u))
388+
zipWith(that, (t: T, u: U) => (t, u))
389389
}
390390

391391
/**
392392
* Returns an Observable formed from this Observable and another Observable by combining
393393
* corresponding elements using the selector function.
394394
* The number of `onNext` invocations of the resulting `Observable[(T, U)]`
395395
* is the minumum of the number of `onNext` invocations of `this` and `that`.
396-
*
397-
* Note that this function is private because Scala collections don't have such a function.
398396
*/
399-
private def zip[U, R](that: Observable[U], selector: (T,U) => R): Observable[R] = {
397+
def zipWith[U, R](that: Observable[U], selector: (T,U) => R): Observable[R] = {
400398
toScalaObservable[R](rx.Observable.zip[T, U, R](this.asJavaObservable, that.asJavaObservable, selector))
401399
}
402400

@@ -407,8 +405,7 @@ trait Observable[+T]
407405
* their index. Indices start at 0.
408406
*/
409407
def zipWithIndex: Observable[(T, Int)] = {
410-
var n = 0;
411-
this.map(x => { val result = (x,n); n += 1; result })
408+
zip((0 until Int.MaxValue).toObservable)
412409
}
413410

414411
/**

language-adaptors/rxjava-scala/src/test/scala/rx/lang/scala/ObservableTest.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,12 @@ class ObservableTests extends JUnitSuite {
168168
val o: Observable[String] = List[String]().toObservable.tail
169169
o.toBlockingObservable.toList
170170
}
171+
172+
@Test
173+
def testZipWithIndex() {
174+
val o = List("alice", "bob", "carol").toObservable.zipWithIndex.map(_._2)
175+
assertEquals(List(0, 1, 2), o.toBlockingObservable.toList)
176+
assertEquals(List(0, 1, 2), o.toBlockingObservable.toList)
177+
}
178+
171179
}

0 commit comments

Comments
 (0)