State
The State monad is one which harbors state variables in its context. It is a version of the Reader monad adapted primarily for the purpose of safely manipulating values that would otherwise be used globally. To follow is a list of primitives that show the potency of the State monad.
#
statestate(action)
state :: (s -> a s) -> m a
Embeds a simple action into the State monad.
Since:
bingo-functional
- v1.11.0bingo-functional-js
- v0.1.0
Argument(s):
- action - The action to place in the State monad
- JavaScript
- PHP
import { state } from 'bingo-functional-js'
const state = state((x) => x ** 3)
use function Chemem\Bingo\Functional\Functors\Monads\State\state;
$state = state(fn (int $x): int => $x ** 3);
#
putput(state)
put :: s -> m ()
Replaces the state inside the State monad.
Since:
bingo-functional
- v1.11.0bingo-functional-js
- v0.1.0
Argument(s):
- state - An arbitrary value to place in the State monad
- JavaScript
- PHP
import { put } from 'bingo-functional-js'
const state = put('foo')
use function Chemem\Bingo\Functional\Functors\Monads\State\put;
$state = put('foo');
#
getget()
get :: m s
Returns the state form the internals of the State monad.
Since:
bingo-functional
- v1.11.0bingo-functional-js
- v0.1.0
Argument(s):
None
- JavaScript
- PHP
import { get } from 'bingo-functional-js'
const state = get()
use function Chemem\Bingo\Functional\Functors\Monads\State\get;
$state = get();
#
getsgets(projection)
gets :: (s -> a) -> m a
Gets specific component of the state via a projection function.
Since:
bingo-functional
- v1.11.0bingo-functional-js
- v0.1.0
Argument(s):
- projection - Transformative projection function
- JavaScript
- PHP
import { gets, evalState } from 'bingo-functional-js'
const ucfirst = (str) => `${str.charAt(0).toUpperCase()}${str.substr(1)}`const state = gets((x) => ucfirst(x.toLowerCase()))
console.log(evalState(state, null)('lOKI'))
use Chemem\Bingo\Functional as f;use function Chemem\Bingo\Functional\Functors\Monads\State\{gets, evalState};
$state = gets(f\compose('strtolower', 'ucfirst'));
echo evalState($state, null)('lOKI');
#
modifymodify(function)
modify :: (s -> s) -> m ()
Maps an old state to a new state inside a State monad.
Since:
bingo-functional
- v1.11.0bingo-functional-js
- v0.1.0
Argument(s):
- function - Function to map onto old state
- JavaScript
- PHP
import { modify, evalState } from 'bingo-functional-js'
const ucfirst = (str) => `${str.charAt(0).toUpperCase()}${str.substr(1)}`const state = modify((x) => ucfirst(x.toLowerCase()))
console.log(evalState(state, null)('lOKI'))
use Chemem\Bingo\Functional as f;use function Chemem\Bingo\Functional\Functors\Monads\State\{modify, evalState};
$state = modify(f\compose('strtolower', 'ucfirst'));
echo evalState($state, null)('lOKI');
#
runStaterunState(state, value)
runState :: State s a -> s -> (a, s)
Unwraps a State monad computation.
Since:
bingo-functional
- v1.11.0bingo-functional-js
- v0.1.0
Argument(s):
- state - State monad object
- value - Arbitrary value to apply to State monad
- JavaScript
- PHP
import { runState, put } from 'bingo-functional-js'
const state = runState(put(2), 4)
use function Chemem\Bingo\Functional\Functors\Monads\State\{put, runState};
$state = runState(put(2), 4);
#
evalStateevalState(state, value)
evalState :: State s a -> s -> a
Evaluates a state computation with the given initial state; returns the final value.
Since:
bingo-functional
- v1.11.0bingo-functional-js
- v0.1.0
Argument(s):
- state - State monad object
- value - Arbitrary value to apply to State monad
- JavaScript
- PHP
import { evalState, put } from 'bingo-functional-js'
const state = evalState(put(2), 4)
use function Chemem\Bingo\Functional\Functors\Monads\State\{put, evalState};
$state = evalState(put(2), 4);
#
execStateexecState(state, value)
execState :: State s a -> s -> s
Evaluates a state computation with the given initial state; returns the final state.
Since:
bingo-functional
- v1.11.0bingo-functional-js
- v0.1.0
Argument(s):
- state - State monad object
- value - Arbitrary value to apply to State monad
- JavaScript
- PHP
import { execState, put } from 'bingo-functional-js'
const state = execState(put(2), 4)
use function Chemem\Bingo\Functional\Functors\Monads\State\{put, execState};
$state = execState(put(2), 4);