Skip to content

Commit

Permalink
Merge pull request #1226 from zsxwing/zip
Browse files Browse the repository at this point in the history
Fix bug in `zipWithIndex` and set `zip(that, selector)` public in RxScala
  • Loading branch information
benjchristensen committed May 20, 2014
2 parents bd99d58 + d55a130 commit 777c42f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -385,18 +385,16 @@ trait Observable[+T]
* is the minumum of the number of `onNext` invocations of `this` and `that`.
*/
def zip[U](that: Observable[U]): Observable[(T, U)] = {
zip(that, (t: T, u: U) => (t, u))
zipWith(that, (t: T, u: U) => (t, u))
}

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

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,12 @@ class ObservableTests extends JUnitSuite {
val o: Observable[String] = List[String]().toObservable.tail
o.toBlockingObservable.toList
}

@Test
def testZipWithIndex() {
val o = List("alice", "bob", "carol").toObservable.zipWithIndex.map(_._2)
assertEquals(List(0, 1, 2), o.toBlockingObservable.toList)
assertEquals(List(0, 1, 2), o.toBlockingObservable.toList)
}

}

0 comments on commit 777c42f

Please sign in to comment.