Skip to content

Commit 44566ac

Browse files
committed
Merge remote-tracking branch 'origin/main' into scalafmt-upgrade
2 parents f4728ee + 0add397 commit 44566ac

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ jobs:
5555
CI_MODE: ${{matrix.mode}}
5656
CI_PLATFORM: ${{matrix.platform}}
5757
steps:
58-
- uses: actions/checkout@v2
58+
- uses: actions/checkout@v3
5959
with:
6060
fetch-depth: 0
6161
- uses: coursier/cache-action@v6
62-
- uses: actions/setup-java@v2
62+
- uses: actions/setup-java@v3
6363
with:
6464
distribution: temurin
6565
java-version: ${{matrix.java}}

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ lazy val compat = new MultiScalaCrossProject(
8282
sharedSourceDir / "scala-2.11_2.12"
8383
}
8484
},
85-
versionPolicyIntention := Compatibility.BinaryCompatible,
85+
versionPolicyIntention := Compatibility.BinaryAndSourceCompatible,
8686
mimaBinaryIssueFilters ++= {
8787
import com.typesafe.tools.mima.core._
8888
import com.typesafe.tools.mima.core.ProblemFilters._

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,19 @@ class TraversableLikeExtensionMethods[A, Repr](private val self: c.GenTraversabl
564564
}
565565
map.toMap
566566
}
567+
568+
def distinctBy[B, That](f: A => B)(implicit cbf: CanBuildFrom[Repr, A, That]): That = {
569+
val builder = cbf()
570+
val keys = collection.mutable.Set.empty[B]
571+
for (element <- self) {
572+
val key = f(element)
573+
if (!keys.contains(key)) {
574+
builder += element
575+
keys += key
576+
}
577+
}
578+
builder.result()
579+
}
567580
}
568581

569582
class TrulyTraversableLikeExtensionMethods[El1, Repr1](

compat/src/test/scala/test/scala/collection/CollectionTest.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,11 @@ class CollectionTest {
186186
assertTrue(List(1, 2, 3).lengthIs >= 2)
187187
assertTrue(List(1, 2, 3).lengthIs > 2)
188188
}
189+
190+
@Test
191+
def testDistinctBy(): Unit = {
192+
assertEquals(List(1, 2, 3).distinctBy(_ % 2 == 0), List(1, 2))
193+
assertEquals(List(3, 1, 2).distinctBy(_ % 2 == 0), List(3, 2))
194+
assertEquals(List.empty[Int].distinctBy(_ % 2 == 0), List.empty)
195+
}
189196
}

0 commit comments

Comments
 (0)