apply

Applies the function contained in one Result to the value in another Result. Application only occurs if both Results are Ok, otherwise keeps the first Error.

Signature

forall a, b, c:
  (Result a ((b) => c)).(Result a b) => Result a c

Documentation

Applies the function contained in one Result to the value in another Result. Application only occurs if both Results are Ok, otherwise keeps the first Error.

Note that Result.Ok(x => x + 1).apply(Result.Ok(1)) is equivalent to Result.Ok(1).map(x => x + 1).

Example:

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

function increment(value) {
  return value + 1;
}

Result.Ok(increment).apply(Result.Ok(1));
// ==> Result.Ok(2)

Result.Ok(increment).apply(Result.Error(1));
// ==> Result.Error(1)

Result.Error(increment).apply(Result.Ok(1));
// ==> Result.Error(increment)

Result.Error(increment).apply(Result.Error(1));
// ==> Result.Error(increment)

Properties

Source Code

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

    /*~*/
    Ok: function apply(anResult) {
      assertResult('Result.Ok#apply', anResult);
      return anResult.map(this.value);
    }
  }
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/)