Skip to content

tap

tap<A>(f): (a) => A

Defined in: Composition/tap.ts:30

Executes a side effect function and returns the original value unchanged. Useful for logging, debugging, or other side effects within a pipeline.

A

(a) => void

(a): A

A

A

// Debugging a pipeline
pipe(
  Option.of(5),
  tap(x => console.log("Before map:", x)),
  Option.map(n => n * 2),
  tap(x => console.log("After map:", x)),
  Option.getOrElse(0)
);
// logs: "Before map: { kind: 'Some', value: 5 }"
// logs: "After map: { kind: 'Some', value: 10 }"
// returns: 10

// Collecting intermediate values
const values: number[] = [];
pipe(
  [1, 2, 3],
  arr => arr.map(n => n * 2),
  tap(arr => values.push(...arr))
);

Option.tap for Option-specific tap that only runs on Some