Skip to content

fromPredicate

fromPredicate<E, A>(pred, onFalse): (a) => Result<E, A>

Defined in: Core/Result.ts:202

Creates a Result from a predicate applied to a value. Returns Ok if the predicate passes, Err from onFalse otherwise.

E

A

(a) => boolean

(a) => E

(a) => Result<E, A>

pipe(5, Result.fromPredicate(n => n > 0, n => `${n} is not positive`));  // Ok(5)
pipe(-1, Result.fromPredicate(n => n > 0, n => `${n} is not positive`)); // Err("-1 is not positive")
pipe("", Result.fromPredicate(s => s.length > 0, () => "empty string")); // Err("empty string")