-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Extend when()
to receive N arguments
#2842
Comments
I don't recall why we gave this a different feel than ifElse(gt, divide, multiply)(5, 10) //=> 50
ifElse(gt, divide, multiply)(10, 5) //=> 2 But while I would welcome a PR for this, there is no guarantee that everyone would accept it. |
actually, my question is not to create a similar to ifElse, is just, extend |
Sorry, I misread your question a bit. But that means that ifElse is even more relevant. I believe you would get what you want just by using const maybeInit = ifElse (hasUsername, startRealtime, console.error); The only difference is that
const when = (pred, whenTrue) => ifElse (pred, whenTrue, identity)
const unless = (pred, whenFalse) => ifElse (pred, identity, whenFalse) These are not the actual implementations, and there is a difference I alluded to earlier: The functions generated by |
boom goes the dynamite!.... The only tradeoff is that Nevertheless, before closing the issue, any ideas in how to replace
But I wonder if there is a more compact way, because then
|
As to You can just supply a no-op in place of I want to point out, though, that all of this feels a bit like an abuse of Ramda. While side-effects are obviously necessary for any programs that interact with the world, Ramda is not in the business of catering to side-effects. While I described |
In that case, how would the implementation look like without abusing ramda?
The use case is this: "Start a real-time server only if $session.user
exists, otherwise do nothing."
The imperative way of doing it:
```
if ($session.user != null) {
startRealtime();
}
```
On Jun 9, 2019 12:17 PM, "Scott Sauyet" <notifications@github.com> wrote:
As to hasValue, there is a PR now to add isNotNil: #2818
<#2818>. But note that there is nothing
wrong with having your hasValue helper function in scope and passing it to
pathSatisfies. That's how functional programming works, by combining many
small functions.
You can just supply a no-op in place of console.error, either a named noOp
function or just () => {}.
I want to point out, though, that all of this feels a bit like an abuse of
Ramda. While side-effects are obviously necessary for any programs that
interact with the world, Ramda is not in the business of catering to
side-effects. While I described when and unless as glosses on ifElse, ifElse
itself is a gloss on cond. The functions returned by each of these
functions are meant to act as pure functions: you give them arguments and
they return a value. They are all ways of avoiding control flow code with if
- else or switch - case, making your code more declarative. They are
definitely not meant to simply replicate code flow instructions.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#2842?email_source=notifications&email_token=ADANIAXSQEGXYR43WUTRXX3PZUUI7A5CNFSM4HVH5ZV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXINLZI#issuecomment-500225509>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADANIAUP2AI4T2BEIESQTVDPZUUI7ANCNFSM4HVH5ZVQ>
.
|
My point was simply that not everything needs to be -- or even should be -- done with Ramda tools. This is a library put in the hands of its users. It's not up to me to decide how you should use it, but it is designed to be useful for a certain category of problems. If you try to solve other ones that don't match its sweet spot, Ramda won't add particular clarity to your program, and may even make the code harder to read. Ramda's core is probably in data transformations, making it easy to break complex transformations into simpler ones joined together through functional composition. In this, Ramda is different from the more ambitious underscore or lodash, which would like to be your general-purpose utility library. But even for underscore, it's hard to imagine a utility library improving on the simplicity of your imperative version. |
Great explanation.
With ramda, |
By all means, use You could also try if ( prop ('user', $session) != null ) {
startRealtime();
} or const hasUser = pipe ( prop ('user'), isNil, not )
// ...
if ( hasUser ($session) ) {
startRealtime();
} or const isNotNil = pipe (isNil, not)
const hasUser = pipe ( prop('user'), isNotNil )
// ...
if ( hasUser ($session) ) {
startRealtime();
} or perhaps const isNotNil = pipe (isNil, not)
const hasUser = lift (isNotNil) ( prop ('user') )
// ...
if ( hasUser ($session) ) {
startRealtime();
} In every one of these we still keep your |
Here,
when
correctly givessessionStore
to bothhasUsername
andstartRealtime
fns,But
startRealtime
also expectsclientStore
variable to be available, andwhen
doesn’t pass it, because the fn only works with 1 argument.So, my recommendation is to extend
when
or create a similar fn that does exactly that.Currently, this is how I achieve it with eitherWhen:
The text was updated successfully, but these errors were encountered: