@b-jones-rfd/func-help - v0.7.1
    Preparing search index...

    Class Reader<R, A>

    A computation that needs an environment of type R and yields a value of type A.

    Nothing runs until run(env) is called, and each run runs 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).

    R is contravariant: a Reader that needs only part of an environment fits where the whole is supplied, so a Reader<{ log: Logger }, void> can be used as a Reader<Env, void> when Env has a log.

    A throw in a caller-supplied function is thrown from run(), not from the call that built the Reader.

    import { Reader } from '@b-jones-rfd/func-help'

    type Env = { baseUrl: string }

    const url = (path: string) =>
    Reader.asks((env: Env) => `${env.baseUrl}${path}`)

    url('/users').run({ baseUrl: 'https://api.test' })
    // 'https://api.test/users'

    Type Parameters

    • in R

      The environment the computation reads; contravariant.

    • out A

      The value the computation yields.

    Implements

    Index
    • 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.

      Returns Generator<Reader<R, A>, A, unknown>

      A generator that yields this Reader once.

      import { Reader } from '@b-jones-rfd/func-help'

      Reader.do(function* () {
      const n = yield* Reader.ask<number>() // calls this method
      return n * 2
      }).run(3) // 6
    • 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().

      Type Parameters

      • B

        The next step's value type.

      Parameters

      • f: (a: A) => Reader<R, B>

        The next step, given this step's value.

      Returns Reader<R, B>

      A Reader that runs this step, then the Reader f returns, both with the environment given to run.

      import { Reader } from '@b-jones-rfd/func-help'

      type Env = { baseUrl: string; version: string }

      const url = Reader.asks((env: Env) => env.baseUrl).bind((base) =>
      Reader.asks((env: Env) => `${base}/${env.version}`)
      )
      url.run({ baseUrl: 'https://api.test', version: 'v2' })
      // 'https://api.test/v2'
    • 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.

      Type Parameters

      • R2 = R

        The environment the result is run with; defaults to R.

      Parameters

      • f: (env: R2) => R

        Builds the environment this Reader sees from the one it is run with.

      Returns Reader<R2, A>

      A Reader that runs this one with f(env).

      import { Reader } from '@b-jones-rfd/func-help'

      type Env = { baseUrl: string; retries: number }

      const retries = Reader.asks((env: Env) => env.retries)
      const patient = retries.local((env) => ({ ...env, retries: env.retries * 2 }))

      patient.run({ baseUrl: 'https://api.test', retries: 3 }) // 6
    • Transform the value the computation yields; the environment is untouched.

      Haskell's fmap. If f throws, the error is thrown from run().

      Type Parameters

      • B

        The new value type.

      Parameters

      • f: (a: A) => B

        The function to apply to the value.

      Returns Reader<R, B>

      A Reader that yields f(value) with the same environment.

      import { Reader } from '@b-jones-rfd/func-help'

      Reader.asks((env: { retries: number }) => env.retries)
      .map((n) => n * 2)
      .run({ retries: 3 }) // 6
    • 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.

      Parameters

      • env: R

        The environment to run with.

      Returns A

      The value the computation yields.

      TypeError When a bind step returns a monad of another kind instead of a Reader. The types rule this out; only an untyped Do.run/Do.with or a cast can reach it.

      import { Reader } from '@b-jones-rfd/func-help'

      const greeting = Reader.asks((env: { name: string }) => `Hello, ${env.name}`)

      greeting.run({ name: 'Ada' }) // 'Hello, Ada'
      greeting.run({ name: 'Grace' }) // 'Hello, Grace'
    • Read the whole environment.

      Haskell's ask. Name the environment type, as nothing else can infer it.

      Type Parameters

      • R

        The environment type.

      Returns Reader<R, R>

      A Reader that yields the environment it is run with.

      import { Reader } from '@b-jones-rfd/func-help'

      type Env = { baseUrl: string }

      Reader.ask<Env>().run({ baseUrl: 'https://api.test' })
      // { baseUrl: 'https://api.test' }
    • Read a value derived from the environment.

      Haskell's asks. f runs on each run(env), not when the Reader is built; if f throws, the error is thrown from run().

      Type Parameters

      • R

        The environment type.

      • A

        The derived value type.

      Parameters

      • f: (env: R) => A

        Derives the value from the environment.

      Returns Reader<R, A>

      A Reader that yields f(env).

      import { Reader } from '@b-jones-rfd/func-help'

      const baseUrl = Reader.asks((env: { baseUrl: string }) => env.baseUrl)
      baseUrl.run({ baseUrl: 'https://api.test' }) // 'https://api.test'
    • Build 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*.

      Type Parameters

      • R

        The environment; inferred from the yielded Readers.

      • A

        The value the block returns.

      Parameters

      • block: () => Generator<Reader<R, unknown>, A, unknown>

        A generator function that yield*s Readers and returns the result.

      Returns Reader<R, A>

      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'
    • Lift a value into Reader, ignoring the environment.

      Haskell's pure for Reader r.

      Type Parameters

      • R

        The environment type.

      • A

        The value type.

      Parameters

      • value: A

        The value the computation yields.

      Returns Reader<R, A>

      A Reader that yields value for any environment.

      import { Reader } from '@b-jones-rfd/func-help'

      Reader.of<{ debug: boolean }, number>(3).run({ debug: true }) // 3