darcs patch: Add justIf to Data.Maybe

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Sat Aug 22 11:22:40 EDT 2009


Duncan Coutts <duncan.coutts at worc.ox.ac.uk> writes:

> On Sat, 2009-08-22 at 10:18 +0200, Joachim Breitner wrote:
>> Hi,
>> 
>> I propose the attached change
>
>> Fri Aug 21 19:17:12 CEST 2009  Joachim Breitner <mail at joachim-breitner.de>
>>   * Add justIf to Data.Maybe

> As Jón was suggesting we might want to generalise to MonadPlus rather
> than putting it in Data.Maybe, it becomes rather similar to the existing
> function 'guard':

[...] some alpha conversion
>
> checkA :: MonadPlus m => Bool -> a -> m a
> checkA True  x  = return x
> checkA False _  = mzero
>
> Using a Bool for the condition is a tad more general than a predicate
> like:
>
> checkB :: MonadPlus m => (a -> Bool) -> a -> m a

I'd say the generality comparison goes the other way:

> In particular, the use case in Cabal does not fit the predicate pattern
> because the condition is not a condition on the value being returned but
> on something else in the environment.

But you can write any 

   checkA e
as
   checkB $ const e

whereas a function like

   all_spaces = checkB (all . isSpace)

is hard to write using checkA (suppose you are doing something like “map
(checkB predicate)”) since checkA doesn't look at the second argument.

> BTW, I also don't want to take too strong a position on the name ;-)

me neither, but check (all . isOK) reads nicely ;-)

-- 
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk
http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html  (updated 2009-01-31)



More information about the Libraries mailing list