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

Accumulate Err on Alt for Result type #140

Merged
merged 1 commit into from
Jul 29, 2017
Merged

Accumulate Err on Alt for Result type #140

merged 1 commit into from
Jul 29, 2017

Conversation

evilsoft
Copy link
Owner

Best Laid Plans

image

After messing about with Alt and trying to come up with helpers for Alternative and Alt, I was really sad that Result did not accumulate Err like it does with ap. It would be very useful and does not seem to break any laws.

So that is what this is. Now if you use a Semigroup for your Err and use alt it will concat your Errs. If you do not use a Semigroup, then alt behaves in a manner that works like Either.alt, in which it return the first Ok, but the last Err. Like so:

const withSemi =
  Err([ 3 ])
    .alt(Err([ 13 ]))
    .alt(Err([ 42 ]))

const noSemi =
  Err(3)
    .alt(Err(13))
    .alt(Err(42))

withSemi
// > Err [ 3, 13, 42 ]

withSemi
  .alt(Ok('yep'))
// > Ok 'yep'

noSemi
// > Err 42

noSemi
  .alt(Ok('yep'))
// > Ok 'yep'

@coveralls
Copy link

coveralls commented Jul 29, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 0d967c9 on result-alt-accum into 5de3309 on master.

@evilsoft
Copy link
Owner Author

image

@evilsoft evilsoft merged commit ee85d74 into master Jul 29, 2017
@evilsoft evilsoft deleted the result-alt-accum branch July 29, 2017 02:44
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.

2 participants