Sign in

    thy1016/mooncmd/monads does not have a README file

    EventWriter

    pub struct EventWriter[E, A] {
    run : () -> (A, Array[E])
    }

    EventWriter::bind

    fn[E, A, B] EventWriter::bind(self : EventWriter[E, A], f : (A) -> EventWriter[E, B]) -> EventWriter[E, B]

    组合

    EventWriter::eval

    fn[E, A] EventWriter::eval(self : EventWriter[E, A]) -> A

    EventWriter::exec

    fn[E, A] EventWriter::exec(self : EventWriter[E, A]) -> Array[E]

    EventWriter::map

    fn[E, A, B] EventWriter::map(self : EventWriter[E, A], f : (A) -> B) -> EventWriter[E, B]

    映射

    EventWriter::new

    fn[E, A] EventWriter::new(run : () -> (A, Array[E])) -> EventWriter[E, A]

    EventWriter::pure

    fn[E, A] EventWriter::pure(value : A) -> EventWriter[E, A]

    纯值

    EventWriter::run_writer

    fn[E, A] EventWriter::run_writer(self : EventWriter[E, A]) -> (A, Array[E])

    执行

    EventWriter::sequence

    fn[E, A] EventWriter::sequence(writers : Array[EventWriter[E, A]]) -> EventWriter[E, Array[A]]

    EventWriter::tell

    fn[E] EventWriter::tell(event : E) -> EventWriter[E, Unit]

    产生单个事件

    EventWriter::tell_many

    fn[E] EventWriter::tell_many(events : Array[E]) -> EventWriter[E, Unit]

    产生多个事件

    EventWriter::traverse

    fn[E, A, B] EventWriter::traverse(arr : Array[A], f : (A) -> EventWriter[E, B]) -> EventWriter[E, Array[B]]

    Reader

    pub struct Reader[Env, A] {
    // private fields
    }

    Reader::ask

    fn[Env] Reader::ask() -> Reader[Env, Env]

    Creates a Reader that simply returns the environment itself.

    Generic Parameters:

    • Env: The type of the environment.

    Returns a reader computation that produces the environment when run.

    Example:

    test "ask" {
    let reader = Reader::ask()
    inspect(reader.run("hello"), content="hello")
    }

    Reader::asks

    fn[Env, A] Reader::asks(f : (Env) -> A) -> Reader[Env, A]

    Creates a Reader that transforms the environment using a given function.

    Parameters:

    • f : A function that takes an environment of type Env and produces a value of type A. This function will be applied to the environment when the reader is run.

    Returns a new Reader that will apply the given function to the environment.

    Example:

    ///|
    test "asks" {
    let reader = Reader::asks(fn { env => env + 1 })
    inspect!(reader.run(41), content="42")
    }

    Reader::bind

    fn[Env, A, B] Reader::bind(reader : Reader[Env, A], f : (A) -> Reader[Env, B]) -> Reader[Env, B]

    Chains two reader computations by applying a function to the result of the first reader to produce a second reader computation.

    Parameters:

    • reader : The first reader computation that produces a value of type A.
    • f : A function that takes the result of the first reader and returns a new reader computation of type B.

    Returns a new reader that represents the sequential composition of the two computations.

    Example:

    ///|
    test "Reader::bind" {
    let reader1 = Reader::pure(5)
    let reader2 = reader1.bind(fn { x => Reader::pure(x * 2) })
    inspect!(reader2.run(0), content="10")
    }

    Reader::local_

    fn[Env, A] Reader::local_(reader : Reader[Env, A], modifier : (Env) -> Env) -> Reader[Env, A]

    Executes a reader computation with a modified environment.

    Parameters:

    • reader : A Reader computation that produces a value of type A.
    • modifier : A function that transforms the environment before it's passed to the reader computation.

    Returns a new reader that will run the original computation with the modified environment.

    Example:

    ///|
    test "Reader::local_" {
    let reader = Reader::new(fn { env => env * 2 })
    let modified = reader.local_(fn { env => env + 1 })
    inspect!(modified.run(5), content="12")
    }

    Reader::map

    fn[Env, A, B] Reader::map(reader : Reader[Env, A], f : (A) -> B) -> Reader[Env, B]

    Reader::new

    fn[Env, A] Reader::new(f : (Env) -> A) -> Reader[Env, A]

    Creates a new Reader from a function that transforms an environment into a value.

    Parameters:

    • f: A function that takes an environment of type Env and produces a value of type A. This function represents the computation that will be performed in the context of the environment.

    Returns a new Reader that encapsulates the given computation.

    Example:

    ///|
    test "Reader::new" {
    let reader : Reader[String, Int] = Reader::new(fn { env => env.length() })
    inspect!(reader.run("hello"), content="5")
    }

    Reader::pure

    fn[Env, A] Reader::pure(x : A) -> Reader[Env, A]

    Creates a new Reader that ignores the environment and always returns a constant value.

    Parameters:

    • x : The value to be returned by the reader.

    Generic Parameters:

    • Env : The type of environment (which will be ignored).
    • A : The type of the value to be returned.

    Returns a new Reader that always produces the given value regardless of the environment.

    Example:

    test "Reader::pure" {
    let reader = Reader::pure("constant")
    inspect(reader.run(42), content="constant")
    }

    Reader::run

    fn[Env, A] Reader::run(reader : Reader[Env, A], config : Env) -> A

    Executes a reader computation with the given environment.

    Parameters:

    • reader : A Reader that contains the computation to be executed.
    • environment : The environment value to be provided to the computation.

    Returns the result of running the reader computation with the given environment.

    Example:

    ///|
    test "Reader::run" {
    let reader = Reader::new(fn { env => env + 1 })
    inspect!(reader.run(41), content="42")
    }

    State

    type State[S, V]

    State::apply

    fn[S, V, W] State::apply(self : State[S, (V) -> W], arg : State[S, V]) -> State[S, W]

    State::bind

    fn[S, V, A] State::bind(self : State[S, V], f : (V) -> State[S, A]) -> State[S, A]

    State::eval_state

    fn[S, V] State::eval_state(self : State[S, V], state : S) -> V

    State::exec_state

    fn[S, V] State::exec_state(self : State[S, V], state : S) -> S

    State::get

    fn[S] State::get() -> State[S, S]

    State::map

    fn[S, V, W] State::map(self : State[S, V], f : (V) -> W) -> State[S, W]

    State::modify

    fn[S] State::modify(f : (S) -> S) -> State[S, Unit]

    State::new

    fn[S, V] State::new(val : V) -> State[S, V]

    State::put

    fn[S] State::put(new_state : S) -> State[S, Unit]

    State::run_state

    fn[S, V] State::run_state(self : State[S, V], state : S) -> (V, S)

    State::sequence

    fn[S, A] State::sequence(states : Array[State[S, A]]) -> State[S, Array[A]]

    将 Array[State[S, A]] 转为 State[S, Array[A]]

    State::state

    fn[S, V] State::state(f : (S) -> (V, S)) -> State[S, V]

    State::traverse

    fn[S, A, B] State::traverse(arr : Array[A], f : (A) -> State[S, B]) -> State[S, Array[B]]

    将 Array[A] 映射为 State[S, B] 后 sequence

    Writer

    pub struct Writer[W, A] {
    run : () -> (A, W)
    config : WriterConfig[W]
    }

    Writer Monad 类型

    Writer::bind

    fn[W, A, B] Writer::bind(self : Writer[W, A], f : (A) -> Writer[W, B]) -> Writer[W, B]

    组合

    Writer::eval

    fn[W, A] Writer::eval(self : Writer[W, A]) -> A

    执行并只获取值

    Writer::exec

    fn[W, A] Writer::exec(self : Writer[W, A]) -> W

    执行并只获取日志

    Writer::map

    fn[W, A, B] Writer::map(self : Writer[W, A], f : (A) -> B) -> Writer[W, B]

    映射

    Writer::new

    fn[W] Writer::new(empty : W, concat : (W, W) -> W) -> WriterConfig[W]

    Writer::pure

    fn[W, A] Writer::pure(value : A, config : WriterConfig[W]) -> Writer[W, A]

    Writer::run_writer

    fn[W, A] Writer::run_writer(self : Writer[W, A]) -> (A, W)

    执行并获取结果和日志

    Writer::tell

    fn[W] Writer::tell(log : W, config : WriterConfig[W]) -> Writer[W, Unit]

    产生日志

    WriterConfig

    pub struct WriterConfig[W] {
    empty : W
    concat : (W, W) -> W
    }