sanctuary-js / sanctuary

:see_no_evil: Refuge from unsafe JavaScript

Home Page:https://sanctuary.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

futpib opened this issue · comments

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

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?

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

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

I disagree, these goals are not reached yet:

  • From issue description:
    • Change the meaning of fromEither (#690)
  • From #642 (comment):
    • Add EitherMaybe conversion functions (#644)

@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?

Having #644 open is enough, I think