
Data.Maybe has functions for processing Maybe's but nothing useful for creating maybe. I think the following would be a very useful addition, a guarded function: guarded :: (a -> Bool) -> (a -> b) -> a -> Maybe b guarded p f x | p x = Just (f x) | otherwise = Nothing such a function in the std libs would make functions like "unfoldr" more attractive -- uses of foldr nearly always encapsulate this notion. Tim Newsham http://www.thenewsh.com/~newsham/

On 7 Nov 2007, at 12:40 PM, Tim Newsham wrote:
Data.Maybe has functions for processing Maybe's but nothing useful for creating maybe. I think the following would be a very useful addition, a guarded function:
guarded :: (a -> Bool) -> (a -> b) -> a -> Maybe b guarded p f x | p x = Just (f x) | otherwise = Nothing
such a function in the std libs would make functions like "unfoldr" more attractive -- uses of foldr nearly always encapsulate this notion.
guarded p f x = guard (p x) >> f x which I would expect looks even better in practice: [1..10] = unfoldr (\ n -> guard (n <= 10) >> return (n, n + 1)) 1 vs. [1..10] = unfoldr (guarded (<= 10) (\ n -> return (n, n + 1)) 1 For example. jcc

On 11/8/07, Tim Newsham
Data.Maybe has functions for processing Maybe's but nothing useful for creating maybe. I think the following would be a very useful addition, a guarded function:
guarded :: (a -> Bool) -> (a -> b) -> a -> Maybe b guarded p f x | p x = Just (f x) | otherwise = Nothing
such a function in the std libs would make functions like "unfoldr" more attractive -- uses of foldr nearly always encapsulate this notion.
How about this variant: ensure :: (MonadPlus m) => (a -> Bool) -> a -> m a ensure p x | p x = return x | otherwise = mzero Which as Jonathan points out, could also be written: ensure p x = guard (p x) >> return x Now we can define guarded p f x = ensure p x >>> fmap f Stuart

On Thu, 8 Nov 2007, Stuart Cook wrote:
On 11/8/07, Tim Newsham
wrote: Data.Maybe has functions for processing Maybe's but nothing useful for creating maybe. I think the following would be a very useful addition, a guarded function:
guarded :: (a -> Bool) -> (a -> b) -> a -> Maybe b guarded p f x | p x = Just (f x) | otherwise = Nothing
such a function in the std libs would make functions like "unfoldr" more attractive -- uses of foldr nearly always encapsulate this notion.
How about this variant:
ensure :: (MonadPlus m) => (a -> Bool) -> a -> m a ensure p x | p x = return x | otherwise = mzero
Which as Jonathan points out, could also be written:
ensure p x = guard (p x) >> return x
Now we can define
guarded p f x = ensure p x >>> fmap f
My 'toMaybe' is a specialisation of '\b x -> guard b >> return x'. This let us return to the discussion, whether there should be a specialised function, if there is a general function which does the same. http://www.haskell.org/haskellwiki/Simple_to_complex#Type_class_methods I use 'toMaybe' a lot, because it allows partial application and I have seen library code, where 'toMaybe' could have been used, but its author didn't seem to know or like 'guard b >> return x'.

guarded = liftM2 ((>>) . guard) toMaybe = (. return) . (>>) . guard Regards, lambdabot :)

On Wed, 7 Nov 2007, Tim Newsham wrote:
Data.Maybe has functions for processing Maybe's but nothing useful for creating maybe. I think the following would be a very useful addition, a guarded function:
guarded :: (a -> Bool) -> (a -> b) -> a -> Maybe b guarded p f x | p x = Just (f x) | otherwise = Nothing
such a function in the std libs would make functions like "unfoldr" more attractive -- uses of foldr nearly always encapsulate this notion.
This would be guarded p f x = toMaybe (p x) (f x) in terms of my beloved but already rejected 'toMaybe': http://www.haskell.org/pipermail/libraries/2004-July/002326.html
participants (5)
-
Henning Thielemann
-
Jonathan Cast
-
Stuart Cook
-
Tim Newsham
-
Yitzchak Gale