State is parameterized by two types, a state s
and a resultant a
State should wrap a function of the form s -> Pair a s
and can be constructed by providing a function of this form. There are 3 methods that are available on the State for running with a given initial state
Implements: Monad
State constructor
Param | Type | Description |
---|---|---|
f | function |
A function of the form s -> Pair a that is wrapped by the State, nothing is executed until it is run with an initial state s |
State constructor that populates the right portion with it's argument. of essentially will lift a value of type a into a State
Param | Type | Description |
---|---|---|
v | any |
any value that needs to be lifted to the State |
Get a stringified version of the State
Apply the function f to the right portion of the State
Param | Type | Description |
---|---|---|
f | function |
Function |
Get the function within the State
ap allows for values wrapped in a State to be applied to functions also wrapped in a State. In order to use ap, the State must contain a function as its value on the right section.
Param | Type | Description |
---|---|---|
t | State |
State with function as the second element |
Chain together many computations that return a State
Param | Type | Description |
---|---|---|
f | function |
Function that returns another State |
As State is a lazy datatype that requires a initial state to run, it's instance provides a runWith method that takes in a initial state and returns the result of the computation as a Pair result state
.
Param | Type | Description |
---|---|---|
s | any |
An initial state that needs to be passed to the State |
When called, execWith will run the state transition with the given value as the initial state and will return the state.
Param | Type | Description |
---|---|---|
s | any |
An initial state that needs to be passed to the State |
When called, evalWith will run the state transition with the given value as the initial state and will return the resultant.
Param | Type | Description |
---|---|---|
s | any |
An initial state that needs to be passed to the State |