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

    Class Do

    Runs a generator function as a monadic do block, for a monad of your own.

    For the package's own types, use their do instead (Either.do, Except.do, IO.do, AsyncIO.do, State.do, Reader.do): it types every binding and returns the concrete type, with no casts.

    You yield monadic values inside a generator, and the helper stitches the bind / map calls together. It stays generic by taking the underlying monad's of up front, because Monad<T> has no static of to call.

    The result is typed Monad<A>, because the helper cannot know which monad of builds; cast it to the concrete monad (as IO<number>) to reach run() or fold().

    yield* m on a monad with a [Symbol.iterator] method that yields the monad and returns the value it is resumed with (as the package's types have) binds the value with its real type. A plain yield m works on any Monad, but the binding is typed any, because TypeScript cannot relate a yield's type to the value sent back in; annotate it.

    Unlike Haskell, the compiler does not check that every monad yielded in one block is the same kind as of builds; mixing kinds fails at run time.

    A throw in the generator surfaces the way the underlying monad surfaces a throw in bind: from run() for IO, State or Reader, as a rejection of run()'s promise for AsyncIO, and from the Do call itself for Either or Except.

    import { Do, IO } from '@b-jones-rfd/func-help'

    const DoIO = Do.with(IO.of)

    const prog = DoIO(function* () {
    const a = yield* IO.of(1) // number
    const b = yield* IO.of(2) // number
    return a + b
    }) as IO<number>

    prog.run() // 3
    Index
    • Returns Do

    • Runs one do block, given the underlying monad's of.

      The block's monad comes from of, not from the yielded values, so a block that yields nothing still has a monad to return. If gen throws, the error surfaces as the underlying monad's bind surfaces it.

      Type Parameters

      • A

        The value the generator returns.

      Parameters

      • of: <T>(value: T) => Monad<T>

        The underlying monad's of, used to start the block and wrap its return value.

      • gen: () => Generator<Monad<any>, A, any>

        The generator function holding the block. Each yield* (or plain yield) binds a monadic value; its return is the block's result.

      Returns Monad<A>

      A Monad<A> for the whole block; cast it to the concrete monad.

      import { Do, Either } from '@b-jones-rfd/func-help'

      const sum = Do.run(Either.of, function* () {
      const a = yield* Either.right<number, string>(1) // number
      const b = yield* Either.right<number, string>(2) // number
      return a + b
      }) as Either<string, number>

      sum.fold((e) => e, (n) => `${n}`) // '3'
    • Fixes the underlying monad's of, giving a runner for blocks in that monad.

      Binding of once saves passing it to every block, as Haskell's do infers the monad from context. A throw in the generator surfaces as in Do.run.

      Parameters

      • of: <T>(value: T) => Monad<T>

        The underlying monad's of, used to start each block and wrap its return value.

      Returns <A>(gen: () => Generator<Monad<any>, A, any>) => Monad<A>

      A function that takes a generator function and returns a Monad<A> for the block, as Do.run does; cast it to the concrete monad.

      import { Do, IO } from '@b-jones-rfd/func-help'

      const DoIO = Do.with(IO.of)

      const greeting = DoIO(function* () {
      const name = yield* IO.of('Ada') // string
      return `Hello, ${name}`
      }) as IO<string>

      greeting.run() // 'Hello, Ada'