Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping the left side of an Either #98

Closed
raine opened this issue Sep 22, 2015 · 13 comments
Closed

Mapping the left side of an Either #98

raine opened this issue Sep 22, 2015 · 13 comments

Comments

@raine
Copy link

raine commented Sep 22, 2015

Let's explore if these functions in data.either would be useful in Sanctuary:

@benperez
Copy link
Contributor

Nice suggestions! In Haskell Data.Either is an instance of Data.Bifunctor. Which would give us bimap, first (or leftMap), and second (or rightMap/fmap). orElse seems interesting too, what's an example use case for that?

@benperez
Copy link
Contributor

benperez commented Oct 1, 2015

I'm going to go ahead and take a crack at leftMap and biMap. @albrow and I had a need for this earlier today.

@benperez benperez self-assigned this Oct 1, 2015
@davidchambers
Copy link
Member

Are you still interested in working on this, @benperez? If not, I'm happy to investigate these options.

@blperez
Copy link

blperez commented Dec 18, 2015

I can check it out this weekend after I submit my ramda PR.

@davidchambers
Copy link
Member

That would be wonderful!

@benperez
Copy link
Contributor

@blperez is an imposter, I'll take a look when if I get a chance this weekend.

@iliocatallo
Copy link

any news on this side?

@davidchambers
Copy link
Member

I think we should implement Either#bimap in accordance with fantasyland/fantasy-land#137:

Either#bimap :: Either a b ~> (a -> c) -> (b -> d) -> Either c d

I'm preparing to move to a different continent in a couple of days, but I should be able to devote significant time to Sanctuary once I'm in Europe. If you'd like to contribute a pull request for this addition, though, please do so. :)

@jlmorgan
Copy link

orElse, orElseGet and orElseThrow are tied to extract/get.

Examples:

S.Right(1).extract();
// => 1
S.Left("NaN").extract();
// => ?

S.Right(1).orElse(2);
// => 1
S.Left("NaN").orElse(2);
// => 2

S.Right(1).orElseGet(() => 2);
// => 1
S.Left("NaN").orElseGet(() => 2);
// => 2

S.Right(1).orElseThrow(() => new Error("fatal: because reasons");
// => 1
S.Left("NaN").orElseThrow(() => new Error("fatal: because reasons");
// => throws Error

With extract being a possible mixed type, you would likely end up with getOrElse, getOrElseGet, and getOrElseThrow to avoid marrying to Comonad.

@davidchambers
Copy link
Member

With extract being a possible mixed type

In other words, Either a b cannot support extract.

@jlmorgan
Copy link

Basically. Hence the get prefixed versions of the method that require you to provide a fallback value.

@jlmorgan
Copy link

Static versions come in handy when going through a map as well rather than having to purge all the lefts/nothings/failures.

@davidchambers
Copy link
Member

Thanks to #229 we have fromEither :: b -> Either a b -> b. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants