sanctuary-js / sanctuary

:see_no_evil: Refuge from unsafe JavaScript
https://sanctuary.js.org
MIT License
3.04k stars 94 forks source link

`fromEither` should be called `fromRight` to match Haskell and allow for `fromLeft` #642

Closed futpib closed 4 years ago

futpib commented 5 years ago

Eventually, we might want to have both fromLeft and fromRight:

fromLeft :: a -> Either a b -> a
fromRight :: b -> Either a b -> b

but right now we have fromEither :: b -> Either a b -> b which, if kept as it is, would result in this inconsistency:

fromLeft :: a -> Either a b -> a
fromEither :: b -> Either a b -> b

Also, fromEither is a good name for a different function:

fromEither :: Either a a -> a

This would also match names for these function in Haskell

davidchambers commented 5 years ago

fromLeft and fromRight sound good to me, @futpib! The only problem with your proposal is that it would make the naming of S.fromMaybe internally inconsistent. For consistency, that function should be renamed fromJust, but Haskell uses that name for its impure Maybe a -> a function.

I see three options:

  1. Status quo (internally consistent; no a -> Either a b -> a function)
  2. Copy Haskell (internally inconsistent)
  3. Provide fromJust, fromLeft, and fromRight (internally consistent; inconsistent with Haskell)

Which option do you favour?

futpib commented 5 years ago

Do you think, if rename anything at all, we should choose once whether to use type or constructor names in these function?

If we choose to always use constructor names, maybeToNullable (justToNullable) will be an outlier, with no constructor for Nullable possible.

I think the best strategy is to use type names when no confusion is imaginable, otherwise use constructor names.

Under this rule, fromMaybe and maybeToNullable are fine since there is no value in Nothing (fromNothing, nothingToNullable make no sense).

I also noticed other similar inconsistencies, so I think Maybe-related functions should be left as is, but these Either function should be split into Left and Right pairs:

Old New
maybeToEither maybeToLeft
maybeToRight
fromEither fromLeft
fromRight
eitherToMaybe leftToMaybe
rightToMaybe

And fromEither should be added with the new meaning of

fromEither :: Either a a -> a
davidchambers commented 4 years ago

I believe that this issue can be closed now that #683 has been merged. Do you agree, @futpib?

futpib commented 4 years ago

I disagree, these goals are not reached yet:

davidchambers commented 4 years ago

@futpib, shall we close this issue and open a new one for the conversion functions, or would you prefer to update this issue's title and description?

futpib commented 4 years ago

Having #644 open is enough, I think