Skip to main content

Monads

A monad is a versatile functor that adheres to the laws of associativity, left-identity, and right-identity. The monads available as part of bingo-functional's offering include IO, State, Reader, List, Writer, Either, and Maybe. Monads, like other non Composite Data Type artifacts, are manipulable via function primitives. To follow is an elaborate listing of useful monad helper functions/primitives.

bind#

bind(function, monad)

bind :: Monad m => m a -> (a -> m b) -> m b

Sequentially composes two actions, passing any value produced by the first as an argument to the second. Akin to the >>= operator in Haskell.

Since:

  • bingo-functional - v1.11.0
  • bingo-functional-js - v0.1.0

Argument(s):

  • function - A Monadic function
  • monad - An instance of a Monad
use function Chemem\Bingo\Functional\Functors\Monads\bind;use Chemem\Bingo\Functional\Functors\Monads\Reader;
$res = bind(  fn (string $greet) => Reader\reader(fn (string $name) => (    f\concat('', $greet, '.', $name === 'world' ? '' : ' How are you?')  )),  Reader\reader(f\partial(f\concat, ' ', 'Hello,')));
$res->run('Loki');

mcompose#

mcompose(...functions)

mcompose :: m a -> n s -> n a

Composes two monadic operations from right to left.

Since:

  • bingo-functional - v1.11.0
  • bingo-functional-js - v0.1.0

Argument(s):

  • function - A Monadic function
use function Chemem\Bingo\Functional\Functors\Monads\mcompose;use Chemem\Bingo\Functional\Functors\Monads\State;
$res = mcompose(  fn (int $x) => State::of($x ** 3),  fn (int $x) => State::of($x ** 2),);
$res(State::of(2))->run('square then cube');

foldM#

foldM(function, list, accumulator)

foldM :: (a -> b -> m a) -> [b] -> c -> m b

Analogous to fold except its result is encapsulated within a monad.

Since:

  • bingo-functional - v1.12.0
  • bingo-functional-js - v0.1.0

Argument(s):

  • function - A Monadic fold function
  • list - Arbitrary list to transform
  • accumulator - The accumulator value
use function Chemem\Bingo\Functional\Functors\Monads\foldM;use Chemem\Bingo\Functional\Functors\Monads\IO;
$maxM = foldM(  fn ($acc, $val) => IO\IO(fn () => $val < $acc ? $val : $acc),  [4, 12, 23, 2, 19],  0);

filterM#

filterM(function, list)

filterM :: (a -> m a) -> [a] -> m [a]

Analogous to filter except its result is encapsulated within a monad.

Since:

  • bingo-functional - v1.12.0
  • bingo-functional-js - v0.1.0

Argument(s):

  • function - A Monadic filter function
  • list - Arbitrary list to transform
use function Chemem\Bingo\Functional\Functors\Monads\filterM;use Chemem\Bingo\Functional\Functors\Monads\IO;
$evenM = filterM(  fn (int $x) => IO\IO(fn () => $x % 2 == 0),  [4, 12, 23, 2, 19],);

mapM#

mapM(function, list)

mapM :: (a -> m b) -> [a] -> m [b]

Analogous to map except its result is encapsulated within a monad.

Since:

  • bingo-functional - v1.13.0
  • bingo-functional-js - v0.1.0

Argument(s):

  • function - A transformative function
  • list - Arbitrary list to transform
use function Chemem\Bingo\Functional\Functors\Monads\mapM;use Chemem\Bingo\Functional\Functors\Monads\IO;
$uppercase = mapM(  fn (string $str) => IO\IO(fn () => strtoupper($str)),  ['foo', 'bar', 'baz'],);

liftM#

liftM(function, ...args)

liftM :: Monad m => (a -> r) -> m a -> m r

Promotes a function to a monad.

Since:

  • bingo-functional - v2.0.0

Argument(s):

  • function - An arbitrary function
  • args - Monad-encapsulated function arguments
use function Chemem\Bingo\Functional\Functors\Monads\liftM;use Chemem\Bingo\Functional\Functors\Monads\State;
$msquare = liftM(fn ($x) => $x ** 2, State::of(3));