Proposal: (a -> Bool) -> a -> f a
David Luposchainsky
dluposchainsky at googlemail.com
Sun Dec 21 14:30:07 UTC 2014
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 21.12.2014 11:54, 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
This reminds me of the commonly recommended, but wacky looking function
pureWhen :: MonadPlus m => Bool -> a -> m a
pureWhen = \p x -> x <$ guard p
which is (pure x) when p holds, and mzero otherwise. This is of course
easily generalizable to using Alternative.
Using your `cond`, one could express this as
pureWhen p = cond (const p)
which looks reasonable to me. I think this is an often used function in
one form or another, and would welcome it in the libraries.
+1
Greetings,
David/quchen
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
iQEcBAEBAgAGBQJUltluAAoJELrQsaT5WQUsKdEIAOMkOnFv2w2fe3snUYq9tFRe
xwWajQjxyowfNwFR4wdv1Z0PjDz1p8vM7EGGJ0cr6KtsxK4YsvOoylJwAMcIjUhQ
fzrdgYo+NodQaDO1BmUYrLW/UBZjKBgSxiz7XhRW4uA6mRc9Cvy6ljoMbixAXoPv
NG0jfLEduQf10Ro5tv+Glbdsbu7N0QDfo95y88c6cDFSAeYLECIEKxzNojrd89W+
8LQFRiDLo4U9EZVB839LCBiDT/ZCGNEdxIsMk5I/viR8kiKezlYLod6dp1gqK3jg
9KkaENxVzWkimoza3BhSf23OwICliFxJRv3wqEv2ZiweBUjN9xrNZ4d9euaSmtI=
=x54F
-----END PGP SIGNATURE-----
More information about the Libraries
mailing list