-
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
Merged
Shadowfiend
merged 15 commits into
lift:master
from
Bhashit:issue-1856-box-combinator-and-other-improvements
Sep 25, 2017
Merged
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e2150e7
Add collect and mapFailure methods. WIP
Bhashit f9ecab8
Remove duplicated collect method
Bhashit ba011d3
Tests for the mapFailure method
Bhashit 5c607f9
change mapFailure tests to use a valid Failure param when testing wit…
Bhashit 42bbf08
Change the collect function to be a bit more readable. Add tests for …
Bhashit a5b3972
Corrected spacing
Bhashit 2cc1ac8
Initial pass at `collectFailure` method
Bhashit b797621
Merge branch 'master' into issue-1856-box-combinator-and-other-improv…
Bhashit a10b78e
Correction in collectFailure method signature.
Bhashit d90ccff
Redefine the `collect` method to use an existing `apply` function.
Bhashit 52e6f17
Implement `flip` to take a partial-function.
Bhashit 833abfa
Remvoed a typo
Bhashit 94cc62c
Implement `transform` method to transform a Box into another Box.
Bhashit f504a82
Rollback 'or' to use its own simpler implementation.
Bhashit 783bf6e
Put backticks around the method-name links
Bhashit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.