JS Built-ins
TypeScriptadvanced

Generics β€” pipe<T>

πŸ“– Usage Example
const result = pipe(
  5,
  x => x * 2,
  x => x + 1
)  // β†’ 11

pipe<T> threads a value through a sequence of transformations left-to-right.

Generics: Function Composition with pipe<T>

Challenge: Implement pipe<T>(...fns: ((x: T) => T)[]): (x: T) => T β€” a left-to-right function composer.

pipe(x => x + 1, x => x * 2)(3)  // β†’ (3+1)*2 = 8
pipe()(42)                         // β†’ 42
πŸ’‘ Hints (1)
  • β€’ reduce with the initial value x threads the value through all functions.