Maybe
The Maybe monad is, like the Either monad, a union type. It contextually evaluates to one of either a Just or Nothing value. The former is transformable while the latter always yields a unit type - null. To follow is a list of functions that meld well with the Maybe type.
maybe#
maybe(default, function, maybe)maybe :: b -> (a -> b) -> Maybe a -> b
Performs case-analysis on the Maybe monad: applies function to value inside Just if Maybe value is not Nothing, and returns a default value otherwise.
Since:
bingo-functional- v1.11.0bingo-functional-js- v0.1.0
Argument(s):
- default - Arbitrary default value
- function - Aribitrary function to apply to instance of Just
- maybe - Instance of Maybe monad
- JavaScript
- PHP
import { maybe, fromValue } from 'bingo-functional-js'
const val = 10const result = maybe( val, (x) => x ** 2, fromValue(val).filter((x) => x % 2 == 0),)use Chemem\Bingo\Functional\Functors\Monads\Maybe;
$val = 10;$result = Maybe\maybe( $val, fn (int $x): int => $x ** 2, Maybe::fromValue($val)->filter(fn (int $x): bool => $x % 2 == 0));isJust & isNothing#
isJust|isNothing(maybe)isJust :: Maybe a -> Bool | isNothing :: Maybe a -> Bool
Returns true if the argument is of type Just|Nothing.
Since:
bingo-functional- v1.11.0bingo-functional-js- v0.1.0
Argument(s):
- maybe - Instance of Maybe monad
- JavaScript
- PHP
import { isJust, isNothing, fromValue } from 'bingo-functional-js'
const just = fromValue(2)const nothing = fromValue(null)
isJust(nothing)isNothing(just)use function Chemem\Bingo\Functional\Functors\Monads\Maybe;
$just = Maybe::fromValue(2);$nothing = Maybe::nothing();
echo Maybe\isJust($nothing);echo Maybe\isNothing($just);fromJust#
fromJust(maybe)fromJust :: Maybe a -> a
Extracts the element out of a Just and throws an error if its argument is Nothing.
Since:
bingo-functional- v1.11.0bingo-functional-js- v0.1.0
Argument(s):
- maybe - Instance of Maybe monad
- JavaScript
- PHP
import { fromJust, fromValue } from 'bingo-functional-js'
const result = fromJust(fromValue('foo'))use Chemem\Bingo\Functional\Functors\Monads\Maybe;
$result = Maybe\fromJust(Maybe::fromValue('foo'));fromMaybe#
fromMaybe(default, maybe)fromMaybe :: a -> Maybe a -> a
Returns default value if Maybe is Nothing; returns Just value otherwise.
Since:
bingo-functional- v1.11.0bingo-functional-js- v0.1.0
Argument(s):
- default - Arbitrary default value
- maybe - Instance of Maybe monad
- JavaScript
- PHP
import { fromMaybe } from 'bingo-functional-js'
const result = fromMaybe(0, fromValue(null))use Chemem\Bingo\Functional\Functors\Monads\Maybe;
$result = Maybe\fromMaybe(Maybe::nothing());listToMaybe#
listToMaybe(list)listToMaybe :: [a] -> Maybe a
Returns Nothing if the primary argument is an empty list or a Just instance containing the first element of a non-empty list.
Since:
bingo-functional- v1.11.0bingo-functional-js- v0.1.0
Argument(s):
- list - An arbitrary array
- JavaScript
- PHP
import { listToMaybe } from 'bingo-functional-js'
const result = listToMaybe([3, 4, 5, 6, 7])use function Chemem\Bingo\Functional\Functors\Monads\Maybe;
$result = Maybe\listToMaybe(range(3, 7));maybeToList#
maybeToList(maybe)maybeToList :: Maybe a -> [a]
Returns an empty list when given Nothing or a singleton list otherwise.
Since:
bingo-functional- v1.11.0bingo-functional-js- v0.1.0
Argument(s):
- maybe - Instance of Maybe monad
- JavaScript
- PHP
import { maybeToList, fromValue } from 'bingo-functional-js'
const list = maybeToList(fromValue('foo'))use Chemem\Bingo\Functional\Functors\Monads\Maybe;
$list = Maybe\maybeToList(Maybe::just('foo'));catMaybes#
catMaybes(maybes)catMaybes :: [Maybe a] -> [a]
Returns a list of all the Just values from a list of Maybe objects.
Since:
bingo-functional- v1.11.0bingo-functional-js- v0.1.0
Argument(s):
- maybes - List of Maybe objects
- JavaScript
- PHP
import { catMaybes, fromValue } from 'bingo-functional-js'
const maybes = catMaybes([fromValue('foo'), fromValue(null), fromValue(12)])use Chemem\Bingo\Functional\Functors\Monads\Maybe;
$maybes = Maybe\catMaybes([ Maybe::just('foo'), Maybe::nothing(), Maybe::just(12)]);mapMaybe#
mapMaybe(function, list)mapMaybe :: (a -> Maybe b) -> [a] -> [b]
The mapMaybe function is a version of map which can throw out elements.
Since:
bingo-functional- v1.11.0bingo-functional-js- v0.1.0
Argument(s):
- function - A discretionary transformer function
- list - An arbitrary list
- JavaScript
- PHP
import { mapMaybe, concat, fromValue } from 'bingo-functional-js'
const res = mapMaybe( (x) => fromValue(concat('', x, 'ing')), ['eat', 'sleep', 'fish', 'mix'],)use Chemem\Bingo\Functional\Functors\Monads\Maybe;
$res = Maybe\mapMaybe( fn (string $x) => Maybe::right(f\concat('', $x, 'ing')), ['eat', 'sleep', 'fish', 'mix']);