-
-
Notifications
You must be signed in to change notification settings - Fork 95
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
Add Either
↔ Maybe
conversion functions
#644
base: main
Are you sure you want to change the base?
Conversation
//. 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are nice examples. :)
index.js
Outdated
} | ||
_.fromEither = { | ||
consts: {}, | ||
types: [$.Either (a) (a), a], | ||
impl: fromEither |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use either (I) (I)
here directly?
f7a9689
to
7a28ebe
Compare
index.js
Outdated
_.fromLeft = { | ||
consts: {}, | ||
types: [a, $.Either (a) (b), a], | ||
impl: fromLeft |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of using B (either (I)) (K)
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a difference, except perhaps stylistic, so you decide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's stick with your implementation. :)
index.js
Outdated
consts: {}, | ||
types: [b, $.Either (a) (b), b], | ||
impl: fromEither | ||
impl: fromRight |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidchambers Should use B (C (either) (I)) (K)
here to match fromLeft
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 pull request adds the following functions: