Proposal: add fromRight and fromLeft to Data.Either

When working with Either, I am often missing two simple functions: fromRight :: Either a b -> b fromLeft :: Either a b -> a It has been implemented a couple of times: http://hayoo.fh-wedel.de/?query=fromRight But I don't want to depend on yet another library for such a basic function. Could it be added? Anton

I wouldn't want to encourage partial functions without even an error
message. Usually with Either you'll want to use the unexpected value in
the error msg, e.g 'either (error . ("xyz: "++) . show) id'.
On Fri, May 20, 2016 at 1:08 PM, Anton Felix Lorenzen
When working with Either, I am often missing two simple functions: fromRight :: Either a b -> b fromLeft :: Either a b -> a
It has been implemented a couple of times: http://hayoo.fh-wedel.de/?query=fromRight
But I don't want to depend on yet another library for such a basic function.
Could it be added?
Anton _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

-1. I don't want to encourage this sort of bad coding practice practice
either. Keeping it inconvenient is fine by me.
On May 20, 2016 4:08 PM, "Anton Felix Lorenzen"
When working with Either, I am often missing two simple functions: fromRight :: Either a b -> b fromLeft :: Either a b -> a
It has been implemented a couple of times: http://hayoo.fh-wedel.de/?query=fromRight
But I don't want to depend on yet another library for such a basic function.
Could it be added?
Anton _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

I wouldn't mind something like this being available... but only if prefixed
with the word "unsafe" or "partial" or other such scary words.
-- Dan Burton
On Fri, May 20, 2016 at 1:34 PM, David Feuer
-1. I don't want to encourage this sort of bad coding practice practice either. Keeping it inconvenient is fine by me. On May 20, 2016 4:08 PM, "Anton Felix Lorenzen"
wrote: When working with Either, I am often missing two simple functions: fromRight :: Either a b -> b fromLeft :: Either a b -> a
It has been implemented a couple of times: http://hayoo.fh-wedel.de/?query=fromRight
But I don't want to depend on yet another library for such a basic function.
Could it be added?
Anton _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

js :: a -> (Void, a) js x = (undefined, x) left :: (Void, Either a b) -> a left (_, Left x) = x There we go... now it looks truly terrifying: left.js $(myValue) (Apologies to Kosyrev for the double-send. Especially since that version was broken.) On Fri, May 20, 2016 at 2:36 PM, Kosyrev Serge <_deepfire@feelingofgreen.ru> wrote:
Dan Burton
writes: I wouldn't mind something like this being available... but only if prefixed with the word "unsafe" or "partial" or other such scary words.
Heh, `unsafePerformLeft` / `unsafePerformRight` ? : -)
-- с уважениeм / respectfully, Косырев Сергей _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

On Fri, May 20, 2016 at 10:08:40PM +0200, Anton Felix Lorenzen wrote:
When working with Either, I am often missing two simple functions: fromRight :: Either a b -> b fromLeft :: Either a b -> a
Sometimes my lazy inner-self is guilty of longing for `fromRight` when typing: λ> either undefined id xyz but then again, importing Data.Either wouldn't make thing any shorter :P λ> :m +Da<tab>.Ei<tab> λ> fromRight zyx I can see fromRight being useful in one-off script; anything bigger than a few lines is asking for trouble!

There's also (\(Right x)->x), which is pretty short and will give you a warning about the missing case. (I could go either way on adding fromLeft/Right though) Tom
El 20 may 2016, a las 19:22, Francesco Ariis
escribió: On Fri, May 20, 2016 at 10:08:40PM +0200, Anton Felix Lorenzen wrote: When working with Either, I am often missing two simple functions: fromRight :: Either a b -> b fromLeft :: Either a b -> a
Sometimes my lazy inner-self is guilty of longing for `fromRight` when typing:
λ> either undefined id xyz
but then again, importing Data.Either wouldn't make thing any shorter :P
λ> :m +Da<tab>.Ei<tab> λ> fromRight zyx
I can see fromRight being useful in one-off script; anything bigger than a few lines is asking for trouble!
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

I'm personally -1 on this.
A large portion of the community is vehemently against adding new partial
functions, and they have to share base with the rest of us. I'd say more
people want to remove fromMaybe, head, tail, etc. than want to double down
on the pattern at this time.
-Edward
On Fri, May 20, 2016 at 4:08 PM, Anton Felix Lorenzen
When working with Either, I am often missing two simple functions: fromRight :: Either a b -> b fromLeft :: Either a b -> a
It has been implemented a couple of times: http://hayoo.fh-wedel.de/?query=fromRight
But I don't want to depend on yet another library for such a basic function.
Could it be added?
Anton _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

