Combines two maybes such that if they both have values (are a Just
) their values
are concatenated. Maybe values are expected to be Fantasy Land 1.x semigroups and
implement a concat
method.
forall a: (Maybe a).(Maybe a) => Maybe a
where a is Semigroup
Combines two maybes such that if they both have values (are a Just
) their values
are concatenated. Maybe values are expected to be Fantasy Land 1.x semigroups and
implement a concat
method.
const Maybe = require('folktale/maybe');
Maybe.Just([1]).concat(Maybe.Just([2]));
// ==> Maybe.Just([1, 2])
Maybe.Just([1]).concat(Maybe.Nothing());
// ==> Maybe.Just([1])
Maybe.Nothing().concat(Maybe.Nothing());
// ==> Maybe.Nothing()
{
/*~*/
Nothing: function concat(aMaybe) {
assertMaybe('Maybe.Nothing#concat', aMaybe);
return aMaybe;
},
/*~*/
Just: function concat(aMaybe) {
assertMaybe('Maybe.Just#concat', aMaybe);
return aMaybe.matchWith({
Nothing: () => Just(this.value),
Just: (a) => Just(this.value.concat(a.value))
});
}
}