Maybe to Either -- is there a better way?

I find it convenient sometimes to convert a Maybe value to an Either thus (excuse the syntax, it's CAL, not Haskell): maybeToEither :: a -> Maybe b -> Either a b; maybeToEither errorValue = maybe (Left errorValue) (\x -> Right x); but that seemingly obvious function isn't in Hoogle, AFAICT, so perhaps there's some other approach? Thanks, Tom

It's available in MissingH:
http://hackage.haskell.org/packages/archive/MissingH/latest/doc/html/Data-Ei...
You can find this using Hayoo, which indexes Hackage.
MissingH is pretty huge, though, just for one function. It's kind of
annoying. I'm using this function in my library and one other, in the
hopes I'll need more to make depending on MissingH worth it.
On 2 August 2010 16:14, Tom Davies
I find it convenient sometimes to convert a Maybe value to an Either thus (excuse the syntax, it's CAL, not Haskell):
maybeToEither :: a -> Maybe b -> Either a b; maybeToEither errorValue = maybe (Left errorValue) (\x -> Right x);
but that seemingly obvious function isn't in Hoogle, AFAICT, so perhaps there's some other approach?
Thanks, Tom_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Tom Davies wrote:
I find it convenient sometimes to convert a Maybe value to an Either
maybeToEither = flip maybe Right . Left Christopher Done wrote:
It's available in MissingH
While useful, I think its ubiquity to simplicity ratio is not high enough to justify either depending on MissingH just for that, or adding it to a base library. However, if your personal style makes this so ubiquitous as to override its simplicity, go ahead and upload it to Hackage, perhaps others will also benefit. Regards, Yitz

Yitzchak Gale
Tom Davies wrote:
I find it convenient sometimes to convert a Maybe value to an Either
maybeToEither = flip maybe Right . Left
Remember, some people don't like flip! :p maybeToEither = (`maybe` Right) . Left
While useful, I think its ubiquity to simplicity ratio is not high enough to justify either depending on MissingH just for that, or adding it to a base library.
Just like the swap :: (a,b) -> (b,a) function a lot of people were discussing on libraries@? In general, I agree.
However, if your personal style makes this so ubiquitous as to override its simplicity, go ahead and upload it to Hackage, perhaps others will also benefit.
Just give it a good name, rather than "fooToolkit", "barToolkit", etc. -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

I wrote:
maybeToEither = flip maybe Right . Left
Ivan Lazar Miljenovic wrote:
Remember, some people don't like flip! :p maybeToEither = (`maybe` Right) . Left
Yes, absolutely!
...go ahead and upload it to Hackage
Just give it a good name, rather than "fooToolkit", "barToolkit", etc.
How about calling it "fgl"? No, please don't really. I'm just teasing Ivan. Regards, Yitz

Ivan Lazar Miljenovic schrieb:
Yitzchak Gale
writes:
While useful, I think its ubiquity to simplicity ratio is not high enough to justify either depending on MissingH just for that, or adding it to a base library.
Just like the swap :: (a,b) -> (b,a) function a lot of people were discussing on libraries@?
I have 'swap' in my utility-ht.
In general, I agree.
Problem in Haskell is, that it allows for a high degree of modularization such that most components become trivial. Does that mean that we should not define simple functions at all, which in the extreme case would mean that we inline all potential function calls? I think converting Maybe to Either is an often repeating task which justifies an individual function. Maybe I add it to utility-ht.

On Wed, Aug 4, 2010 at 9:21 AM, Henning Thielemann
Ivan Lazar Miljenovic schrieb:
Yitzchak Gale
writes: While useful, I think its ubiquity to simplicity ratio is not high enough to justify either depending on MissingH just for that, or adding it to a base library.
Just like the swap :: (a,b) -> (b,a) function a lot of people were discussing on libraries@?
I have 'swap' in my utility-ht.
In general, I agree.
Problem in Haskell is, that it allows for a high degree of modularization such that most components become trivial. Does that mean that we should not define simple functions at all, which in the extreme case would mean that we inline all potential function calls?
It's also nice for people reading code if common functions are functions from common libraries. This allows readers' "vocabulary" of common functions to increase, so they don't have to trawl through someone's personal "utility" library to figure out what each utility function does. Alex

On 4 August 2010 18:40, Alexander Dunlap
It's also nice for people reading code if common functions are functions from common libraries. This allows readers' "vocabulary" of common functions to increase, so they don't have to trawl through someone's personal "utility" library to figure out what each utility function does.
Agreed. That's why I like readMay. There are many ways to write a reads wrapper: case reads str of [(x,_)] -> Just x; _ -> Nothing vs case reads str of [(x,"")] -> Just x; _ -> Nothing -- stricter Etc. It seems everyone defines their one, where as Safe.readMay is common. There could be others defined in Safe that encompass most cases.

Tom Davies
I find it convenient sometimes to convert a Maybe value to an Either thus (excuse the syntax, it's CAL, not Haskell):
maybeToEither :: a -> Maybe b -> Either a b; maybeToEither errorValue = maybe (Left errorValue) (\x -> Right x);
As a side note, this is perfectly valid Haskell code, even though it's quite verbose. The equivalent: maybeToEither :: a -> Maybe b -> Either a b maybeToEither leftValue = maybe (Left leftValue) Right Honestly I find this better than the completely points-free variants posted in the other subthread. They demonstrate overusing points-free style. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

On 03/08/2010, at 10:09 PM, Ertugrul Soeylemez wrote:
Tom Davies
wrote: I find it convenient sometimes to convert a Maybe value to an Either thus (excuse the syntax, it's CAL, not Haskell):
maybeToEither :: a -> Maybe b -> Either a b; maybeToEither errorValue = maybe (Left errorValue) (\x -> Right x);
As a side note, this is perfectly valid Haskell code, even though it's quite verbose. The equivalent:
maybeToEither :: a -> Maybe b -> Either a b maybeToEither leftValue = maybe (Left leftValue) Right
I like that better, and CAL supports that, thanks.
Honestly I find this better than the completely points-free variants posted in the other subthread. They demonstrate overusing points-free style.
I agree -- as an inexperienced functional programmer, but not a complete beginner, I found the points free versions incomprehensible! Tom

On 02/08/10 15:14, Tom Davies wrote:
I find it convenient sometimes to convert a Maybe value to an Either thus (excuse the syntax, it's CAL, not Haskell):
maybeToEither :: a -> Maybe b -> Either a b; maybeToEither errorValue = maybe (Left errorValue) (\x -> Right x);
but that seemingly obvious function isn't in Hoogle, AFAICT, so perhaps there's some other approach?
I just uploaded djinn-th [1], a fork of Lennart Augustsson's djinn [2] which uses TemplateHaskell to do things like: {-# LANGUAGE TemplateHaskell, ScopedTypeVariables #-} import Language.Haskell.Djinn (djinnD) $(djinnD "maybeToEither" [t|forall a b . a -> Maybe b -> Either a b|]) main = print . map (maybeToEither "foo") $ [Nothing, Just "bar"] and get some results, if not always the one you intended. [1] http://hackage.haskell.org/package/djinn-th [2] http://hackage.haskell.org/package/djinn Thanks, Claude -- http://claudiusmaximus.goto10.org

On Tue, Aug 3, 2010 at 8:33 PM, Claude Heiland-Allen
{-# LANGUAGE TemplateHaskell, ScopedTypeVariables #-} import Language.Haskell.Djinn (djinnD) $(djinnD "maybeToEither" [t|forall a b . a -> Maybe b -> Either a b|]) main = print . map (maybeToEither "foo") $ [Nothing, Just "bar"]
This is very cool (as is Djinn itself), but for me the ideal syntax would be maybeToEither :: a -> Maybe b -> Either a b maybeToEither = $(derived) Is something like this possible in TH? The splice would have to know its declared (even inferred?) type. --Max
participants (9)
-
Alexander Dunlap
-
Christopher Done
-
Claude Heiland-Allen
-
Ertugrul Soeylemez
-
Henning Thielemann
-
Ivan Lazar Miljenovic
-
Max Rabkin
-
Tom Davies
-
Yitzchak Gale