Maybe a -> Maybe b

Hello, I have this data Proxy = Proxy String And I want to write a function f :: Maybe Proxy -> Maybe String f ma = case ma of (Just (Proxy a)) -> Just a Nothing -> Nothing I was wondering if there is no simpler way to do this ? thanks Frederic

PICCA Frederic-Emmanuel wrote (2017-08-25 08:05:34):
Hello, I have this
data Proxy = Proxy String
And I want to write a function
f :: Maybe Proxy -> Maybe String f ma = case ma of (Just (Proxy a)) -> Just a Nothing -> Nothing
I was wondering if there is no simpler way to do this ?
Maybe implements the Functor type class (as an exercise, you can try to figure out the implementation), therefore you can simply use fmap, which in the case of Maybe, has type (a -> b) -> Maybe a -> Maybe b You just need a function to "unwrap" the String inside of the Proxy, and you're done. -- Olivier

data Proxy a = Proxy a exProxy::Functor f => f (Proxy a) -> f a exProxy = (un1 <$>) where un1 (Proxy a1) = a1 nice :)
participants (3)
-
Imants Cekusins
-
Olivier Iffrig
-
PICCA Frederic-Emmanuel