Add Either ↔ Maybe conversion functions#644
Add Either ↔ Maybe conversion functions#644futpib wants to merge 5 commits intosanctuary-js:mainfrom
Either ↔ Maybe conversion functions#644Conversation
| //. Right ('No negative numbers') | ||
| //. | ||
| //. > S.maybeToLeft ('No negative numbers') (S.find (S.lt (0)) ([-1, 0, 1])) | ||
| //. Left (-1) |
There was a problem hiding this comment.
These are nice examples. :)
index.js
Outdated
| _.fromEither = { | ||
| consts: {}, | ||
| types: [$.Either (a) (a), a], | ||
| impl: fromEither |
There was a problem hiding this comment.
Could we use either (I) (I) here directly?
f7a9689 to
7a28ebe
Compare
| _.fromLeft = { | ||
| consts: {}, | ||
| types: [a, $.Either (a) (b), a], | ||
| impl: fromLeft |
There was a problem hiding this comment.
What do you think of using B (either (I)) (K) here?
There was a problem hiding this comment.
I don't see a difference, except perhaps stylistic, so you decide.
There was a problem hiding this comment.
Let's stick with your implementation. :)
| consts: {}, | ||
| types: [b, $.Either (a) (b), b], | ||
| impl: fromEither | ||
| impl: fromRight |
There was a problem hiding this comment.
@davidchambers Should use B (C (either) (I)) (K) here to match fromLeft?
There was a problem hiding this comment.
I find the existing implementation of fromRight easy to read. Let's keep it. Let's also keep your implementation of fromLeft, as there is value in similar things looking similar. :)
|
@davidchambers do we have a |
|
We have |
|
@davidchambers Right, there's a number of different alternatives here. FWIW I think going down the road of exhaustively comparing all these alternatives would swamp the PR, and I think the functions in the PR are simple and handy enough to justify addition as is. To explain the relationship between the functions here and So for example instead of only having Similarly, instead of only having There's also wacky stuff you can do with lenses (have an iso between |
|
Thanks for the clarification, Asad. I understand your point now. :) |
I'm happy with them. My only concern is that in implementing the entire matrix of conversions between
Instead of My concern exists because the composition model is easily scalable under inclusion of additional types, whereas the above matrix would grow exponentially with every new constructor added. I've just spent a lot of words explaining this concern, which makes it seem as though I might feel strongly about this, but it's really very minor - just something you might not have considered. |
|
Going over these changes again, I think they represent an improvement over the status quo. My API surface scaling concern is small, because these functions will always have descriptive names / namespaces. |
7a28ebe to
a71a210
Compare
|
@futpib, are you interested in submitting a small pull request to change |
a71a210 to
f3d6f71
Compare
f3d6f71 to
c7e798c
Compare
|
I also dropped the commit with new |
|
Lovely work, @futpib. Thank you for pointing out that this pull request no longer contains breaking changes. Please update the commit messages and this pull request's title to reflect the fact that this pull request is purely additive now that #664 has been merged. Also, this pull request's description is no longer accurate. |
Either functions to Left and Right pairsEither ↔ Maybe conversion functions
|
@davidchambers Done. |
|
Thank you, @futpib. This pull request is in a very good state. :) I am not certain that these functions should be added to the library. Here they are in use: > maybeToRight ('XXX') (Nothing)
Left ("XXX")
> maybeToRight ('XXX') (Just (42))
Right (42)
> rightToMaybe (Left ('XXX'))
Nothing
> rightToMaybe (Right (42))
Just (42)The transformations above are possible with existing functions: > maybe (Left ('XXX')) (Right) (Nothing)
Left ("XXX")
> maybe (Left ('XXX')) (Right) (Just (42))
Right (42)
> either (K (Nothing)) (Just) (Left ('XXX'))
Nothing
> either (K (Nothing)) (Just) (Right (42))
Just (42)To me, the expressions involving Note that Do these functions offer enough value to justify their inclusion? I would like to hear from @masaeedu, @Avaq, and others. :) |
Should we consider |
This addresses the naming concern raised in PR sanctuary-js#644 review where Avaq noted that `maybeToRight` can return a Left, creating a naming paradox similar to NaN. The new names emphasize what happens to a Just value, making the API clearer and more intuitive.
This completes the naming consistency improvements started in the previous commit. All four conversion functions now use consistent naming that emphasizes the target constructor: - justToLeft / justToRight (Just → Left/Right) - leftToJust / rightToJust (Left/Right → Just)
This pull request adds the following functions: