new State()
The State data structure is used to describe computations with intermediate states
- Version:
- 3.0.0
- Source:
Example
const {State} = require('futils').data;
const state = State((s) => State.Result(1, s));
state.run(0); // -> 1
state.exec(0); // -> 0
state.compute(0).value; // -> 1
state.compute(0).state; // -> 0
Classes
Methods
(static) get() → {State}
Returns a State which drops intermediate values and copies the intermediate state as the value and the state of the computation
- Source:
Returns:
- Type:
-
State
A new State with the intermediate state copied to the value
Example
const {State} = require('futils').data;
const state = State.get();
state.run(0); // -> 0
state.exec(0); // -> 0
(static) modify(f) → {State}
If given a function, uses the function to modify the intermediate state and drops the current value
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
A function which returns a new intermediate state from the current one |
Returns:
- Type:
-
State
A new State
Example
const {State} = require('futils').data;
const state = State.modify((n) => n + 1);
state.run(0); // -> null
state.exec(0); // -> 1
(static) of(a) → {State}
Lifts a value into a State
- Source:
Parameters:
Name | Type | Description |
---|---|---|
a |
any
|
The value to lift |
Returns:
- Type:
-
State
A new State structure
Example
const {State} = require('futils').data;
const state = State.of(1);
state.run(0); // -> 1
state.exec(0); // -> 0
(static) put(s) → {State}
Puts whatever is given as the new intermediate state, replacing the current value with null and dropping the current intermediate state
- Source:
Parameters:
Name | Type | Description |
---|---|---|
s |
any
|
Whatever should be the new intermediate state |
Returns:
- Type:
-
State
A new State
Example
const {State} = require('futils').data;
const state = State.put(1);
state.run(0); // -> null
state.exec(0); // -> 1
ap(a) → {State}
Applies a function as the current value of a State to a value in another State
- Source:
Parameters:
Name | Type | Description |
---|---|---|
a |
State
|
The State which has the current value |
Returns:
- Type:
-
State
State which contains the result of applying the function
Example
const {State} = require('futils').data;
const state = State.of(1);
const mInc = State.of((n) => n + 1);
mInc.ap(state); // -> State(2, ?)
exec(s) → {any}
Given an initial state, computes the final state and returns it
- Source:
Parameters:
Name | Type | Description |
---|---|---|
s |
any
|
The initial state |
Returns:
- Type:
-
any
The final state of the computation
Example
const {State} = require('futils').data;
State.of(1).run(0); // -> 0
flat() → {State}
Flattens a nested State one level
- Source:
Returns:
- Type:
-
State
A new State
Example
const {State} = require('futils').data;
const state = State.of(State.of(1));
state.flat(); // -> State(1, ?)
flatMap(f) → {State}
Maps a State returning function over a State and flattens the result
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
A State returning function to map |
Returns:
- Type:
-
State
A new State
Example
const {State} = require('futils').data;
const state = State.of(1);
const inc = (n) => State.of(n + 1);
state.flatMap(inc); // -> State(2, ?)
map(f) → {State}
Maps a function over the current value of a State
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
The function to map |
Returns:
- Type:
-
State
A new State
Example
const {State} = require('futils').data;
const state = State.of(1);
const inc = (n) => n + 1;
state.map(inc); // -> State(2, ?)
run(s) → {any}
Given an initial state, computes the final value and returns it
- Source:
Parameters:
Name | Type | Description |
---|---|---|
s |
any
|
The initial state |
Returns:
- Type:
-
any
The final value of the computation
Example
const {State} = require('futils').data;
State.of(1).run(0); // -> 1