Example 1 — Official pipeWhileNotNil
A transformer that only continues while the result is not null or undefined. This is the sample from the Ramda docs.
const pipeWhileNotNil = R.pipeWith((f, res) =>
R.isNil(res) ? res : f(res)
);
const f = pipeWhileNotNil([Math.pow, R.negate, R.inc]);
console.log(f(3, 4)); // -80 How It Works
- Pipe maker. Passing only the transformer returns
pipeWhileNotNil, ready for any array of steps. - First step.
Math.pow(3, 4)runs directly →81. - Checked steps.
R.isNilis false each time, soR.negate→-81, thenR.inc→-80. - Same as pipe. With no nil values, the result matches
R.pipe(Math.pow, R.negate, R.inc).
