ap<A, C, B> function

TaskEither<A, B> Function(TaskEither<A, C>) ap<A, C, B>(
  1. TaskEither<A, B Function(C)> fnTaskEither
)

Applies the provided function wrapped in a TaskEither instance to a TaskEither instance.

The ap function allows you to apply a function wrapped inside a TaskEither (usually referred to as an "applicative action") to another TaskEither instance. It is essentially used to combine the effects of two TaskEither instances.

This function is primarily useful in contexts where you have both data and function inside a context (in this case, TaskEither) and you want to apply this function to this data, all while keeping everything inside the context.

Example:

final myFunctionTaskEither = TaskEither<int, String Function(int)>(
    () => Future.value(Right<int, String Function(int)>((x) => 'Result: ${x + 10}'))
);

final myDataTaskEither = TaskEither<int, int>(() => Future.value(Right<int, int>(5)));

final resultTaskEither = ap(myFunctionTaskEither)(myDataTaskEither);

resultTaskEither.value.then((result) {
  // result is Right<int, String>('Result: 15')
});

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

final anotherResultTaskEither = ap(errorFunctionTaskEither)(myDataTaskEither);

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

fnTaskEither: The TaskEither containing the function to be applied. Returns: A function that takes a TaskEither and applies the function contained in fnTaskEither to it.

Implementation

TaskEither<A, B> Function(TaskEither<A, C>) ap<A, C, B>(
        TaskEither<A, B Function(C)> fnTaskEither) =>
    (TaskEither<A, C> taskEither) => flatMap<A, B Function(C), B>(
        (B Function(C) fn) => map<A, C, B>(fn)(taskEither))(fnTaskEither);