Sign in

    reader

    An implementation of Reader Monad in Moonbit

    reader
    monad
    reader monad
    DI
    dependency injection
    Download zip
    Author
    Version
    0.4.0
    License
    Apache-2.0
    Last updated
    11 days ago
    Downloads
    3K

    No dependencies

    #colmugx/reader

    MoonBit implementation of the Reader Monad pattern.

    The Reader Monad allows you to easily manage and access a shared environment (like configuration or dependencies) throughout a computation without explicitly passing it around.

    #Installation

    moon add colmugx/reader

    #License

    This project appears to be licensed under the MIT License (based on the LICENSE file). Please refer to the LICENSE file for full details.

    Reader

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

    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(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::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(fn { env => env + 1 })
    inspect!(reader.run(41), content="42")
    }

    ask

    fn[Env] 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 = ask()
    inspect(reader.run("hello"), content="hello")
    }

    asks

    fn[Env, A] 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 = asks(fn { env => env + 1 })
    inspect!(reader.run(41), content="42")
    }

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io