fold

Applies a function to each case of a Result.

This feature is experimental!

This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.

Signature

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

Documentation

Applies a function to each case of a Result.

Example:

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

const inc = (x) => x + 1;
const dec = (x) => x - 1;

Result.Error(1).fold(inc, dec);
// ==> inc(1)

Result.Ok(1).fold(inc, dec);
// ==> dec(1)

Properties

Source Code

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

    /*~*/
    Ok: function fold(f, g) {
      assertFunction('Result.Ok#fold', f);
      assertFunction('Result.Ok#fold', g);
      return g(this.value);
    }
  }
Stability
experimental
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/)