-
Notifications
You must be signed in to change notification settings - Fork 276
Add transform and flip methods. #1864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
e2150e7
f9ecab8
ba011d3
5c607f9
42bbf08
a5b3972
2cc1ac8
b797621
a10b78e
d90ccff
52e6f17
833abfa
94cc62c
f504a82
783bf6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -516,6 +516,30 @@ sealed abstract class Box[+A] extends Product with Serializable{ | |
| */ | ||
| def foreach[U](f: A => U): Unit = {} | ||
|
|
||
| /** | ||
| * If this box is a `Failure`, returns a `Failure` box that results from passing this box | ||
| * to `fn`. Otherwise, returns this box. Used for transforming Failures without having to | ||
| * cover all three cases of Box, which is required by `map`. | ||
| * | ||
| * `map` is only applied if the box is full, `mapFailure` is only applied if this box is a `Failure`. | ||
| * | ||
| * @example {{{ | ||
| * // Returns `Failure("Changed failure")` because this box is a failure. | ||
| * Failure("Original Failure") mapFailure { failed => Failure("Changed failure") } | ||
| * | ||
| * // Returns Full("some-value") on which it was called | ||
| * Full("some-value") mapFailure { failure => Failure("Changed failure") } | ||
| * | ||
| * // Returns this Empty instance | ||
| * Empty mapFailure { case value => value } | ||
| * }}} | ||
| * | ||
| * @param fn the function that will be used to transform the Failure if this box is a failure. | ||
| * @return A `Failure` instance that results from applying `fn` if this box is a `Failure`, otherwise | ||
| * the same instance on which it was called. | ||
| */ | ||
| def mapFailure(fn: Failure => Failure): Box[A] = this | ||
|
|
||
| /** | ||
| * If this box is `Full` and contains an object of type `B`, returns a `Full` | ||
| * of type `Box[B]`. Otherwise, returns `Empty`. | ||
|
|
@@ -773,11 +797,7 @@ sealed abstract class Box[+A] extends Product with Serializable{ | |
| * If the partial function is defined at the current Box's value, apply the | ||
| * partial function. | ||
| */ | ||
| final def collect[B](pf: PartialFunction[A, B]): Box[B] = { | ||
| flatMap(value => | ||
| if (pf.isDefinedAt(value)) Full(pf(value)) | ||
| else Empty) | ||
| } | ||
| final def collect[B](pf: PartialFunction[A, B]): Box[B] = filter(pf.isDefinedAt).map(pf) | ||
|
|
||
| /** | ||
| * An alias for `collect`. | ||
|
|
@@ -788,6 +808,11 @@ sealed abstract class Box[+A] extends Product with Serializable{ | |
| final def collectFirst[B](pf: PartialFunction[A, B]): Box[B] = { | ||
| collect(pf) | ||
| } | ||
|
|
||
| final def collectFailure[B](pf: PartialFunction[Failure, Failure]): Box[Failure] = this match { | ||
| case f: Failure if pf.isDefinedAt(f) => Full(pf(f)) | ||
| case _ => Empty | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels like this should be a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was my first instinct as well. Almost sent a comment asking about these two signatures. I think I'll go with that one then. Now that you said it, it makes more sense. Convert specific cases of Failure into something else. |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -904,6 +929,8 @@ sealed case class Failure(msg: String, exception: Box[Throwable], chain: Box[Fai | |
|
|
||
| override def map[B](f: A => B): Box[B] = this | ||
|
|
||
| override def mapFailure(f: (Failure) => Failure): Box[A] = f(this) | ||
|
|
||
| override def flatMap[B](f: A => Box[B]): Box[B] = this | ||
|
|
||
| override def isA[B](cls: Class[B]): Box[B] = this | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,8 +67,8 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator { | |
| Full(1) reduceLeft {(x: Int, y: Int) => x + y} must_== 1 | ||
| } | ||
| "be used as an Option" in { | ||
| Full(1) orElse Some(2) must_== Some(1) | ||
| Empty orElse Some(2) must_== Some(2) | ||
| Full(1) orElse Some(2) must beSome(1) | ||
| Empty orElse Some(2) must beSome(2) | ||
| } | ||
| "be implicitly defined from an Option. The openOrThrowException method can be used on an Option for example" in { | ||
| Some(1).openOrThrowException("This is a test") must_== 1 | ||
|
|
@@ -152,14 +152,26 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator { | |
| Full(Empty).flatten must_== Empty | ||
| } | ||
| } | ||
| "define a 'mapFailure' method returning itself." in { | ||
| val failure = Failure("new-error", Empty, Empty) | ||
| Full(1).mapFailure(_ => failure) must_== Full(1) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it's a duplicate of one below?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh wait jk I just remembered how this spec is structured heh. |
||
| "define a 'collect' method that takes a PartialFunction to transform its contents" in { | ||
| "If the partial-function is defined for the contents of this box, returns a full box containing the result of applying that partial function to this Box's contents" in { | ||
| Full("Albus") collect { case "Albus" => "Dumbledore"} must_== Full("Dumbledore") | ||
| } | ||
| "If the partial-function is not defined for the contents of this box, returns Empty" in { | ||
| Full("Hermione") collect { case "Albus" => "Dumbledore"} must beEmpty | ||
| } | ||
| } | ||
| "define an 'elements' method returning an iterator containing its value" in { | ||
| Full(1).elements.next must_== 1 | ||
| } | ||
| "define a 'toList' method returning a List containing its value" in { | ||
| Full(1).toList must_== List(1) | ||
| } | ||
| "define a 'toOption' method returning a Some object containing its value" in { | ||
| Full(1).toOption must_== Some(1) | ||
| Full(1).toOption must beSome(1) | ||
| } | ||
| "return itself if asked for its status with the operator ?~" in { | ||
| Full(1) ?~ "error" must_== Full(1) | ||
|
|
@@ -298,14 +310,21 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator { | |
| "define a 'flatten' method returning Empty" in { | ||
| Empty.flatten must beEmpty | ||
| } | ||
| "define a 'mapFailure' method returning itself." in { | ||
| val failure = Failure("new-error", Empty, Empty) | ||
| Empty.mapFailure(_ => failure) must beEmpty | ||
| } | ||
| "define a 'collect' method returning Empty" in { | ||
| Empty collect { case _ => "Some Value" } must beEmpty | ||
| } | ||
| "define an 'elements' method returning an empty iterator" in { | ||
| Empty.elements.hasNext must beFalse | ||
| } | ||
| "define a 'toList' method returning Nil" in { | ||
| Empty.toList must_== Nil | ||
| } | ||
| "define a 'toOption' method returning None" in { | ||
| Empty.toOption must_== None | ||
| Empty.toOption must beNone | ||
| } | ||
| "return a failure with a message if asked for its status with the operator ?~" in { | ||
| Empty ?~ "nothing" must_== Failure("nothing", Empty, Empty) | ||
|
|
@@ -358,7 +377,14 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator { | |
| Failure("error", Empty, Empty) flatMap {x: String => Full(x.toString)} must_== Failure("error", Empty, Empty) | ||
| Failure("error", Empty, Empty).flatten must_== Failure("error", Empty, Empty) | ||
| } | ||
| "return a itself when asked for its status with the operator ?~" in { | ||
| "define a 'mapFailure' method that transforms it into another Failure instance." in { | ||
| val exception = new Exception("transformed") | ||
| Failure("error", Empty, Empty) mapFailure { _ => Failure("new-error", Full(exception), Empty) } must_== Failure("new-error", Full(exception), Empty) | ||
| } | ||
| "define a 'collect' method returning itself" in { | ||
| Failure("error", Empty, Empty) collect { case _ => "Some Value" } must_== Failure("error", Empty, Empty) | ||
| } | ||
| "return itself when asked for its status with the operator ?~" in { | ||
| Failure("error", Empty, Empty) ?~ "nothing" must_== Failure("error", Empty, Empty) | ||
| } | ||
| "create a new failure with a chained message if asked for its status with the operator ?~!" in { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Shadowfiend thought this was more readable than
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do think this version will result in a double-application of the
PartialFunctionguards/pattern matches (a problem that the previous version in the repo also shared):So while I agree this is slightly more readable, I think you nailed it with the original alternative formulation.