Skip to content

Latest commit

 

History

History
104 lines (70 loc) · 3.24 KB

state.md

File metadata and controls

104 lines (70 loc) · 3.24 KB

State

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(f)

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.of(v)

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

State.toString()

Get a stringified version of the State

State.map(f)

Apply the function f to the right portion of the State

Param Type Description
f function Function

State.getValue()

Get the function within the State

State.ap(t)

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

State.chain(f)

Chain together many computations that return a State

Param Type Description
f function Function that returns another State

State.runWith(s)

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

State.execWith(s)

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

State.evalWith(s)

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