<p dir="ltr">+1 on catchMaybe from this corner of the peanut gallery, FWIW. It feels far more idiomatic, and provides the same power with fewer moving parts.</p>
<div class="gmail_quote">On Jul 13, 2016 1:14 PM, "David Feuer" <<a href="mailto:david.feuer@gmail.com">david.feuer@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I hate having to make arbitrary choices when writing code. With<br>
`catchJust`, I have to decide what to calculate in the selector and<br>
what to calculate in the handler. As far as I can tell, there's never<br>
any reason to leave any calculation for the handler.<br>
<br>
I don't think the `Maybe (IO a)` type is nearly as hard to think about<br>
as exceptions themselves are. The handler either provides a recovery<br>
action or it doesn't. The catchMaybe signature strikes me, personally,<br>
as easier to understand, because I don't need to use parametricity to<br>
string the pieces together.<br>
<br>
On Wed, Jul 13, 2016 at 3:36 PM, Andreas Abel <<a href="mailto:andreas.abel@ifi.lmu.de">andreas.abel@ifi.lmu.de</a>> wrote:<br>
> I can only guess why catchJust was designed like it is. A type like<br>
><br>
> b -> Maybe (IO a)<br>
><br>
> is not as intuitive as the types<br>
><br>
> e -> Maybe b<br>
> -- ^ if you do not understand this, get back to Haskell school!<br>
><br>
> b -> IO a<br>
> -- ^ a continuation, we know this from >>= and friends<br>
><br>
> A type like Maybe (IO a) is more unusual, requires more thinking.<br>
><br>
> +-0. I have no opinion on what is better.<br>
><br>
><br>
> On 12.07.2016 02:23, David Feuer wrote:<br>
>><br>
>> The catchJust and handleJust functions seem a bit weird and unidiomatic.<br>
>><br>
>> catchJust<br>
>> :: Exception e<br>
>> => (e -> Maybe b) -- ^ Predicate to select exceptions<br>
>> -> IO a -- ^ Computation to run<br>
>> -> (b -> IO a) -- ^ Handler<br>
>> -> IO a<br>
>> catchJust p a handler = catch a handler'<br>
>> where handler' e = case p e of<br>
>> Nothing -> throwIO e<br>
>> Just b -> handler b<br>
>><br>
>> This takes two functions and then puts them together. I would think the<br>
>> more natural API would be<br>
>><br>
>> catchMaybe :: Exception e => IO a -> (e -> Maybe (IO a)) -> IO a<br>
>> catchMaybe m handler = catch m handler' where<br>
>> handler' e = fromMaybe (throwIO e) (handler e)<br>
>><br>
>> This is exactly as powerful as catchJust:<br>
>><br>
>> catchMaybe m handler = catchJust handler m id<br>
>> catchJust p m handler = catchMaybe m $ fmap handler . p<br>
>><br>
>> But catchMaybe doesn't enforce the arbitrary separation between<br>
>> "selection" and "handling".<br>
>><br>
>><br>
>><br>
>> _______________________________________________<br>
>> Libraries mailing list<br>
>> <a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
>> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
>><br>
><br>
><br>
> --<br>
> Andreas Abel <>< Du bist der geliebte Mensch.<br>
><br>
> Department of Computer Science and Engineering<br>
> Chalmers and Gothenburg University, Sweden<br>
><br>
> <a href="mailto:andreas.abel@gu.se">andreas.abel@gu.se</a><br>
> <a href="http://www2.tcs.ifi.lmu.de/~abel/" rel="noreferrer" target="_blank">http://www2.tcs.ifi.lmu.de/~abel/</a><br>
_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
</blockquote></div>