Add Append & Remove instances for Option

This commit is contained in:
Dale Wijnand 2016-03-11 17:18:57 +00:00
parent f6c5be06b3
commit 60bc28829b
5 changed files with 53 additions and 3 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

@ -17,4 +17,9 @@ object Remove {
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 := {