fromPredicate<A, B> function
- Predicate<
B> predicate, - A leftValue(
A function that takes a predicate and a function that produces a leftValue.
It returns a function that takes a value and returns an Either
that is Right
with the provided
value if the predicate returns true for this value, and Left
with the provided value if the predicate
returns false.
Example usage:
var eitherFromPredicate = fromPredicate<String, int>((value) => value > 0, () => 'Negative value');
// returns a function
Either<String, int> either1 = eitherFromPredicate(42); // This will be Right(42)
Either<String, int> either2 = eitherFromPredicate(-1); // This will be Left('Negative value')
@category lift
Implementation
Either<A, B> Function(B value) fromPredicate<A, B>(
Predicate<B> predicate, A Function() leftValue) =>
(B value) =>
predicate(value) ? right<A, B>(value) : left<A, B>(leftValue());