Skip to content
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 orElse to ApplicativeError #2243

Merged
merged 4 commits into from
May 6, 2018
Merged

Add orElse to ApplicativeError #2243

merged 4 commits into from
May 6, 2018

Conversation

barambani
Copy link
Contributor

@barambani barambani commented May 3, 2018

This is to address #2242

@barambani barambani changed the title Added or to applicative error Add or to ApplicativeError May 3, 2018
kailuowang
kailuowang previously approved these changes May 3, 2018
Copy link
Contributor

@kailuowang kailuowang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's quick. Thanks!

@@ -70,4 +70,7 @@ final class ApplicativeErrorOps[F[_], E, A](val fa: F[A]) extends AnyVal {

def onError(pf: PartialFunction[E, F[Unit]])(implicit F: ApplicativeError[F, E]): F[A] =
F.onError(fa)(pf)

def or(other: F[A])(implicit F: ApplicativeError[F, E]): F[A] =
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't you want other to be lazy?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I’d be +1 in making it lazy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very true, I will change it. Thanks

@codecov-io
Copy link

codecov-io commented May 3, 2018

Codecov Report

Merging #2243 into master will decrease coverage by 0.1%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2243      +/-   ##
==========================================
- Coverage   95.05%   94.95%   -0.11%     
==========================================
  Files         333      333              
  Lines        5789     5788       -1     
  Branches      211      221      +10     
==========================================
- Hits         5503     5496       -7     
- Misses        286      292       +6
Impacted Files Coverage Δ
.../src/main/scala/cats/syntax/applicativeError.scala 100% <100%> (ø) ⬆️
core/src/main/scala/cats/data/Tuple2K.scala 90.69% <0%> (-4.66%) ⬇️
core/src/main/scala/cats/data/EitherK.scala 93.47% <0%> (-4.35%) ⬇️
core/src/main/scala/cats/data/Nested.scala 94.28% <0%> (-1.43%) ⬇️
core/src/main/scala/cats/data/Kleisli.scala 96.9% <0%> (-1.04%) ⬇️
...in/scala/cats/data/IndexedReaderWriterStateT.scala 99.04% <0%> (-0.02%) ⬇️
core/src/main/scala/cats/data/EitherT.scala 97.65% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 49374a4...d62b3b2. Read the comment docs.

@rossabaker
Copy link
Member

Either, EitherT, and Validated call this orElse. OptionT has no ApplicativeError, but calls a similar operation orElse. Might that be a better name here?

@barambani
Copy link
Contributor Author

changing it to orElse. Thanks

@barambani barambani changed the title Add or to ApplicativeError Add orElse to ApplicativeError May 3, 2018
@barambani
Copy link
Contributor Author

Could we run the CI again ? Thanks

@LukaJCB LukaJCB self-requested a review May 3, 2018 17:05
test("or leaves unchanged a success") {
17.some or None should === (Some(17))
test("orElse leaves a success unchanged") {
17.some orElse None should === (Some(17))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is only testing the the Option.orElsemethod.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paris_tuileries_garden_facepalm_statue

@barambani
Copy link
Contributor Author

I added a wrapper for option with its ApplicativeError instance as I couldn't find anything with one that didn't also have an orElse method. I know that there is Future but that makes more complex the comparison.
I'm happy to change it if there is something simpler though.

@@ -35,5 +35,31 @@ class ApplicativeErrorSuite extends CatsSuite {
failed.recoverWith { case _ => Some(7) } should === (Some(7))
}

{
final case class OptionWrapper[A](option: Option[A])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fix. Just FYI (not saying we should introduce another dependency in cats), with https://github.com/estatico/scala-newtype, all these can be written as

@newtype case class OptionWrapper[A](option: Option[A])
object OptionWrapper {
    implicit def ae[E](implicit ev: ApplicativeError[Option, E]): ApplicativeError[OptionWrapper, E] = ev.asInstanceOf
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, yes, I would love to use it 😄 , very compact indeed. May be in the near future ... 😃

Copy link
Contributor

@johnynek johnynek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this!

@barambani
Copy link
Contributor Author

👍 thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants