Skip to content

Commit a2de25d

Browse files
authored
Merge pull request scala#7379 from nasadorian/either-filterToOption
Rename Either's `filter` method to `filterToOption`
2 parents 9fc4983 + b0f499c commit a2de25d

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/library/scala/util/Either.scala

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,11 +621,26 @@ object Either {
621621
* Right(12).left.filter(_ > 10) // None
622622
* }}}
623623
*/
624+
@deprecated("Use `filterToOption`, which more accurately reflects the return type", "2.13.0")
624625
def filter[B1](p: A => Boolean): Option[Either[A, B1]] = e match {
625626
case x @ Left(a) if p(a) => Some(x.asInstanceOf[Either[A, B1]])
626627
case _ => None
627628
}
628629

630+
/** Returns `None` if this is a `Right` or if the given predicate
631+
* `p` does not hold for the left value, otherwise, returns a `Left`.
632+
*
633+
* {{{
634+
* Left(12).left.filterToOption(_ > 10) // Some(Left(12))
635+
* Left(7).left.filterToOption(_ > 10) // None
636+
* Right(12).left.filterToOption(_ > 10) // None
637+
* }}}
638+
*/
639+
def filterToOption[B1](p: A => Boolean): Option[Either[A, B1]] = e match {
640+
case x @ Left(a) if p(a) => Some(x.asInstanceOf[Either[A, B1]])
641+
case _ => None
642+
}
643+
629644
/** Returns a `Seq` containing the `Left` value if it exists or an empty
630645
* `Seq` if this is a `Right`.
631646
*
@@ -764,11 +779,27 @@ object Either {
764779
* Left(12).right.filter(_ > 10) // None
765780
* }}}
766781
*/
782+
@deprecated("Use `filterToOption`, which more accurately reflects the return type", "2.13.0")
767783
def filter[A1](p: B => Boolean): Option[Either[A1, B]] = e match {
768784
case Right(b) if p(b) => Some(Right(b))
769785
case _ => None
770786
}
771-
787+
788+
/** Returns `None` if this is a `Left` or if the
789+
* given predicate `p` does not hold for the right value,
790+
* otherwise, returns a `Right`.
791+
*
792+
* {{{
793+
* Right(12).right.filterToOption(_ > 10) // Some(Right(12))
794+
* Right(7).right.filterToOption(_ > 10) // None
795+
* Left(12).right.filterToOption(_ > 10) // None
796+
* }}}
797+
*/
798+
def filterToOption[A1](p: B => Boolean): Option[Either[A1, B]] = e match {
799+
case r @ Right(b) if p(b) => Some(r.asInstanceOf[Either[A1, B]])
800+
case _ => None
801+
}
802+
772803
/** Returns a `Seq` containing the `Right` value if
773804
* it exists or an empty `Seq` if this is a `Left`.
774805
*

test/scalacheck/CheckEither.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ object CheckEitherTest extends Properties("Either") {
5252
def g(s: String) = s.reverse
5353
e.left.map(x => f(g(x))) == e.left.map(x => g(x)).left.map(f(_))})
5454

55-
val prop_filter = forAll((e: Either[Int, Int], x: Int) => e.left.filter(_ % 2 == 0) ==
55+
val prop_filterToOption = forAll((e: Either[Int, Int], x: Int) => e.left.filterToOption(_ % 2 == 0) ==
5656
(if(e.isRight || e.left.get % 2 != 0) None else Some(e)))
5757

5858
val prop_seq = forAll((e: Either[Int, Int]) => e.left.toSeq == (e match {
@@ -98,7 +98,7 @@ object CheckEitherTest extends Properties("Either") {
9898
def g(s: String) = s.reverse
9999
e.right.map(x => f(g(x))) == e.right.map(x => g(x)).right.map(f(_))})
100100

101-
val prop_filter = forAll((e: Either[Int, Int], x: Int) => e.right.filter(_ % 2 == 0) ==
101+
val prop_filterToOption = forAll((e: Either[Int, Int], x: Int) => e.right.filterToOption(_ % 2 == 0) ==
102102
(if(e.isLeft || e.right.get % 2 != 0) None else Some(e)))
103103

104104
val prop_seq = forAll((e: Either[Int, Int]) => e.right.toSeq == (e match {
@@ -202,7 +202,7 @@ object CheckEitherTest extends Properties("Either") {
202202
("Left.prop_flatMapComposition", CheckLeftProjection.prop_flatMapComposition),
203203
("Left.prop_mapIdentity", CheckLeftProjection.prop_mapIdentity),
204204
("Left.prop_mapComposition", CheckLeftProjection.prop_mapComposition),
205-
("Left.prop_filter", CheckLeftProjection.prop_filter),
205+
("Left.prop_filterToOption", CheckLeftProjection.prop_filterToOption),
206206
("Left.prop_seq", CheckLeftProjection.prop_seq),
207207
("Left.prop_option", CheckLeftProjection.prop_option),
208208
("Right.prop_value", CheckRightProjection.prop_value),
@@ -214,7 +214,7 @@ object CheckEitherTest extends Properties("Either") {
214214
("Right.prop_flatMapComposition", CheckRightProjection.prop_flatMapComposition),
215215
("Right.prop_mapIdentity", CheckRightProjection.prop_mapIdentity),
216216
("Right.prop_mapComposition", CheckRightProjection.prop_mapComposition),
217-
("Right.prop_filter", CheckRightProjection.prop_filter),
217+
("Right.prop_filterToOption", CheckRightProjection.prop_filterToOption),
218218
("Right.prop_seq", CheckRightProjection.prop_seq),
219219
("Right.prop_option", CheckRightProjection.prop_option),
220220
("prop_Either_left", prop_Either_left),

0 commit comments

Comments
 (0)