I would add (as I probably did last time something like this came up)
that it's difficult to get excited about all these functions named by
a systematic grammar, when we know _today_ how to just implement that
grammar as a library of composable pieces. And it has been implemented
in lens (and possibly other libraries that are subsets of lens).
Then you can have `is`, `over` (like map), `from` (take your pick on
whether it's partial or with-default), `foldOf`, `traverseOf`, ...,
and `_Left`, `_Right`, `_Just`, `_Nothing` (not all of these
correspond to lens; just you could define them). These building blocks
make all these functions definable, with almost exactly the names
they'd be given as one-off functions in several modules. But of
course, they also compose in more interesting ways, too. You can have
`from (_Left . _Just)`, but we will never be adding a
`fromLeftAndJust` function to base.
I don't really expect (or hope for) base to start pulling in big
chunks of lens. And maybe it makes sense for all of these to be in
base just for uniformity. But it also seems like people could just be
using something better.
-- Dan
On Sat, May 21, 2016 at 8:55 AM, Edward Kmett
I'm personally -1 on this.
A large portion of the community is vehemently against adding new partial functions, and they have to share base with the rest of us. I'd say more people want to remove fromMaybe, head, tail, etc. than want to double down on the pattern at this time.
-Edward
On Fri, May 20, 2016 at 4:08 PM, Anton Felix Lorenzen
wrote: When working with Either, I am often missing two simple functions: fromRight :: Either a b -> b fromLeft :: Either a b -> a
It has been implemented a couple of times: http://hayoo.fh-wedel.de/?query=fromRight
But I don't want to depend on yet another library for such a basic function.
Could it be added?
Anton _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

+1 from me. I often use such functions when working in GHCi, and it is consistent with the existence of fromJust. That said, I understand the opposition, and these functions should never be used in production code. Maybe we should put the lot of them (together with head, tail, fromJust) into a "Partial" module? Twan On 2016-05-20 22:08, Anton Felix Lorenzen wrote:
When working with Either, I am often missing two simple functions: fromRight :: Either a b -> b fromLeft :: Either a b -> a
It has been implemented a couple of times: http://hayoo.fh-wedel.de/?query=fromRight
But I don't want to depend on yet another library for such a basic function.
Could it be added?
Anton _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

It has been two weeks now since I wrote my proposal. First of all, thanks to all for their replies (and sorry for having started such a big discussion). Tom has mentioned, that there is (\(Right x)->x). I didn't consider that (shame on me), but I think it is perfect as it is short and easy to read. Also it shows the idea of unwrapping, something which "either undefined id" lacks. I think it is a good replacement and therefore withdraw my proposal. Meanwhile, it was proposed to include fromLeft :: a -> Either a b -> a fromRight :: b -> Either a b -> b I am a weak +1 on this. -- Anton On 05/20/2016 10:08 PM, Anton Felix Lorenzen wrote:
When working with Either, I am often missing two simple functions: fromRight :: Either a b -> b fromLeft :: Either a b -> a
It has been implemented a couple of times: http://hayoo.fh-wedel.de/?query=fromRight
But I don't want to depend on yet another library for such a basic function.
Could it be added?
Anton _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

Anton Felix Lorenzen wrote:
When working with Either, I am often missing two simple functions: fromRight :: Either a b -> b fromLeft :: Either a b -> a
It has been implemented a couple of times: http://hayoo.fh-wedel.de/?query=fromRight
But I don't want to depend on yet another library for such a basic function.
Could it be added?
-1 on the addition of yet another partial function. Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/

Putting on my CLC hat to summarize the talk so far into a concrete plan:
It looks like there is sufficient consensus to add the
fromLeft :: a -> Either a b -> a
fromRight :: b -> Either a b -> b
variants to Data.Either, so let's do that.
This avoids partiality concerns, and seems to have broad support. I'll
modify the `either` package to re-export them once they make it into base
to avoid import conflicts.
Does someone want to bang out a patch?
-Edward
On Sun, Jun 12, 2016 at 2:29 AM, Erik de Castro Lopo
Anton Felix Lorenzen wrote:
When working with Either, I am often missing two simple functions: fromRight :: Either a b -> b fromLeft :: Either a b -> a
It has been implemented a couple of times: http://hayoo.fh-wedel.de/?query=fromRight
But I don't want to depend on yet another library for such a basic function.
Could it be added?
-1 on the addition of yet another partial function.
Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/ _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
participants (13)
-
amindfv@gmail.com
-
Anton Felix Lorenzen
-
Dan Burton
-
Dan Doel
-
David Feuer
-
Edward Kmett
-
Erik de Castro Lopo
-
Evan Laforge
-
Francesco Ariis
-
Kosyrev Serge
-
M Farkas-Dyck
-
Theodore Lief Gannon
-
Twan van Laarhoven