Proposal: (a -> Bool) -> a -> f a

Nikita Volkov nikita.y.volkov at
Sun Dec 21 10:54:00 UTC 2014

I propose to add the following utility function into Control.Applicative:

cond :: (Alternative f) => (a -> Bool) -> a -> f a
cond p a = if p a then pure a else empty

Following is a typical use case:

\x -> fromMaybe 0 $ cond (> 0) $ x - 10

which is the same as

\x -> let y = x - 10 in if y > 0 then y else 0

Why the first one is better:


   The control flow is evident and goes from right to left step by step.
   It’s not scattered around like in the second example.

   No need to interrupt to imagine a name for a temporary variable.

   Less noise.

Now, since it’s generalised to Alternative one can imagine tons of other
useful cases for this.

Alternative titles:




Best regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Libraries mailing list