Skip to content

Commit b0098c5

Browse files
committed
scala.collection.Map.foreachEntry backport for 2.11/2.12
Fixes #335.
1 parent c828122 commit b0098c5

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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

+12
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ private[compat] trait PackageShared {
177177
type IterableOnce[+X] = c.TraversableOnce[X]
178178
val IterableOnce = c.TraversableOnce
179179

180+
implicit def toMapExtensionMethods[K, V](self: scala.collection.Map[K, V]): MapExtensionMethods[K, V] =
181+
new MapExtensionMethods[K, V](self)
182+
180183
implicit def toMapViewExtensionMethods[K, V, C <: scala.collection.Map[K, V]](
181184
self: IterableView[(K, V), C]): MapViewExtensionMethods[K, V, C] =
182185
new MapViewExtensionMethods[K, V, C](self)
@@ -393,6 +396,15 @@ class Tuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2](
393396
new Tuple3Zipped((self.colls._1, self.colls._2, t3))
394397
}
395398

399+
class MapExtensionMethods[K, V](private val self: scala.collection.Map[K, V])
400+
extends AnyVal {
401+
402+
def foreachEntry[U](f: (K, V) => U): Unit = {
403+
self.foreach { case (k, v) => f(k, v) }
404+
}
405+
406+
}
407+
396408
class MapViewExtensionMethods[K, V, C <: scala.collection.Map[K, V]](
397409
private val self: IterableView[(K, V), C])
398410
extends AnyVal {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
package test.scala.collection
13+
14+
import org.junit.Test
15+
import org.junit.Assert._
16+
17+
import scala.collection.compat._
18+
19+
class MapTest {
20+
21+
@Test
22+
def foreachEntry: Unit = {
23+
val map = Map("a" -> 1, "b" -> 2, "c" -> 3)
24+
var copy = Map.empty[String, Int]
25+
map.foreachEntry((k, v) => copy += k -> v)
26+
assertEquals(map, copy)
27+
}
28+
29+
}

0 commit comments

Comments
 (0)