Options
All
  • Public
  • Public/Protected
  • All
Menu

Class State<S, A>

State is a Monad with the operations get and put, which can be used to model a single piece of mutable state (S) with a return value (A).

Type parameters

  • S

  • A

Hierarchy

  • State

Implements

Index

Properties

Methods

Properties

run

run: function

The wrapped computation.

Type declaration

    • (s: S): [A, S]
    • Parameters

      • s: S

      Returns [A, S]

Methods

apply

  • Apply a function in the result of one State to the result in this State.

    const doubled = State.of(double)
    const initial = State.of(1)
    
    const state = initial.apply(doubled)
    
    state.eval(0) // 2

    Type parameters

    • B

    Parameters

    Returns State<S, B>

eval

  • eval(s: S): A
  • Run a computation, discarding the final state.

    Parameters

    • s: S

    Returns A

exec

  • exec(s: S): S
  • Run a computation, discarding the result.

    Parameters

    • s: S

    Returns S

fmap

  • fmap<B>(func: function): State<S, B>
  • Bind a new computation to the State, transforming the result.

    const double = (n: number) => n * 2
    
    const state = State.of(1).fmap(a => put(double(a)))
    
    state.exec(0) // 2

    Type parameters

    • B

    Parameters

    • func: function
        • Parameters

          • a: A

          Returns State<S, B>

    Returns State<S, B>

map

  • map<B>(func: function): State<S, B>
  • Change the type of the result in an action.

    const double = (n: number) => n * 2
    
    const state = State.of(1).map(double)
    
    state.eval(0) // 2

    Type parameters

    • B

    Parameters

    • func: function
        • (a: A): B
        • Parameters

          • a: A

          Returns B

    Returns State<S, B>

Static get

  • Get the current state.

    const state = get()
    
    state.eval(1) // 1
    state.exec(1) // 1

    Type parameters

    • S

    Returns State<S, S>

Static gets

  • gets<S, A>(func: function): State<S, A>
  • Get a value which depends on the current state.

    const state = gets(double)
    
    state.eval(1) // 2
    state.exec(1) // 1

    Type parameters

    • S

    • A

    Parameters

    • func: function
        • (s: S): A
        • Parameters

          • s: S

          Returns A

    Returns State<S, A>

Static modify

  • modify<S>(func: function): State<S, undefined>
  • Modify the state by applying a function to the current state.

    const double = (n: number) => n * 2
    
    const state = modify(double)
    
    state.eval(1) // undefined
    state.exec(1) // 2

    Type parameters

    • S

    Parameters

    • func: function
        • (s: S): S
        • Parameters

          • s: S

          Returns S

    Returns State<S, undefined>

Static of

  • of<S, A>(a: A): State<S, A>
  • Create a new state that will replace the return value when run.

    Type parameters

    • S

    • A

    Parameters

    • a: A

    Returns State<S, A>

Static put

  • put<S>(s: S): State<S, undefined>
  • Set the state.

    const state = put(2)
    
    state.eval(1) // undefined
    state.exec(1) // 2

    Type parameters

    • S

    Parameters

    • s: S

    Returns State<S, undefined>

Generated using TypeDoc