[Haskell-cafe] Missing common function for Maybe types
Marc Busqué
marc at lamarciana.com
Tue Jul 31 13:58:58 UTC 2018
On Tue, 31 Jul 2018, Nigel Rantor wrote:
>
> Your `foobar` function cannot work, it doesn't handle the case when the
> URL is neither http nor https.
>
> I would use catMaybes and headMay to work this out...untested code, and
> you'll need the `safe` library for headMay too...
>
> parse :: ByteString -> Maybe (Url Http, Option scheme)
> parse input = headMay $ catMaybes $ fmap input [parseUrlHttp, parseUrlHttps]
>
> And everything is lazy so if the input gets parsed correctly by the
> first call you never actually perform the second one....
Thanks Nigel! The problem is that the return type could be an `Url Http`
or an `Url Https`, so that wouldn't work. But an answer before by David
Feuer worked for my case:
```
boo :: Maybe a -> Maybe b -> Maybe (Either a b)
boo ma mb = (Left <$> ma) <|> (Right <$> mb)
```
Marc Busqué
http://waiting-for-dev.github.io/about/
>
> n
>
> On 31/07/18 08:07, Marc Busqué wrote:
>> Hi!
>>
>> I have two functions:
>>
>> ```
>> foo :: a -> Maybe b
>> bar :: a -> Maybe c
>> ```
>>
>> From which I want to build a higher order function:
>>
>> ```
>> foobar :: a -> (a -> Maybe b) -> (a -> Maybe c) -> Either b c
>> ```
>>
>> The implementation I need is:
>>
>> ```
>> foobar x f g =
>> case (f x) of
>> Nothing -> g x
>> Just y -> Just y
>> ```
>>
>> I'm a bit surprised that looking at hoogle I don't find a built-in
>> solution for this quite common need for `Maybe` types (or perhaps for
>> any monad).
>>
>> Am I looking in the wrong way? Does it exist a similar abstraction but
>> with a different shape?
>>
>> Thanks in advance,
>>
>> Marc Busqué
>> http://waiting-for-dev.github.io/about/
>>
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> To (un)subscribe, modify options or view archives go to:
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> Only members subscribed via the mailman list are allowed to post.
>>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
More information about the Haskell-Cafe
mailing list