Performs functions composition from right to left.
💡
The compose
function is equivalent to a(b(c(val)))
.
Related
Syntax
import { compose } from '@opentf/std';
compose(val: unknown, ...fns: Function[]): unknown
Examples
compose(1); //=> 1
compose(-1, Math.abs); //=> 1
compose(-4, Math.sqrt, Math.abs); //=> 2
compose(
1.5,
Math.ceil,
(x) => x + 1,
(x) => x * 5
); //=> 9
Try
Learn
Why we need function composition?
- The deep nesting of functions is hard to read.
- It eliminates temporary variables.
- Method chaining is limited, for Eg: await, yeild, etc.