Skip to main content
Module

x/functional/Maybe.js

Common Functional Programming Algebraic data types for JavaScript that is compatible with most modern browsers and Deno.
Go to Latest
File
import { assertIsDefined, assertIsNone } from "./asserts.js";import { factorizeSumType } from "./SumType.js";
const $$value = Symbol.for("TypeValue");
export const Maybe = factorizeSumType( "Maybe", { Nothing: [], Just: [ $$value ] });
Maybe.fromNullable = value => !assertIsDefined(value) || assertIsNone(value) ? Maybe.nothing() : Maybe.just(value);Maybe.just = value => Maybe.Just(value);Maybe.nothing = () => Maybe.Nothing;Maybe.of = value => Maybe.Just(value);
// alt :: Maybe a ~> Maybe a -> Maybe aMaybe.prototype.alt = Maybe.prototype["fantasy-land/alt"] = function (container) {
return this.fold({ Just: _ => this, Nothing: _ => container });};
// ap :: Maybe a ~> Maybe (a -> b) -> Maybe bMaybe.prototype.ap = Maybe.prototype["fantasy-land/ap"] = function (container) { if (Maybe.Nothing.is(this)) return this;
return Maybe.Just.is(container) ? Maybe.of(container[$$value](this[$$value])) : container;};
// chain :: Maybe a ~> (a -> Maybe b) -> Maybe bMaybe.prototype.chain = Maybe.prototype["fantasy-land/chain"] = function (unaryFunction) {
return this.fold({ Just: value => unaryFunction(value), Nothing: _ => Maybe.Nothing });};
// filter :: Maybe a ~> (a -> Boolean) -> Maybe aMaybe.prototype.filter = Maybe.prototype["fantasy-land/filter"] = function (predicate) {
return this.fold({ Just: _ => predicate(this[$$value]) ? this : Maybe.Nothing, Nothing: _ => this });};
// map :: Maybe a ~> (a -> b) -> Maybe bMaybe.prototype.map = Maybe.prototype["fantasy-land/map"] = function (unaryFunction) {
return this.fold({ Just: _ => Maybe.of(unaryFunction(this[$$value])), Nothing: _ => this });};
// reduce :: Maybe a ~> ((b, a) -> b, b) -> bMaybe.prototype.reduce = Maybe.prototype["fantasy-land/reduce"] = function (binaryFunction, accumulator) {
return this.fold({ Just: _ => binaryFunction(accumulator, this[$$value]), Nothing: _ => accumulator });};
Maybe.prototype.sequence = Maybe.prototype["fantasy-land/sequence"] = function (TypeRep) {
return this.traverse(TypeRep, x => x);};
// traverse :: Applicative f, Traversable t => t a ~> (TypeRep f, a -> f b) -> f (t b)Maybe.prototype.traverse = function (TypeRep, unaryFunction) {
return this.fold({ Just: _ => unaryFunction(this[$$value]).map(Maybe.of), Nothing: _ => TypeRep.of(this) });};
export default Maybe;