🧪 Creates a function that performs function composition from right to left.
💡
The resulting function is equivalent to a(b(c(...args))).
Syntax
import { composeFunction } from '@opentf/std';
composeFunction(
...fns: ((...args: any[]) => any)[]
): (...args: any[]) => anyParameters
...fns: A sequence of functions to compose from right to left.
Returns
A new function that takes arguments, passes them to the rightmost function, and then pipes the result through the remaining functions from right to left.
Examples
const transform = composeFunction(Math.abs, Math.pow);
transform(-2, 3); //=> 8 ( Math.abs(Math.pow(-2, 3)) = Math.abs(-8) = 8 )Related
Try
Learn
Why do we need function composition?
- Readability: Deep nesting of functions like
c(b(a(x)))is hard to read. - Conciseness: It eliminates the need for temporary variables.
- Flexibility: Method chaining is limited when dealing with
await,yield, etc.