Merge pull request #2510 from dwijnand/append-and-remove-option

Add Append & Remove instances for Option
This commit is contained in:
Dale Wijnand 2016-03-31 16:41:29 +01:00
commit 37c7f3febc
5 changed files with 54 additions and 4 deletions

View File

@ -56,4 +56,9 @@ object Append {
def appendValues(a: Map[A, B], b: Map[X, Y]): Map[A, B] = a ++ b
def appendValue(a: Map[A, B], b: (X, Y)): Map[A, B] = a + b
}
implicit def appendOption[T]: Sequence[Seq[T], Option[T], Option[T]] =
new Sequence[Seq[T], Option[T], Option[T]] {
def appendValue(a: Seq[T], b: Option[T]): Seq[T] = b.fold(a)(a :+ _)
def appendValues(a: Seq[T], b: Option[T]): Seq[T] = b.fold(a)(a :+ _)
}
}

View File

@ -11,10 +11,15 @@ object Remove {
trait Values[A, -B] extends Any {
def removeValues(a: A, b: B): A
}
sealed trait Sequence[A, -B, T] extends Value[A, T] with Values[A, B]
trait Sequence[A, -B, T] extends Value[A, T] with Values[A, B]
implicit def removeSeq[T, V <: T]: Sequence[Seq[T], Seq[V], V] = new Sequence[Seq[T], Seq[V], V] {
def removeValue(a: Seq[T], b: V): Seq[T] = a filterNot b.==
def removeValues(a: Seq[T], b: Seq[V]): Seq[T] = a diff b
}
implicit def removeOption[T]: Sequence[Seq[T], Option[T], Option[T]] =
new Sequence[Seq[T], Option[T], Option[T]] {
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 _.==)
}
}

View File

@ -0,0 +1,33 @@
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")
scalaVersion := "2.11.6"
intsTask := Nil
intsTask += 3
intsTask ++= Seq(1, 2)
intsTask += Option(6)
intsTask ++= Option(7)
intsSetting := Nil
intsSetting += 3
intsSetting ++= Seq(1, 2)
intsSetting += Option(6)
intsSetting ++= Option(7)
intsFromScalaV := Nil
intsFromScalaV += { if (scalaVersion.value == "2.11.6") 3 else 5 }
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 }
val check = taskKey[Unit]("Runs the check")
check := {
assertEquals("intsTask", intsTask.value, Seq(3, 1, 2, 6, 7))
assertEquals("intsSetting", intsSetting.value, Seq(3, 1, 2, 6, 7))
assertEquals("intsFromScalaV", intsFromScalaV.value, Seq(3, 1, 2, 6, 7))
}
def assertEquals[T](label: String, actual: T, expect: T) =
assert(actual == expect, s"$label should be $expect but is $actual")

View File

@ -0,0 +1 @@
> check

View File

@ -4,17 +4,23 @@ val intsFromScalaV = settingKey[Seq[Int]]("a seq of ints from scalaVersion")
scalaVersion := "2.11.6"
intsTask := Seq(1, 2, 3, 4, 5)
intsTask := Seq(1, 2, 3, 4, 5, 6, 7)
intsTask -= 3
intsTask --= Seq(1, 2)
intsTask -= Option(6)
intsTask --= Option(7)
intsSetting := Seq(1, 2, 3, 4, 5)
intsSetting := Seq(1, 2, 3, 4, 5, 6, 7)
intsSetting -= 3
intsSetting --= Seq(1, 2)
intsSetting -= Option(6)
intsSetting --= Option(7)
intsFromScalaV := Seq(1, 2, 3, 4, 5)
intsFromScalaV := Seq(1, 2, 3, 4, 5, 6, 7)
intsFromScalaV -= { if (scalaVersion.value == "2.11.6") 3 else 5 }
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 }
val check = taskKey[Unit]("Runs the check")
check := {