Maybe (Monad)
The Maybe type in PureEval may differ from the Data Maybe found in other functional languages like Haskell. In PureEval, Maybe is a fusion of Data Maybe and Class Monad.
Maybe
The Maybe type encapsulates a value that may be indeterminate.
If an operation on a specific value within Maybe fails, it turns into Nothing.
Both undefined and null are considered as Nothing.
- Type
Maybe.of()
Lifts a value into a Maybe type.
- Type
- Example
Maybe.of(1); // Just 1
Maybe.of(null); // NothingMaybe.of(1); // Just 1
Maybe.of(null); // NothingMaybe.is()
Determines whether a value is of type Maybe.
- Type
- Example
Maybe.is(Just(1)); // true
Maybe.is(Nothing); // trueMaybe.is(Just(1)); // true
Maybe.is(Nothing); // trueMaybe.isNothing()
Determines if the Maybe is Nothing.
- Type
- Example
Just(1).isNothing(); // false
Just(null).isNothing(); // trueJust(1).isNothing(); // false
Just(null).isNothing(); // trueMaybe.map()
Maps the value inside Maybe through a function, constructing a new Maybe to hold the result.
- Type
- Example
Just(1).map(add(114513)); // Just 114514Just(1).map(add(114513)); // Just 114514Maybe.fold()
Applies a function to the value within Maybe a.
Takes two unary function parameters for handling cases when Maybe a is Nothing and when it is not.
- Type
- Example
Just('The body next door').fold(() => 'Nothing', id); // "The body next door"Just('The body next door').fold(() => 'Nothing', id); // "The body next door"Maybe.chain()
Chains a function that returns a Maybe type, allowing for method chaining.
- Type
- Example
// Example demonstrating method chaining using Maybe
// [...]
console.log(city); // 'Anytown'// Example demonstrating method chaining using Maybe
// [...]
console.log(city); // 'Anytown'Just()
Constructor function for the Maybe type.
Equivalent to Maybe.of.
- Type
- Example
Just(1); // Just 1Just(1); // Just 1Nothing
Constructor for the Maybe type representing the absence of a value.