fromPredicate<A, B> function
- Predicate<
B> predicate, - A leftValue(
Creates a TaskEither based on a predicate function.
This function evaluates a given predicate using the provided value. If the
predicate evaluates to true
, the result is a TaskEither that resolves to
a e.Right with the same value. If the predicate evaluates to false
, the result
is a TaskEither that resolves to a e.Left with the value produced by the leftValue
function.
/// Example:
final predicate = (int value) => value > 5;
final leftValue = () => "Less than or equal to 5";
final taskEitherFunction = fromPredicate<String, int>(predicate, leftValue);
taskEitherFunction(6).value().then((result) {
if (result is e.Right) {
print(result.value); // Outputs: 6
} else if (result is e.Left) {
print(result.value); // This line won't execute for the value 6
}
});
taskEitherFunction(4).value().then((result) {
if (result is e.Right) {
print(result.value); // This line won't execute for the value 4
} else if (result is e.Left) {
print(result.value); // Outputs: "Less than or equal to 5"
}
});
A
: The type of the left value.
B
: The type of the right value and the input value.
Returns a function that takes a value of type B
and produces a TaskEither of A
or B
.
Implementation
TaskEither<A, B> Function(B value) fromPredicate<A, B>(
Predicate<B> predicate,
A Function() leftValue,
) {
return (B value) =>
predicate(value) ? right<A, B>(value) : left<A, B>(leftValue());
}