-
-
Notifications
You must be signed in to change notification settings - Fork 503
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
Functor: add bindMap
function as a do-notation helper
#1725
Conversation
previously #1524? |
@imcotton I think you're right that this covers that use case as well. For my mental model, I think this PR is equivalent to: do
x <- Just 42
y <- Just 1337
let z = x * y -- this line
etc |
@imcotton oh you're right, I failed to notice that there was already an open issue on this. It does cover this use case and yes that is more or less the |
One thing I noticed from your implementation is allowing redefine existing keys, e.g. pipe(
option.Do,
option.apS('x', some(42)),
option.apS('y', some(1337)),
option.bindMap('y', ({ x, y }) => x * y), // <-- reassign y here
) Is this intend behavior or oversight in compare to: Lines 156 to 163 in 34b28fa
in L159: |
@imcotton I'll be honest, that is an oversight on my part and I will fix it for consistency with Good catch, though. Thank you 🙏 |
IMO, name shadowing from outer scope is reasonable (but I'd avoid it personally), however in case of Overall I'm 👍 for this feature tho, thanks for the effort. |
@imcotton I agree and I did make the change again for consistency with I still think that although it is a smell in most cases, it can be desirable in some to override a value with a modified one but at the end of the day it is not the subject of this PR (hence the change 😄). Thank you for the find and the support 🙏 |
For what concerns the name I'd really like (*)
const let_ = ...
export {
/**
* @since 2.13.0
*/
let_ as let
} is it a viable option? |
@gcanti thank you for the suggestion, I was really bummed I couldn't use I changed the name to Let me know if something is missing / needs changing in the PR |
@Punie some |
Never mind, I was looking at the md files :) |
Thank you! |
Hello 👋
I took the liberty to create a new function on the
Functor
typeclass to help with introducing new values in do-notation context while leveraging pure computations that depend on said context.ie. I needed to do something akin to this (example greatly simplified from my original genuine use-case):
Another way to achieve the same result would be to just use
option.map
at the last step:I find both these solutions unsatisfying so I introduced the
bindMap
function onFunctor
specifically for those use-cases. It is tomap
whatbind
is tochain
andapS
is toap
.With this, we can rewrite the example above as such (getting rid of the
option.of
or the recreation of the context object):I hope this can be of use to others as well. I'm not too fond of the name
bindMap
but it's the best I could think of. I kinda likedlet
but as a reserved keyword in TS it was a no-go.