If the Maybe is a Just, passes its value to the predicate. If the predicate returns true, then the Maybe is returned unchanged. In every other case, a Nothing gets returned.
This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.
forall a: (Maybe a).((a) => Boolean) => Maybe a
If the Maybe is a Just, passes its value to the predicate. If the predicate returns true, then the Maybe is returned unchanged. In every other case, a Nothing gets returned.
const Maybe = require('folktale/maybe');
// This line evaluates to true.
Maybe.Just.hasInstance(Maybe.Just(3).filter(n => n === 3));
// These lines evaluate to false.
Maybe.Just.hasInstance(Maybe.Just(2).filter(n => n === 3));
Maybe.Just.hasInstance(Maybe.Nothing().filter(n => n !== 3));
Maybe.Just.hasInstance(Maybe.Nothing().filter(n => n === 3));
{
/*~*/
Nothing: function filter(predicate) {
assertFunction('Maybe.Nothing#filter', predicate);
return this;
},
/*~*/
Just: function filter(predicate) {
assertFunction('Maybe.Just#filter', predicate);
return predicate(this.value) ? this : Nothing();
}
}