🧪 Creates a function that performs function composition from left to right.
💡
The resulting function is equivalent to c(b(a(...args))).
Syntax
import { pipeFunction } from '@opentf/std';
pipeFunction(
...fns: ((...args: any[]) => any)[]
): (...args: any[]) => anyParameters
...fns: A sequence of functions to compose from left to right.
Returns
A new function that takes arguments, passes them to the first function, and then pipes the result through the remaining functions from left to right.
Examples
const transform = pipeFunction(Math.pow, Math.abs);
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.