diff --git a/index.js b/index.js index 37c18808..20e52c15 100644 --- a/index.js +++ b/index.js @@ -2060,6 +2060,24 @@ return either.isLeft ? Nothing() : Just(either.value); }); + //# fromEither :: b -> Either a b -> b + //. + //. Takes a default value and an Either, and returns the Right value + //. if the Either is a Right; the default value otherwise. + //. + //. ```javascript + //. > S.fromEither(0, S.Right(42)) + //. 42 + //. + //. > S.fromEither(0, S.Left(42)) + //. 0 + //. ``` + S.fromEither = + def('fromEither', + {}, + [b, $Either(a, b), b], + function(x, either) { return either.isRight ? either.value : x; }); + //. ### Alternative // Alternative :: TypeClass