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.
#
bindbind(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.0bingo-functional-js
- v0.1.0
Argument(s):
- function - A Monadic function
- monad - An instance of a Monad
- JavaScript
- PHP
import { bind, reader, concat, partial } from 'bingo-functional-js'
const res = bind( (greet) => reader((name) => concat('', greet, '.', name === 'world' ? '' : ' How are you?'), ), reader(partial(concat, ' ', 'Hello,')),)
res.run('Loki')
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');
#
mcomposemcompose(...functions)
mcompose :: m a -> n s -> n a
Composes two monadic operations from right to left.
Since:
bingo-functional
- v1.11.0bingo-functional-js
- v0.1.0
Argument(s):
- function - A Monadic function
- JavaScript
- PHP
import { mcompose, State } from 'bingo-functional-js'
const res = mcompose( (x) => State.of(x ** 3), (x) => State.of(x ** 2),)
res(State.of(2)).run('square then cube')
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');
#
foldMfoldM(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.0bingo-functional-js
- v0.1.0
Argument(s):
- function - A Monadic fold function
- list - Arbitrary list to transform
- accumulator - The accumulator value
- JavaScript
- PHP
import { foldM, IO } from 'bingo-functional-js'
const maxM = foldM( (acc, val) => IO(() => (val > acc ? val : acc)), [4, 12, 23, 2, 19], 0,)
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);
#
filterMfilterM(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.0bingo-functional-js
- v0.1.0
Argument(s):
- function - A Monadic filter function
- list - Arbitrary list to transform
- JavaScript
- PHP
import { filterM, fromValue } from 'bingo-functional-js'
const evenM = filterM((x) => fromValue(x % 2 === 0), [4, 12, 23, 2, 19])
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],);
#
mapMmapM(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.0bingo-functional-js
- v0.1.0
Argument(s):
- function - A transformative function
- list - Arbitrary list to transform
- JavaScript
- PHP
import { mapM, writer } from 'bingo-functional-js'
const squares = mapM((x) => writer(x ** 2, `square ${x}`), [3, 9, 12])
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'],);
#
liftMliftM(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));