reader.md

December 15, 2024 ยท View on GitHub

Reader

The Reader monad represents a computation that can read values from a shared environment, pass values from function to function, and execute sub-computations in a modified environment. It is also useful for dependency injection.

Implements: Monad

Reader(f)

The Reader constructor.

ParamTypeDescription
ffunctionA function of the form (e -> a) that is wrapped by the Reader. Nothing is executed until it is run with an environment.

Reader.of(v)

The Reader constructor that populates the right portion with its argument. of essentially lifts a value of type a into a Reader.

ParamTypeDescription
vanyAny value that needs to be lifted into the Reader

Reader.toString()

Gets a stringified version of the Reader.

Reader.map(f)

Applies the function f to the right portion of the Reader.

ParamTypeDescription
ffunctionFunction

Reader.getValue()

Gets the function within the Reader.

Reader.ap(t)

ap allows for values wrapped in a Reader to be applied to functions also wrapped in a Reader. In order to use ap, the Reader must contain a function as its value.

ParamTypeDescription
tReaderA Reader with a function as its value

Reader.chain(f)

Chains together many computations that return a Reader.

ParamTypeDescription
ffunctionFunction that returns another Reader

Reader.runWith(e)

Since Reader is a lazy datatype that requires a shared environment to run, its instance provides a runWith method. This method takes in an environment and returns the result of the computation.

ParamTypeDescription
eanyAn environment that needs to be passed to the Reader