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

Yitzchak Gale gale at sefer.org
Wed Dec 24 15:33:28 UTC 2014


Nikita Volkov wrote:
>>> 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

Roman Cheplyaka wrote:
> * guardp (for "predicate")
> * guardf (for "function")
> * guardWith
> I wouldn't be opposed to cond, either.

Oliver Charles wrote:
> I tend to use `pure` and `mfilter` for this case:
> \x -> fromMaybe 0 $ mfilter (> 0) $ pure (x - 10)

I reach for this all the time and find it lacking.
In an attempt to be merciful to some of my readers,
I avoid Ollie's very nice mfilter solution and refactor
instead.

+1

Regarding the name - all of the suggestions of
Nikita and Roman are fine with me. Let's just
build this bikeshed!

Thanks,
Yitz


More information about the Libraries mailing list