Skip to content

Commit a1cc269

Browse files
committed
Fix implementation of filterKeys
Fix implementation of `IterableView#filterKeys` and add a commented-out, fixed implementation of `IterableView#mapValues`.
1 parent 2badce8 commit a1cc269

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,14 @@ class MapViewExtensionMethods[K, V, C <: scala.collection.Map[K, V]](
284284
implicit bf: CanBuildFrom[IterableView[(K, V), C], (K, W), That]): That =
285285
self.map[(K, W), That] { case (k, v) => (k, f(v)) }
286286

287-
def filterKeys[That](p: K => Boolean)(
288-
implicit bf: CanBuildFrom[IterableView[(K, V), C], (K, V), That]): That =
289-
self.collect[(K, V), That] { case (k, v) if p(k) => (k, v) }
287+
// TODO: replace the current implementation of `mapValues` with this
288+
// after major version bump when bincompat can be broken
289+
/*
290+
def mapValues[W](f: V => W): IterableView[(K, W), C] =
291+
// the implementation of `self.map` also casts the result
292+
self.map({ case (k, v) => (k, f(v)) }).asInstanceOf[IterableView[(K, W), C]]
293+
*/
294+
295+
def filterKeys(p: K => Boolean): IterableView[(K, V), C] =
296+
self.filter { case (k, _) => p(k) }
290297
}

compat/src/test/scala/test/scala/collection/ViewTest.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,12 @@ class ViewTest {
3535
assertEquals(oldStyle.toMap, newStyle.toMap)
3636
}
3737

38+
@Test
39+
def filterKeysMapValues(): Unit = {
40+
val m = Map("a" -> 1, "b" -> 2, "c" -> 3)
41+
assertEquals(Map(), m.view.filterKeys(_.length > 1).mapValues(_ + 1).toMap)
42+
// TODO: uncomment after major version bump when `mapValues` can be fixed
43+
//assertEquals(Map(), m.view.mapValues(_ + 1).filterKeys(_.length > 1).toMap)
44+
}
45+
3846
}

0 commit comments

Comments
 (0)