Discussion: add more idiomatic versions of catchJust and/or handleJust

David Feuer david.feuer at gmail.com
Tue Jul 12 00:23:53 UTC 2016


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".
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20160711/b8430690/attachment.html>


More information about the Libraries mailing list