map<A, C, B> function
- B f(
- C
Transforms the success value of a TaskEither instance using the provided function f
.
The map function takes a function f
of type 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 perform a synchronous transformation on the success value of a TaskEither without affecting its error value.
Example:
final myTaskEither = TaskEither<int, String>(() => Future.value(Right<int, String>('Hello')));
final mappedTaskEither = map<String, int, String>((value) => '$value World')(myTaskEither);
mappedTaskEither.value.then((result) {
// result is Right<int, String>('Hello World')
});
final errorTaskEither = TaskEither<int, String>(() => Future.value(Left<int, String>(404)));
final anotherMappedTaskEither = map<String, int, String>((value) => '$value World')(errorTaskEither);
anotherMappedTaskEither.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>) map<A, C, B>(B Function(C) f) {
return (TaskEither<A, C> taskEither) => TaskEither<A, B>(() async {
return await match<A, C, e.Either<A, B>>(
(leftValue) async => e.Left<A, B>(leftValue),
(rightValue) async => e.Right<A, B>(f(rightValue)))(taskEither);
});
}