Function
pipe

Performs functions composition from left to right.

💡

The pipe function is equivalent to c(b(a(val))).

Related

Syntax

import { pipe } from '@opentf/std';
 
pipe(val: unknown, ...fns: Function[]): unknown

Examples

pipe(1); //=> 1
 
pipe(-1, Math.abs); //=> 1
 
pipe(-4, Math.abs, Math.sqrt); //=> 2
 
pipe(
  1,
  (x) => x + 1,
  (x) => x * 5
); //=> 10

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.

Resources