filter

If the Result is a Ok, passes its value to the predicate. If the predicate returns true, then the Result is returned unchanged. In every other case, an Error gets returned.

Signature

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

Documentation

If the Result is a Ok, passes its value to the predicate. If the predicate returns true, then the Result is returned unchanged. In every other case, an Error gets returned.

Example:

const Result = require('folktale/result');

// This line evaluates to true.
Result.Ok.hasInstance(Result.Ok(3).filter(n => n === 3));

// These lines evaluate to false.
Result.Ok.hasInstance(Result.Ok(2).filter(n => n === 3));
Result.Ok.hasInstance(Result.Error(3).filter(n => n !== 3));
Result.Ok.hasInstance(Result.Error(3).filter(n => n === 3));

Properties

Source Code

Defined in source/result/result.js at line 75, column 19
{
    /*~*/
    Error: function filter(predicate) {
      assertFunction('Result.Error#filter', predicate);
      return this;
    },

    /*~*/
    Ok: function filter(predicate) {
      assertFunction('Result.Ok#filter', predicate);
      return predicate(this.value) ? this : Error();
    }
  }
Stability
stable
Licence
MIT
Module
folktale/result/result
Authors
Copyright
(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS
Authors
  • Quildreen Motta
Maintainers
  • Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)