pub struct State<S, A> { /* private fields */ }Expand description
The State monad represents a stateful computation.
Implementations§
Source§impl<S, A> State<S, A>where
S: 'static,
A: Clone + 'static,
impl<S, A> State<S, A>where
S: 'static,
A: Clone + 'static,
Sourcepub fn value(a: A) -> State<S, A>
pub fn value(a: A) -> State<S, A>
Creates a new State with a constant value.
This method creates a State that, when run, will return the provided value and the unchanged state.
§Arguments
a- The value to be returned by the State
§Returns
State<S, A>- A new State that returns the provided value
§Examples
use prop_check_rs::state::State;
let state = State::<i32, String>::value("hello".to_string());
let (value, new_state) = state.run(42);
assert_eq!(value, "hello");
assert_eq!(new_state, 42); // State is unchangedSourcepub fn new<T, B, F>(f: F) -> State<T, B>
pub fn new<T, B, F>(f: F) -> State<T, B>
Creates a new State with a custom state transformation function.
This is the core constructor for State, allowing you to define exactly how the state should be transformed and what value should be produced.
§Arguments
f- A function that takes a state and returns a tuple of (value, new_state)
§Returns
State<T, B>- A new State that applies the provided function
§Type Parameters
T- The type of the stateB- The type of the value producedF- The type of the function
Sourcepub fn pure<B>(b: B) -> State<S, B>where
B: Clone + 'static,
pub fn pure<B>(b: B) -> State<S, B>where
B: Clone + 'static,
Creates a new State with a constant value (alias for value).
This method is functionally identical to value but is named to align with
the functional programming concept of “pure” or “return”.
§Arguments
b- The value to be returned by the State
§Returns
State<S, B>- A new State that returns the provided value
§Type Parameters
B- The type of the value to be returned
Sourcepub fn map<B, F>(self, f: F) -> State<S, B>
pub fn map<B, F>(self, f: F) -> State<S, B>
Transforms the value produced by this State using a function.
This method allows you to transform the value produced by a State without affecting how the state itself is transformed.
§Arguments
f- A function that transforms the value
§Returns
State<S, B>- A new State that produces the transformed value
§Type Parameters
B- The type of the transformed valueF- The type of the transformation function
Sourcepub fn flat_map<B, F>(self, f: F) -> State<S, B>
pub fn flat_map<B, F>(self, f: F) -> State<S, B>
Chains this State with a function that returns another State.
This method allows for sequential composition of stateful computations.
The function f is applied to the value produced by this State, and the
resulting State is then run with the updated state.
§Arguments
f- A function that takes the value from this State and returns a new State
§Returns
State<S, B>- A new State representing the sequential composition
§Type Parameters
B- The type of the value produced by the resulting StateF- The type of the function
Sourcepub fn and_then<B>(self, sb: State<S, B>) -> State<S, (A, B)>
pub fn and_then<B>(self, sb: State<S, B>) -> State<S, (A, B)>
Combines this State with another State, producing both values.
This method runs this State, then runs the provided State with the updated state, and returns both values as a tuple.
§Arguments
sb- Another State to run after this one
§Returns
State<S, (A, B)>- A new State that produces both values as a tuple
§Type Parameters
B- The type of the value produced by the second State
Sourcepub fn set<T>(t: T) -> State<T, ()>where
T: Clone + 'static,
pub fn set<T>(t: T) -> State<T, ()>where
T: Clone + 'static,
Creates a State that replaces the current state with a new value.
This method is useful for setting the state to a specific value, regardless of its current value.
§Arguments
t- The new state value
§Returns
State<T, ()>- A State that sets the state to the provided value
§Type Parameters
T- The type of the state
Sourcepub fn modify<T, F>(f: F) -> State<T, ()>
pub fn modify<T, F>(f: F) -> State<T, ()>
Creates a State that modifies the current state using a function.
This method allows you to transform the state based on its current value.
§Arguments
f- A function that transforms the state
§Returns
State<T, ()>- A State that modifies the state using the provided function
§Type Parameters
T- The type of the stateF- The type of the transformation function
Sourcepub fn sequence(sas: Vec<State<S, A>>) -> State<S, Vec<A>>
pub fn sequence(sas: Vec<State<S, A>>) -> State<S, Vec<A>>
Executes a sequence of States and collects their results into a vector.
This method runs each State in the provided vector in sequence, threading the state through each computation, and collects all the resulting values.
§Arguments
sas- A vector of States to execute in sequence
§Returns
State<S, Vec<A>>- A State that produces a vector of all the values