Nothing
if the original maybe
is Nothing
, or andMaybe
if the original maybe
is Just
.
You can think of this like a short-circuiting logical "and" operation on a
Maybe
type. If maybe
is just
, then the result is the
andMaybe
. If maybe
is Nothing
, the result is Nothing
.
This is useful when you have another Maybe
value you want to provide if and
only if you have a Just
– that is, when you need to make sure that if you
Nothing
, whatever else you're handing a Maybe
to also gets a Nothing
.
Notice that, unlike in map
or its variants, the original maybe
is
not involved in constructing the new Maybe
.
import Maybe from 'true-myth/maybe';
const justA = Maybe.just('A');
const justB = Maybe.just('B');
const nothing: Maybe<number> = nothing();
console.log(Maybe.and(justB, justA).toString()); // Just(B)
console.log(Maybe.and(justB, nothing).toString()); // Nothing
console.log(Maybe.and(nothing, justA).toString()); // Nothing
console.log(Maybe.and(nothing, nothing).toString()); // Nothing
Nothing
if the original maybe
is Nothing
, or andMaybe
if the original maybe
is Just
.
You can think of this like a short-circuiting logical "and" operation on a
Maybe
type. Ifmaybe
isjust
, then the result is theandMaybe
. Ifmaybe
isNothing
, the result isNothing
.This is useful when you have another
Maybe
value you want to provide if and only if you have aJust
– that is, when you need to make sure that if youNothing
, whatever else you're handing aMaybe
to also gets aNothing
.Notice that, unlike in
map
or its variants, the originalmaybe
is not involved in constructing the newMaybe
.Examples