[Haskell-cafe] Missing common function for Maybe types

Marc Busqué marc at lamarciana.com
Tue Jul 31 11:55:48 UTC 2018


On Tue, 31 Jul 2018, Paul wrote:

> But alternative does not apply function to first Nothing. `maybe` can exec function on Nothing or apply another one on Just.
> 
> maybe (g x) id (f x) ?

As in the alternative case, it seems to be a good fit when there is only
one type involved, but it is two in this case.

Marc Busqué
http://waiting-for-dev.github.io/about/
> 
> 
> 31.07.2018 11:10, Imants Cekusins wrotes:
>       Salut Marc,
> Are you looking for Alternative by any chance:
> http://hackage.haskell.org/package/base-4.11.1.0/docs/Control-Applicative.html#t:Alternative
> 
> 
> Prelude> :m Control.Applicative
> Prelude Control.Applicative> Just 1 <|> Just 2::Maybe Int
> Just 1
> Prelude Control.Applicative> Nothing <|> Just 2::Maybe Int
> Just 2
> 
> 
> 
> 
> On 31 July 2018 at 10:47, Marc Busqué <marc at lamarciana.com> wrote:
>       Apologies, I clearly asked the question in a rushed way...
>
>       I'm working with [req](http://hackage.haskell.org/package/req) package.
>       I need to parse a url from a string, but I don't know whether its schema
>       will be `http` or `https`. However, `req` just provides me with two
>       functions:
>
>       ```
>       parseUrlHttp :: ByteString -> Maybe (Url Http, Option scheme)
>       parseUrlHttps :: ByteString -> Maybe (Url Https, Option scheme)
>       ```
>
>       As I don't know the schema beforehand, I have to apply one of the two
>       functions, do a case analysis on its result and, depending on the
>       result, call the second one or return the first result.
>
>       What I think is a common case here (perhaps I'm wrong) is the need to
>       choose between two or more `Maybe` values where at most one of them will
>       be a `Just`. `maybe` does not do that. `catMaybes` could be used for
>       that when all the `Maybe` have the same inner type, but it is not the
>       exact abstraction and it would need more code (just like simply doing a
>       case analysis).
>
>       Thanks and, again, sorry for the hurried question before (I don't like
>       when I see it from others :()
>
>       Marc Busqué
>       http://waiting-for-dev.github.io/about/
>
>       On Tue, 31 Jul 2018, Paul wrote:
> 
>
>             Something like "maybe" function?
> 
>
>             31.07.2018 10:07, Marc Busqué wrotes:
>                   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