The environment the computation reads; contravariant.
The value the computation yields.
Let yield* bind this Reader inside a do block: yields this Reader and
returns the value the block is resumed with.
It exists for yield*, not for spreading: [...reader] is a one-element
array holding this Reader itself, and does not run it.
A generator that yields this Reader once.
Sequence a step that depends on this step's value; the next Reader gets the same environment.
Haskell's >>=.
If f throws, the error is thrown from run().
The next step's value type.
A Reader that runs this step, then the Reader f returns, both
with the environment given to run.
Run this Reader in a modified environment: it sees f(env) in place of
the environment it is run with. Only this Reader sees the change; a later
step in the same chain sees the original environment.
Haskell's local, as a method: Haskell's local f m is m.local(f).
Nested calls apply the outer function first: m.local(f).local(g) runs
m with f(g(env)). f runs on each run(env); if it throws, the
error is thrown from run().
f may also take a different outer environment (R2, which defaults to
R), as Haskell's withReader does. Its type is what keeps R
contravariant: a local typed (env: R) => R would make R invariant.
The environment the result is run with; defaults to R.
A Reader that runs this one with f(env).
Run the computation with an environment and return its value.
Haskell's runReader. Every call runs the whole computation again with
the environment given. A throw in any caller-supplied function in the
chain is thrown from here.
The environment to run with.
The value the computation yields.
StaticaskStaticasksStaticdoBuild a Reader from a do block: Haskell's do notation, with
x <- m written const x = yield* m.
Each yield* m runs m with the environment given to run and binds its
value, typed with no annotation. The block's environment is the most
specific one it yields: a Reader over part of the environment fits beside
one over the whole, so naming the full Env once (for example with
Reader.ask<Env>()) types the whole block. A block that yields only
Readers over separate parts, with nothing naming the whole, needs the
environment named: Reader.do<Env, A>(…).
Nothing in the block runs until run(env), and every run starts the block
afresh. A throw in the block is thrown from run(). A plain yield
(without *) binds unknown; use yield*.
The environment; inferred from the yielded Readers.
The value the block returns.
A Reader that runs the block and yields what it returns.
import { Reader } from '@b-jones-rfd/func-help'
type Env = { baseUrl: string; log: (msg: string) => void }
const url = (path: string) =>
Reader.asks((env: Env) => `${env.baseUrl}${path}`)
const program = Reader.do(function* () {
const u = yield* url('/users') // string
const env = yield* Reader.ask<Env>() // Env
env.log(`GET ${u}`)
return u
}) // Reader<Env, string>
program.run({ baseUrl: 'https://api.test', log: console.log })
// logs 'GET https://api.test/users', returns 'https://api.test/users'
Staticof
A computation that needs an environment of type
Rand yields a value of typeA.Nothing runs until
run(env)is called, and eachrunruns the whole computation again with the environment it is given, so a Reader can be built once and run against many environments (a test one and a real one).Ris contravariant: a Reader that needs only part of an environment fits where the whole is supplied, so aReader<{ log: Logger }, void>can be used as aReader<Env, void>whenEnvhas alog.A throw in a caller-supplied function is thrown from
run(), not from the call that built the Reader.Example