Missing common function for Maybe types

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/

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

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.

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.

Salut Marc,
Are you looking for Alternative by any chance:
http://hackage.haskell.org/package/base-4.11.1.0/docs/Control-Applicative.ht...
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é
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.

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: http://hackage.haskell.org/package/base-4.11.1.0/docs/Control-Applicative.ht...
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é
mailto:marc@lamarciana.com> wrote: Apologies, I clearly asked the question in a rushed way...
I'm working with [req](http://hackage.haskell.org/package/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/ 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/ 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 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 http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

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

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

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

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

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

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

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/

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 http://iohk.io Twitter: @vamchale PGP Key ID: 4209B7B5 Input Output http://iohk.io Twitter https://twitter.com/InputOutputHK Github https://github.com/input-output-hk LinkedIn https://www.linkedin.com/company/input-output-global 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

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.

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.

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.
participants (7)
-
David Feuer
-
Imants Cekusins
-
Marc Busqué
-
Nigel Rantor
-
Paul
-
Tom Ellis
-
Vanessa McHale