Maybe a -> Maybe b

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Maybe a -> Maybe b

PICCA Frederic-Emmanuel
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
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Maybe a -> Maybe b

Olivier Iffrig
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

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Maybe a -> Maybe b

PICCA Frederic-Emmanuel
> 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

Thanks I reallized this after sending the email

thanks

Frederic
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Maybe a -> Maybe b

Imants Cekusins
as an exercise, you can try to figure out the implementation

.. time is up :-P 
Here is an answer for slightly more generic Proxy and f:


data Proxy a = Proxy a

exProxy::Functor f =>
     f (Proxy a) -> f a
exProxy = (un1 <$>)
    where un1 (Proxy a1) = a1


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Maybe a -> Maybe b

PICCA Frederic-Emmanuel
data Proxy a = Proxy a

exProxy::Functor f =>
     f (Proxy a) -> f a
exProxy = (un1 <$>)
    where un1 (Proxy a1) = a1


nice :)

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners