tap<A, B> function

TapFunction<A, B> tap<A, B>(
  1. void f(
    1. B
    )
)

Takes an Either<A, B> and applies a function to the Right value if it exists. The tap function is often used for debugging or performing side effects.

Example:

void main() {
  final right = Right(5);
  final tapFn = tap((value) => print("Right value: $value"));
  tapFn(right);  // Prints: Right value: 5
}

Implementation

TapFunction<A, B> tap<A, B>(void Function(B) f) {
  return (Either<A, B> either) {
    if (either is Right<A, B>) {
      final Right<A, B> right = either;
      f(right.value);
    }
    return either;
  };
}