Missing common function for Maybe types

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

Missing common function for Maybe types

Marc Busqué
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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

David Feuer
Your code doesn't type check and there is no way to write a total function with the type of foobar. What are you actually trying to do? Why do you say this is "common"?

On Tue, Jul 31, 2018, 3:07 AM Marc Busqué <[hidden email]> 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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Paul
In reply to this post by Marc Busqué

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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Marc Busqué
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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Imants Cekusins
Salut Marc,

Are you looking for Alternative by any chance:
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é <[hidden email]> 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.


_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Paul

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) ?


31.07.2018 11:10, Imants Cekusins wrotes:
Salut Marc,

Are you looking for Alternative by any chance:
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é <[hidden email]> 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.



_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Imants Cekusins
> alternative does not apply function to first Nothing

<|> or other functions from Alternative may not be the entire function to solve Marc's problem.

It is a possibly useful function that is a bit tricky to find. ;)


On 31 July 2018 at 11:26, Paul <[hidden email]> 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) ?


31.07.2018 11:10, Imants Cekusins wrotes:
Salut Marc,

Are you looking for Alternative by any chance:
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é <[hidden email]> 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.




_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

David Feuer
In reply to this post by Marc Busqué
I think you're probably after something like

boo :: Maybe a -> Maybe b -> Maybe (Either a b)
boo ma mb = (Left <$> ma) <|> (Right <$> mb)

On Tue, Jul 31, 2018, 3:47 AM Marc Busqué <[hidden email]> 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.

_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Tom Ellis-5
In reply to this post by Marc Busqué
On Tue, Jul 31, 2018 at 09:47:07AM +0200, Marc Busqué 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)
> ```

This just sounds like bad API design from req.  Why is the API not

    parseUrl :: ByteString -> Maybe (Either (Url Http) (Url Https), Option scheme)

> 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`.

If that invariant really does hold then the authors of those functions
should encode the invariant in the type. Requiring clients to observe the
invariant for themselves sounds like a bad idea.

Tom
_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Vanessa McHale
In reply to this post by Marc Busqué

That doesn't exactly make sense. You'd need two versions of that function in all likelihood - one left-biased and another right-biased.


On 07/31/2018 02:07 AM, 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.

--



Vanessa McHale
Functional Compiler Engineer | Chicago, IL

Website: www.iohk.io
Twitter: @vamchale
PGP Key ID: 4209B7B5

Input
          Output

Twitter Github LinkedIn


This e-mail and any file transmitted with it are confidential and intended solely for the use of the recipient(s) to whom it is addressed. Dissemination, distribution, and/or copying of the transmission by anyone other than the intended recipient(s) is prohibited. If you have received this transmission in error please notify IOHK immediately and delete it from your system. E-mail transmissions cannot be guaranteed to be secure or error free. We do not accept liability for any loss, damage, or error arising from this transmission

_______________________________________________
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.

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Marc Busqué
In reply to this post by Imants Cekusins
On Tue, 31 Jul 2018, Imants Cekusins wrote:

> 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
Thanks for your answer Imants. That seems would be the perfect fit in
the case that both Maybe were parametrized by the same type, but it is
not the case. For that reason I thought I needed an `Either` in the
result.

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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Marc Busqué
In reply to this post by Paul
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é <[hidden email]> 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.
>
>
>
>
>
_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Marc Busqué
In reply to this post by David Feuer
On Tue, 31 Jul 2018, David Feuer wrote:

> I think you're probably after something like
>
> boo :: Maybe a -> Maybe b -> Maybe (Either a b)
> boo ma mb = (Left <$> ma) <|> (Right <$> mb)

Thanks! That seems the right function for my case :) I see we need a
little bit of handcrafting and nothing exists out of the box in base
Haskell.

Marc Busqué
http://waiting-for-dev.github.io/about/

>
> On Tue, Jul 31, 2018, 3:47 AM Marc Busqué <[hidden email]> 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.
>
>
>
_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Nigel Rantor
In reply to this post by Marc Busqué

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....

   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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Marc Busqué
In reply to this post by Tom Ellis-5
On Tue, 31 Jul 2018, Tom Ellis wrote:

> On Tue, Jul 31, 2018 at 09:47:07AM +0200, Marc Busqué 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)
>> ```
>
> This just sounds like bad API design from req.  Why is the API not
>
>    parseUrl :: ByteString -> Maybe (Either (Url Http) (Url Https), Option scheme)
>
>> 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`.
>
> If that invariant really does hold then the authors of those functions
> should encode the invariant in the type. Requiring clients to observe the
> invariant for themselves sounds like a bad idea.
It sounds reasonably. I have opened an [issue in
req](https://github.com/mrkkrp/req/issues/48) with this feature request.

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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Marc Busqué
In reply to this post by Nigel Rantor
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.
_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: Missing common function for Maybe types

Nigel Rantor

I hadn't noticed they were different types...I can't say I love that API
choice.

On 31/07/18 14:58, Marc Busqué wrote:

> 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.

_______________________________________________
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.