flatMap<A, C, B> function

TaskEither<A, B> Function(TaskEither<A, C>) flatMap<A, C, B>(
  1. TaskEither<A, B> f(
    1. C
    )
)

Transforms the success value of a TaskEither instance into another TaskEither using the provided function f.

The flatMap function takes a function f of type TaskEither<A, B> Function(C) and returns a function that can transform a TaskEither with a success value of type C into a TaskEither with a success value of type B. If the TaskEither instance contains an error value, it remains unchanged.

This function is useful when you want to chain asynchronous computations that may fail, and transform the success value of one TaskEither into another TaskEither.

Example:

final myTaskEither = TaskEither<int, String>(() => Future.value(Right<int, String>('Hello')));

final transformedTaskEither = flatMap<String, int, String>((value) =>
    TaskEither<int, String>(() => Future.value(Right<int, String>('$value World')))
)(myTaskEither);

transformedTaskEither.value.then((result) {
  // result is Right<int, String>('Hello World')
});

final errorTaskEither = TaskEither<int, String>(() => Future.value(Left<int, String>(404)));

final anotherTransformedTaskEither = flatMap<String, int, String>((value) =>
    TaskEither<int, String>(() => Future.value(Right<int, String>('$value World')))
)(errorTaskEither);

anotherTransformedTaskEither.value.then((result) {
  // result is Left<int, String>(404)
});

f: The transformation function applied to the success value. Returns: A function that can transform a TaskEither<A, C> to a TaskEither<A, B>.

Implementation

TaskEither<A, B> Function(TaskEither<A, C>) flatMap<A, C, B>(
        TaskEither<A, B> Function(C) f) =>
    (TaskEither<A, C> taskEither) => TaskEither<A, B>(() async =>
        await match<A, C, e.Either<A, B>>((a) => Future.value(e.Left<A, B>(a)),
            (c) async => await f(c).value())(taskEither));