Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
However, for normal conditional use it's error prone, as you've shown in your code. Both expensive gets will run before the condition is even applied to true.
The actual code would be
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Both expensive gets will run before the condition is even applied to true.
This is still false. Only one will run. Never both.
constisEven=n=>n%===0constlogEven=n=>console.log(`${n} is Even!`)constlogOdd=n=>console.log(`${n} is Odd`)ifElse(isEven)(logEven)(logOdd)(10)//=> "10 is Even!"
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Yes you are correct. The expensiveGet method needs to take 2 arguments for it to work the way it is being called in ifElse.
I have created a working example that you can run.
constS=require('sanctuary')constexpensiveGet=(y)=>{console.log("doing expensive get for",y)return2}constcondition=x=>x===100constexpensiveGetPlus=x=>y=>expensiveGet(y)+xS.ifElse(condition)(expensiveGetPlus(1))(expensiveGetPlus(-1))(100)//=> "doing expensive get for 100"
This example will show that ifElse only executes one of the functions.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Same with
ifElse
Check out Sanctuary's
ifElse
sanctuary.js.org. You can live edit their page to see how it works.I'd love to get some live editing docs page for my stuff too. Pretty neat site.
I see it's actually for threading through the value, just like
tap
:However, for normal conditional use it's error prone, as you've shown in your code. Both expensive gets will run before the condition is even applied to true.
The actual code would be
That's correct,
ifElse
accepts functions. So those functions are only executed when the condition is met.This is still false. Only one will run. Never both.
You can see
logOdd
is never called.In your first example
both run.
Yes you are correct. The expensiveGet method needs to take 2 arguments for it to work the way it is being called in
ifElse
.I have created a working example that you can run.
This example will show that
ifElse
only executes one of the functions.