filter

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.

Signature

forall a: (Maybe a).((a) => Boolean) => Maybe a

Documentation

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.

Example:

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));

Properties

Source Code

Defined in source/maybe/maybe.js at line 57, column 18
{
    /*~*/
    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();
    }
  }
Stability
stable
Licence
MIT
Module
folktale/maybe/maybe
Authors
Copyright
(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS
Authors
  • Quildreen Motta
Maintainers
  • Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)