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.
Press + to interact
Javascript (babel-node)
const isEven = (num) => num % 2 === 0;const doubleIfEven = (num) => {if (isEven(num)) {return num * 2;}return num;};console.log(doubleIfEven(100),doubleIfEven(101));
Again, ternaries can work well here.
Press + to interact
Javascript (babel-node)
const isEven = (num) => num % 2 === 0;const doubleIfEven = (num) => isEven(num) ? num * 2 : num;console.log(doubleIfEven(100),doubleIfEven(101));
But we can also use the when
function. It takes three arguments
- Predicate (function that returns
true
orfalse
) - Function to run if predicate returns true
- The value to use
Press + to interact
Javascript (babel-node)
import { when } from 'ramda';const isEven = (num) => num % 2 === 0;const doubleIfEven = when(isEven,(num) => num * 2);console.log(doubleIfEven(100),doubleIfEven(101));
We can make it point-free.
Press + to interact
Javascript (babel-node)
import { multiply, when } from 'ramda';const isEven = (num) => num % 2 === 0;const doubleIfEven = when(isEven,multiply(2));console.log(doubleIfEven(100),doubleIfEven(101));
Conveniently enough, Ramda lets you express the opposite logic using unless
.
This runs your function if the predicate returns false
.
Press + to interact
Javascript (babel-node)
import { multiply, unless } from 'ramda';const isEven = (num) => num % 2 === 0;const doubleIfOdd = unless(isEven,multiply(2));console.log(doubleIfOdd(100),doubleIfOdd(101));
Now this function only doubles odd numbers. If we wanted doubleIfEven
, our predicate must flip as well.
Press + to interact
Javascript (babel-node)
import { multiply, unless } from 'ramda';const isOdd = (num) => num % 2 !== 0;const doubleIfEven = unless(isOdd,multiply(2));console.log(doubleIfEven(100),doubleIfEven(101));