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 toNestedValidated and toNestedValidatedNel #1408

Merged
merged 3 commits into from
Oct 25, 2016

Conversation

conniec
Copy link
Contributor

@conniec conniec commented Oct 17, 2016

Issue: #1344

This is my first time contributing and am fairly new to these types so any feedback welcome. Would also like to add some examples if the code looks about right.

@codecov-io
Copy link

codecov-io commented Oct 17, 2016

Current coverage is 91.68% (diff: 100%)

Merging #1408 into master will increase coverage by <.01%

@@             master      #1408   diff @@
==========================================
  Files           240        240          
  Lines          3608       3610     +2   
  Methods        3540       3544     +4   
  Messages          0          0          
  Branches         67         65     -2   
==========================================
+ Hits           3308       3310     +2   
  Misses          300        300          
  Partials          0          0          

Powered by Codecov. Last update fe026bc...9a678ff

@travisbrown travisbrown mentioned this pull request Oct 18, 2016
13 tasks
@non
Copy link
Contributor

non commented Oct 20, 2016

These seem good to me.

At a minimum we should have tests for both methods. It doesn't look like we have a tutorial for EitherT currently, but if we did have one adding these methods to it would be a good idea.

@travisbrown
Copy link
Contributor

👍, looks good to me with the tests.

@peterneyens
Copy link
Collaborator

peterneyens commented Oct 25, 2016

This looks good, thanks @conniec.

Maybe we can add the following example to toNestedValidatedNel, it shows the stereotypical example of error short circuiting versus error accumulation, but using EitherT and Nested / ValidatedNel. What do you think ?

import cats.data.{Nested, EitherT, ValidatedNel}
import cats.implicits._

type ErrorsOr[x] = ValidatedNel[String, x]
type OptionErrorsOr[x] = Nested[Option, ErrorsOr, x]

val ok = EitherT.right[Option, String, Int](Some(1))
val error = EitherT.left[Option, String, Int](Some("error"))
val none = EitherT[Option, String, Int](None)

val okV: OptionErrorsOr[Int] = ok.toNestedValidatedNel
val errorV: OptionErrorsOr[Int] = error.toNestedValidatedNel
val noneV: OptionErrorsOr[Int] = none.toNestedValidatedNel

(okV |@| okV |@| okV).map(_ + _ + _)
// OptionErrorsOr[Int] = Nested(Some(Valid(3)))

(okV |@| errorV |@| errorV).map(_ + _ + _)
// OptionErrorsOr[Int] = Nested(Some(Invalid(NonEmptyList(error, error))))

(ok |@| error |@| error).map(_ + _ + _)
// EitherT[Option,String,Int] = EitherT(Some(Left(error)))

(okV |@| errorV |@| noneV).map(_ + _ + _)
// OptionErrorsOr[Int] = Nested(None)

(ok |@| error |@| none).map(_ + _ + _)
// EitherT[Option,String,Int] = EitherT(Some(Left(error)))

This might even be something worth adding to the EitherT documentation when it gets written.

@travisbrown
Copy link
Contributor

@peterneyens Maybe we should open an issue for EitherT documentation and update the toNestedValidatedNel example based on what ends up happening there? I'd be happy to see this merged as-is for now.

@peterneyens
Copy link
Collaborator

You're right, see #1425.

@peterneyens peterneyens merged commit 800711b into typelevel:master Oct 25, 2016
@conniec
Copy link
Contributor Author

conniec commented Oct 25, 2016

Thanks for the help everyone!

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

Successfully merging this pull request may close these issues.

5 participants