orElse

Allows recovering from from failed Maybe values.

Signature

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

Documentation

Allows recovering from from failed Maybe values.

While .chain() allows one to sequence operations, such that the second operation is ran only if the first one succeeds, and their state is propagated, .orElse() allows one to recover from a failed operation by providing a new state.

Example:

const Maybe = require('folktale/maybe');

function first(list) {
  return list.length > 0 ?   Maybe.Just(list[0])
  :      /* otherwise */     Maybe.Nothing();
}

let failures = 0;
function emitFailure() {
  failures += 1;
  return Maybe.Just('failed');
}

first(['one']).orElse(emitFailure);
// ==> Maybe.Just('one')

failures; // ==> 0

first([]).orElse(emitFailure);
// ==> Maybe.Just('failed')

failures; // ==> 1

Properties

Source Code

Defined in source/maybe/maybe.js at line 57, column 18
{
    /*~*/
    Nothing: function orElse(handler) {
      assertFunction('Maybe.Nothing#orElse', handler);
      return handler(this.value);
    },

    /*~*/
    Just: function orElse(handler) {
      assertFunction('Maybe.Nothing#orElse', handler);
      return this;
    }
  }
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/)