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

eitherToThrow #48

Open
phadej opened this issue Nov 4, 2015 · 5 comments
Open

eitherToThrow #48

phadej opened this issue Nov 4, 2015 · 5 comments

Comments

@phadej
Copy link
Contributor

phadej commented Nov 4, 2015

I often look for

eitherToThrow :: (MonadThrow m, Exception e) => Either e a -> m a
eitherToThrow (Right x) = pure x
eitherToThrow (Left e)  = throwM e

is it there already, in more general version?

There is eitherToError in either, but it's a bit different.

@ocharles
Copy link
Contributor

ocharles commented Oct 27, 2016

I usually call this liftEither and find it very useful.

Edit: actually my liftEither is much more like eitherToError, so maybe that's not a great name here.

@snoyberg
Copy link
Collaborator

I follow this pattern a lot via either throwM pure. My only concern is that the combinator doesn't add match help vs the three-word version I just provided.

@ocharles
Copy link
Contributor

It explicitly spells out what it's doing, rather than making me think. You can only parse either throwM pure so quickly because you have seen it so often, imo.

@jmcardon
Copy link

I'd like to submit a PR for this if you all are for it. I was looking for something around liftEither or fromEither and couldn't find it.

@domenkozar
Copy link

domenkozar commented Nov 15, 2018

Same here today. https://twitter.com/domenkozar/status/1063147657139503104

@roberth called it escalate:

escalate :: (Exception exc, MonadThrow m) => Either exc a -> m a
escalate = escalateAs id

escalateAs :: (Exception exc, MonadThrow m) => (l -> exc) -> Either l a -> m a
escalateAs f = either (throwM . f) pure

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 a pull request may close this issue.

5 participants