purescript/purescript-transformers

Add attempt

no-longer-on-githu-b opened this issue · 2 comments

I find this function very useful:

attempt :: forall m e a. MonadError e m => m a -> m (Either e a)
attempt a = (Right <$> a) `catchError` (pure <<< Left)

This makes it easier to "not catch more than you need". For example when you want to catch only exceptions thrown by a, and not by b, there is no reasonable way to do this without attempt:

attempt a >>= either e b

-- vs overly defensive

(a >>= b) `catchError` e
paf31 commented

This is the same as try from Control.Monad.Exception so I think we should call this try as well.