Skip to content

Fix bug in zipWithIndex and set zip(that, selector) public in RxScala #1226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}

}