Proposal: (a -> Bool) -> a -> f a
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: 1. The control flow is evident and goes from right to left step by step. It’s not scattered around like in the second example. 2. No need to interrupt to imagine a name for a temporary variable. 3. Less noise. Now, since it’s generalised to Alternative one can imagine tons of other useful cases for this. Alternative titles: - conditional - partial Best regards, Nikita
I like the idea. This is similar to guard (MonadPlus m => Bool -> m ()), so maybe exploit that similarity for naming. Suggestions: * guardp (for "predicate") * guardf (for "function") * guardWith I wouldn't be opposed to cond, either. On 21/12/14 12: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 |
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:
1.
The control flow is evident and goes from right to left step by step. It’s not scattered around like in the second example.
2.
No need to interrupt to imagine a name for a temporary variable.
3.
Less noise.
Now, since it’s generalised to |Alternative| one can imagine tons of other useful cases for this.
Alternative titles:
*
|conditional|
*
|partial|
Best regards, Nikita
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
I tend to use `pure` and `mfilter` for this case:
\x -> fromMaybe 0 $ mfilter (> 0) $ pure (x - 10)
-- ocharles
On Sun, Dec 21, 2014 at 10:54 AM, Nikita Volkov
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:
1.
The control flow is evident and goes from right to left step by step. It’s not scattered around like in the second example. 2.
No need to interrupt to imagine a name for a temporary variable. 3.
Less noise.
Now, since it’s generalised to Alternative one can imagine tons of other useful cases for this.
Alternative titles:
-
conditional -
partial
Best regards, Nikita
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
On 21.12.2014 12:28, Oliver Charles wrote:
I tend to use `pure` and `mfilter` for this case:
\x -> fromMaybe 0 $ mfilter (> 0) $ pure (x - 10)
That's what I thought as well. 'Alternative' seems like a bit of an overkill. cond works for everything that has 'empty' and is pointed (accommodates singletons). import Agda.Utils.Null import Agda.Utils.Singleton singletonWhen p a = if p a then singleton a else empty Cheers, Andreas
On Sun, Dec 21, 2014 at 10:54 AM, Nikita Volkov
mailto:nikita.y.volkov@gmail.com> 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 |
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:
1.
The control flow is evident and goes from right to left step by step. It’s not scattered around like in the second example.
2.
No need to interrupt to imagine a name for a temporary variable.
3.
Less noise.
Now, since it’s generalised to |Alternative| one can imagine tons of other useful cases for this.
Alternative titles:
*
|conditional|
*
|partial|
Best regards, Nikita
_______________________________________________ Libraries mailing list Libraries@haskell.org mailto:Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
-- Andreas Abel <>< Du bist der geliebte Mensch. Department of Computer Science and Engineering Chalmers and Gothenburg University, Sweden andreas.abel@gu.se http://www2.tcs.ifi.lmu.de/~abel/
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
I think "partial" is already used for Maybe somewhere, but cond sounds fine.
On Dec 24, 2014 10:33 AM, "Yitzchak Gale"
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 _______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
Or I guess partial could be changed. Whatever.
On Dec 24, 2014 10:33 AM, "Yitzchak Gale"
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 _______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
-----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-----
participants (7)
-
Andreas Abel -
David Feuer -
David Luposchainsky -
Nikita Volkov -
Oliver Charles -
Roman Cheplyaka -
Yitzchak Gale