Merge pull request #6856 from opencastsoftware/remove-for-set-and-map

Add Remove instances for Set and Map
This commit is contained in:
eugene yokota 2022-06-16 18:24:28 -04:00 committed by GitHub
commit 5e3f547710
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -34,4 +34,14 @@ object Remove {
def removeValue(a: Seq[T], b: Option[T]): Seq[T] = b.fold(a)(a filterNot _.==)
def removeValues(a: Seq[T], b: Option[T]): Seq[T] = b.fold(a)(a filterNot _.==)
}
implicit def removeSet[T, V <: T]: Sequence[Set[T], Set[V], V] =
new Sequence[Set[T], Set[V], V] {
def removeValue(a: Set[T], b: V): Set[T] = a - b
def removeValues(a: Set[T], b: Set[V]): Set[T] = a diff (b.toSeq: Seq[T]).toSet
}
implicit def removeMap[A, B, X <: A]: Sequence[Map[A, B], Seq[X], X] =
new Sequence[Map[A, B], Seq[X], X] {
def removeValue(a: Map[A, B], b: X): Map[A, B] = a - b
def removeValues(a: Map[A, B], b: Seq[X]): Map[A, B] = a -- b
}
}

View File

@ -1,6 +1,8 @@
val intsTask = taskKey[Seq[Int]]("A seq of ints task")
val intsSetting = settingKey[Seq[Int]]("A seq of ints setting")
val intsFromScalaV = settingKey[Seq[Int]]("a seq of ints from scalaVersion")
val intsSetSetting = settingKey[Set[Int]]("A set of ints setting")
val stringIntMapSetting = settingKey[Map[String, Int]]("A map of string to int setting")
scalaVersion := "2.11.6"
@ -22,9 +24,19 @@ intsFromScalaV --= { if (scalaVersion.value == "2.11.6") Seq(1, 2) else Seq(4) }
intsFromScalaV -= { if (scalaVersion.value == "2.11.6") Option(6) else None }
intsFromScalaV --= { if (scalaVersion.value == "2.11.6") Option(7) else None }
intsSetSetting := Set(1, 2, 3, 4, 5, 6, 7)
intsSetSetting -= 3
intsSetSetting --= Set(1, 2)
stringIntMapSetting := Map("a" -> 1, "b" -> 2 , "c" -> 3, "d" -> 4, "e" -> 5)
stringIntMapSetting -= "c"
stringIntMapSetting --= Seq("a", "b")
val check = taskKey[Unit]("Runs the check")
check := {
assert(intsTask.value == Seq(4, 5), s"intsTask should be Seq(4, 5) but is ${intsTask.value}")
assert(intsSetting.value == Seq(4, 5), s"intsSetting should be Seq(4, 5) but is ${intsSetting.value}")
assert(intsFromScalaV.value == Seq(4, 5), s"intsFromScalaV should be Seq(4, 5) but is ${intsFromScalaV.value}")
assert(intsSetSetting.value == Set(4, 5, 6, 7), s"intsSetSetting should be Set(4, 5, 6, 7) but is ${intsSetSetting.value}")
assert(stringIntMapSetting.value == Map("d" -> 4, "e" -> 5), s"stringIntMapSetting should be Map(d -> 4, e -> 5) but is ${stringIntMapSetting.value}")
}