-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add reject
(partial function) method to MonadError
#2194
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
@@ -1,36 +1,59 @@ | ||
package cats | ||
package tests | ||
|
||
import scala.util.{Failure, Success, Try} | ||
|
||
class MonadErrorSuite extends CatsSuite { | ||
|
||
val successful: Option[Int] = 42.some | ||
val failed: Option[Int] = None | ||
implicit val eqThrow: Eq[Throwable] = Eq.fromUniversalEquals | ||
|
||
val successful: Try[Int] = Success(42) | ||
val failedValue: Throwable = new IllegalArgumentException("default failure") | ||
val otherValue: Throwable = new IllegalStateException("other failure") | ||
val failed: Try[Int] = Failure(failedValue) | ||
|
||
test("ensure raises an error if the predicate fails") { | ||
successful.ensure(())(i => false) should === (None) | ||
successful.ensure(failedValue)(_ => false) should === (failed) | ||
} | ||
|
||
test("ensure returns the successful value if the predicate succeeds") { | ||
successful.ensure(())(i => true) should === (successful) | ||
successful.ensure(failedValue)(_ => true) should === (successful) | ||
} | ||
|
||
test("ensure returns the failure, when applied to a failure") { | ||
failed.ensure(())(i => false) should === (failed) | ||
failed.ensure(())(i => true) should === (failed) | ||
test("ensure returns the original failure, when applied to a failure") { | ||
failed.ensure(otherValue)(_ => false) should === (failed) | ||
failed.ensure(otherValue)(_ => true) should === (failed) | ||
} | ||
|
||
test("ensureOr raises an error if the predicate fails") { | ||
successful.ensureOr(_ => ())(_ => false) should === (None) | ||
successful.ensureOr(_ => failedValue)(_ => false) should === (failed) | ||
} | ||
|
||
test("ensureOr returns the successful value if the predicate succeeds") { | ||
successful.ensureOr(_ => ())(_ => true) should === (successful) | ||
successful.ensureOr(_ => failedValue)(_ => true) should === (successful) | ||
} | ||
|
||
test("ensureOr returns the original failure, when applied to a failure") { | ||
failed.ensureOr(_ => otherValue)(_ => false) should === (failed) | ||
failed.ensureOr(_ => otherValue)(_ => true) should === (failed) | ||
} | ||
|
||
test("ensureOr returns the failure, when applied to a failure") { | ||
failed.ensureOr(_ => ())(_ => false) should === (failed) | ||
failed.ensureOr(_ => ())(_ => true) should === (failed) | ||
test("ensureP returns the successful value if the partial function is not defined") { | ||
successful.reject { | ||
case i if i < 0 => failedValue | ||
} should === (successful) | ||
} | ||
|
||
test("ensureP returns the original failure, when applied to a failure") { | ||
failed.reject { | ||
case i if i < 0 => otherValue | ||
} should === (failed) | ||
} | ||
|
||
test("ensureP raises an error if the partial function is defined") { | ||
successful.reject { | ||
case i if i > 0 => failedValue | ||
} should === (failed) | ||
} | ||
|
||
} |
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.
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.
There is this trick that avoids calling
isDefinedAt
and thenapply
of thePartialFunction
, maybe worthwhile to copy here, just a thought. Also, instead ofF.pure(a)
shall we just returnfa
?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'm thinking of doing
F.flatMap(fa)(a => pf.andThen(F.raiseError[A]).applyOrElse(a, _ => fa))
, though that does create a new "AndThen" object. Is avoiding the allocation important?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.
that might be a good balance between readability and performance.