Skip to content

Commit 3c90565

Browse files
committed
Fix the bug that using mutable function in 'zipWithIndex'
1 parent 07d6583 commit 3c90565

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,7 @@ trait Observable[+T]
407407
* their index. Indices start at 0.
408408
*/
409409
def zipWithIndex: Observable[(T, Int)] = {
410-
var n = 0;
411-
this.map(x => { val result = (x,n); n += 1; result })
410+
zip((0 until Int.MaxValue).toObservable)
412411
}
413412

414413
/**

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)