Discussion: add more idiomatic versions of catchJust and/or handleJust
Oleg Grenrus
oleg.grenrus at iki.fi
Thu Jul 14 18:06:01 UTC 2016
Maybe pointing out the obvious, but when used with lens, catchJust approach is handy:
— | here catchJust is from `exceptions` though
catching :: MonadCatch m => Getting (First a) SomeException a -> m r -> (a -> m r) -> m r
catching l = catchJust (preview l)
http://hackage.haskell.org/package/lens-4.14/docs/src/Control.Exception.Lens.html#catching
+/- 0 on catchMaybe
- Oleg
> On 12 Jul 2016, at 03:23, David Feuer <david.feuer at gmail.com> wrote:
>
> The catchJust and handleJust functions seem a bit weird and unidiomatic.
>
> catchJust
> :: Exception e
> => (e -> Maybe b) -- ^ Predicate to select exceptions
> -> IO a -- ^ Computation to run
> -> (b -> IO a) -- ^ Handler
> -> IO a
> catchJust p a handler = catch a handler'
> where handler' e = case p e of
> Nothing -> throwIO e
> Just b -> handler b
>
> This takes two functions and then puts them together. I would think the more natural API would be
>
> catchMaybe :: Exception e => IO a -> (e -> Maybe (IO a)) -> IO a
> catchMaybe m handler = catch m handler' where
> handler' e = fromMaybe (throwIO e) (handler e)
>
> This is exactly as powerful as catchJust:
>
> catchMaybe m handler = catchJust handler m id
> catchJust p m handler = catchMaybe m $ fmap handler . p
>
> But catchMaybe doesn't enforce the arbitrary separation between "selection" and "handling".
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 842 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20160714/31d426cc/attachment.sig>
More information about the Libraries
mailing list