-
Notifications
You must be signed in to change notification settings - Fork 10
Add orJustCall, with tests #14
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
base: master
Are you sure you want to change the base?
Conversation
|
Hi @Leeds-eBooks thanks for your contribution. I've given this some thought and am not clear on a concrete use case for this addition. I think if you want to have lazy evaluation with closures then it's possible to use the existing api. Another reason I'm reluctant to accept it as is, is because |
|
Interestingly however, this is the default behaviour for Scala which turns the value into a closure in the function argument (thus making it lazy). https://github.com/scala/scala/blob/2.13.x/src/library/scala/Option.scala#L133 I'm not sure how to achieve the same result in JS. |
|
Understood. I can give you a couple of use cases I am already using my fork for: import {Seq} from 'immutable'
const seq = Seq(hugeCollection)
return maybe(seq.size).orJustCall(() => seq.count())Immutable sequences are not guaranteed to have a static maybe(existingKey)
.map(ek => db.child(ek).set(newData))
.orJustCall(() =>
db.push({
...newData,
createdBy: currentUser.uid,
})
)In this example, I realise that it's perhaps not the "idiomatic" way of using Perhaps these are not the kinds of uses you envisaged for |
|
It would be possible to create a lazy version of Then you could provide a closure the the You could also try: const lazyMaybe = value => maybe(value).map(v => () => v)
lazyMaybe(seq.size).orJust(() => seq.count())()However I think your examples could easily be rewritten as ternary or if/else expressions. It's not clear if the second example returns a value or just causes side effects. |
|
Yeah, I think we've hit the fundamental core of our debate, which is whether it's better to add another method, or ask the user to dance around with userland util functions like your You're right of course – I could just use if/else. But then why use |
Resolves #13. Very willing to discuss this further!