when/unless
Using when/unless in functional pipelines (4 min. read)
We'll cover the following...
We'll cover the following...
Sometimes you only need the if statement, and the else simply returns the value unchanged.
Again, ternaries can work well here.
But we can also use the when function. It takes three arguments
- Predicate (function that returns
trueorfalse) - Function to run if predicate returns true
- The value to use
We can make it point-free.
Conveniently enough, Ramda lets you express the opposite logic using unless.
This runs your function if the predicate returns false.
Now this function only doubles odd numbers. If we wanted doubleIfEven, our predicate must flip as well.