Skip to main content

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.

state#

state(action)

state :: (s -> a s) -> m a

Embeds a simple action into the State monad.

Since:

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

Argument(s):

  • action - The action to place in the State monad
use function Chemem\Bingo\Functional\Functors\Monads\State\state;
$state = state(fn (int $x): int => $x ** 3);

put#

put(state)

put :: s -> m ()

Replaces the state inside the State monad.

Since:

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

Argument(s):

  • state - An arbitrary value to place in the State monad
use function Chemem\Bingo\Functional\Functors\Monads\State\put;
$state = put('foo');

get#

get()

get :: m s

Returns the state form the internals of the State monad.

Since:

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

Argument(s):

None

use function Chemem\Bingo\Functional\Functors\Monads\State\get;
$state = get();

gets#

gets(projection)

gets :: (s -> a) -> m a

Gets specific component of the state via a projection function.

Since:

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

Argument(s):

  • projection - Transformative projection function
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');

modify#

modify(function)

modify :: (s -> s) -> m ()

Maps an old state to a new state inside a State monad.

Since:

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

Argument(s):

  • function - Function to map onto old state
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');

runState#

runState(state, value)

runState :: State s a -> s -> (a, s)

Unwraps a State monad computation.

Since:

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

Argument(s):

  • state - State monad object
  • value - Arbitrary value to apply to State monad
use function Chemem\Bingo\Functional\Functors\Monads\State\{put, runState};
$state = runState(put(2), 4);

evalState#

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

Argument(s):

  • state - State monad object
  • value - Arbitrary value to apply to State monad
use function Chemem\Bingo\Functional\Functors\Monads\State\{put, evalState};
$state = evalState(put(2), 4);

execState#

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

Argument(s):

  • state - State monad object
  • value - Arbitrary value to apply to State monad
use function Chemem\Bingo\Functional\Functors\Monads\State\{put, execState};
$state = execState(put(2), 4);