Skip to main content

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.0
  • bingo-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
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.0
  • bingo-functional-js - v0.1.0

Argument(s):

  • maybe - Instance of Maybe monad
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.0
  • bingo-functional-js - v0.1.0

Argument(s):

  • maybe - Instance of Maybe monad
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.0
  • bingo-functional-js - v0.1.0

Argument(s):

  • default - Arbitrary default value
  • maybe - Instance of Maybe monad
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.0
  • bingo-functional-js - v0.1.0

Argument(s):

  • list - An arbitrary array
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.0
  • bingo-functional-js - v0.1.0

Argument(s):

  • maybe - Instance of Maybe monad
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.0
  • bingo-functional-js - v0.1.0

Argument(s):

  • maybes - List of Maybe objects
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.0
  • bingo-functional-js - v0.1.0

Argument(s):

  • function - A discretionary transformer function
  • list - An arbitrary list
use Chemem\Bingo\Functional\Functors\Monads\Maybe;
$res = Maybe\mapMaybe(  fn (string $x) => Maybe::right(f\concat('', $x, 'ing')),  ['eat', 'sleep', 'fish', 'mix']);