Skip to content

Commit 3faa28a

Browse files
committed
Add immutable.Map updatedWith and organize Map tests
1 parent 62783e8 commit 3faa28a

File tree

3 files changed

+54
-38
lines changed

3 files changed

+54
-38
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ private[compat] trait PackageShared {
271271
self: scala.collection.Map[K, V]): MapExtensionMethods[K, V] =
272272
new MapExtensionMethods[K, V](self)
273273

274+
implicit def toImmutableMapExtensionMethods[K, V](
275+
self: scala.collection.immutable.Map[K, V]): ImmutableMapExtensionMethods[K, V] =
276+
new ImmutableMapExtensionMethods[K, V](self)
277+
274278
implicit def toMutableMapExtensionMethods[K, V](
275279
self: scala.collection.mutable.Map[K, V]): MutableMapExtensionMethods[K, V] =
276280
new MutableMapExtensionMethods[K, V](self)
@@ -527,6 +531,16 @@ class MapExtensionMethods[K, V](private val self: scala.collection.Map[K, V]) ex
527531

528532
}
529533

534+
class ImmutableMapExtensionMethods[K, V](private val self: scala.collection.immutable.Map[K, V])
535+
extends AnyVal {
536+
537+
def updatedWith[V1 >: V](key: K)(remappingFunction: (Option[V]) => Option[V1]): Map[K, V1] =
538+
remappingFunction(self.get(key)) match {
539+
case Some(v) => self.updated(key, v)
540+
case None => self - key
541+
}
542+
}
543+
530544
class MutableMapExtensionMethods[K, V](private val self: scala.collection.mutable.Map[K, V])
531545
extends AnyVal {
532546

compat/src/test/scala/test/scala/collection/MapTest.scala

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.junit.Test
1616
import org.junit.Assert._
1717

1818
import scala.collection.compat._
19+
import scala.collection.{immutable => i, mutable => m}
1920

2021
class MapTest {
2122

@@ -27,4 +28,43 @@ class MapTest {
2728
assertEquals(map, copy)
2829
}
2930

31+
@Test
32+
def mutableUpdateWith: Unit = {
33+
val map = m.Map("a" -> 1, "c" -> 3)
34+
map.updateWith("b") {
35+
case None => Some(2)
36+
case Some(_) => Some(-1)
37+
}
38+
map.updateWith("c") {
39+
case None => Some(-1)
40+
case Some(_) => None
41+
}
42+
// unmodified entries are preserved
43+
assertEquals(map.get(a), Some(1))
44+
// updateWith can add entries
45+
assertEquals(map.get("b"), Some(2))
46+
// updateWith can remove entries
47+
assertFalse(map.contains("c"))
48+
}
49+
50+
@Test
51+
def immutableUpdatedWith: Unit = {
52+
val map = i.Map("a" -> 1, "c" -> 3)
53+
val bAdded = map.updatedWith("b") {
54+
case None => Some(2)
55+
case Some(_) => Some(-1)
56+
}
57+
val cRemoved = map.updatedWith("c") {
58+
case None => Some(-1)
59+
case Some(_) => None
60+
}
61+
// unmodified entries are preserved
62+
assertEquals(map.get(a), bAdded.get(a))
63+
assertEquals(map.get(a), cRemoved.get(a))
64+
// updatedWith can add entries
65+
assertEquals(bAdded.get("b"), Some(2))
66+
// updatedWith can remove entries
67+
assertFalse(cRemoved.contains("c"))
68+
}
69+
3070
}

compat/src/test/scala/test/scala/collection/MutableMapTest.scala

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)